Game Development Reference
In-Depth Information
Adding the Animation End Event to the Walking State Object
1.
Click on the
Add Event
button and select the
Other, Animation End
event.
2.
Include a
Change Instance
action (
main1
tab) that changes into
obj_pod_standing
and
select
yes
for
Perform Events
.
We're now very close to being able to test our character events for the first time, but before
we can do that, we're actually going to have to create another object for Fishpod. This isn't
another state object but an object that sets up Fishpod's initial properties in the level. Normally,
we would use the
Create
event of one of the state objects (probably
obj_pod_standing
) to provide
any starting values (for things such as health and lives, for example). However, we are already
using the
Create
events to set up the behavior of each state. If we put starting values in these
Create
events as well, then they would get reset between states (so you would go back to full
health while walking, for instance). This is clearly not desirable, so instead we will create a new
obj_pod
object that sets up the initial properties for the character before changing itself into
obj_pod_standing
. In fact, we don't want to give our character health at this stage, but we do need
to initialize the
facing
variable for the character. This will be done in the
Create
event of
obj_pod
,
as it will only ever be created once at the very start of the level. It is essential that we initialize this
value somewhere; otherwise, Game
Maker will quite rightly give an error the first time we try and
check what the value of
facing
is.
Creating a New Starting Object and Its Create Event
1.
Create a new object called
obj_pod
. You can set its
Sprite
to any one of the Fishpod sprites.
2.
Click on the
Add Event
button and select the
Create
event.
Include a
Set Variable
action (
control
tab). Type
facing
into
Variable
and
FACE_LEFT
into
Value
. This sets the initial facing direction of Fishpod to the left.
3.
Include a
Change Instance
action (
main1
tab). Select
obj_pod_standing
from the
Change Into
menu and
yes
for
Perform Events
. This will put the object into the
standing state and it will never return to the starting object again.
4.
■
Note
If you are using the
Lite
version of Game Maker, then now is the time to “fake” the constant values
by including
Set Variable
actions (
control
tab) at the very start of the
Create
event for
obj_pod
that to
create variables called
FACE_LEFT
with a value of
1
and
FACE_RIGHT
with a value of
2
. Unlike constants,
these variables will only be accessible in Fishpod objects, rather than globally (as constants are), but it
makes no difference to this example. You might wonder if we could have achieved the same thing simply
by setting the
facing
variable to the
strings
"FACE_LEFT"
or
"FACE_RIGHT"
. However, while strings would
be just as readable for people, they are much harder and slower for machines to process, so the variable
or constant approach is much more advisable.
You can now reopen your test room and place an instance of
obj_pod
somewhere within the
level. Run the game and check that you can move Fishpod left and right using the arrow keys on
the keyboard. If you can't, then go back and check your steps carefully or compare your version
against our version in
Chapter02/Games/fishpod2.gmk
on the CD. Note that the character will
currently walk on thin air and through platforms as we've not handled those things yet. Pressing
the space bar will also cause Fishpod to spin endlessly and you'll have to quit the game (this is
because he is stuck in the jumping state and there is no way out of that state yet).
Search WWH ::
Custom Search