Moving Actors and Triggering Events
*prerequisite: Making Actors Move Along Paths
Almost as likely as you'll need to move actors in your game, you'll want to trigger events when they reach their destination. This is simply implemented with the room script instance's changeState() method.
Getting Started
*Not needed if you have already done the chapter, Making Actors Move Along Paths.
Since the ego is an actor, everything in this lesson can be applied to it as well. However, to keep things simple, we will create another actor. Scroll down to the end of the script and add the following lines of code:
Set up the actor with the following lines of code:
|
Setting Up The Actor's Motion
Simply call the setMotion method and set it's motion to MoveTo. Make sure the fourth parameter is set the the script's RoomScript instance.
(aMan:
init()
setCycle(Walk)
setMotion(MoveTo 180 150 RoomScript)
)The actor is now set to move to the coordinates (180,150).
Handling States
Script states are used as event triggers. States are fully explained in the Volume One of the tutorial.
Each state has a number identifying it.
Add the following method to the RoomScript instance:
(method (changeState newState)
= state newState
(switch (state)
(case 0
Print("This is state 0! The room has started!")
= cycles 1
)
(case 1
Print("This is state 1! The room has now FULLY started!")
)
(case 2
Print("The view has moved to it's first position!")
(aMan:setMotion(MoveTo 250 100 RoomScript))
)
(case 3
Print("The view has moved to it's second position!")
)
)
)
- When the room script is first executed, state 0 is called.
- If a case is provided for state 0, it is executed.
- If state 0's case sets the cycles property to anything greater than zero, it will wait that many interpreter cycles and then execute the next state.
- In this case, it sets the cycles to 1, so one interpreter cycle after executing state 0, it executes state 1.
- By this time, the room has been fully set up.
- Since state 1's case does not set the cycles property, it ends the state execution.
- Next, when the actor reaches it's first destination (180,150), it calls state 2's case (the next state).
- In this example state 2's case sets the actor to move to (250,100).
- When the actor reaches it's destination (250,100), it calls the next state's case (state 3).
- And of course, since the states don't set the cycles property, it ends the state execution.
Compile and run and you'll see it all in action!
That sums up actor movement and triggering events. Good luck!