Game Development Reference
In-Depth Information
Now when you run your game, you should have a much better view of Zool as he moves
around the level (albeit in mid-air and at incredible speeds). He should also stop when he collides
with walls (but pass through ramps). If you have problems, then you will find a version of the
game at this point in the file
zool1.gmk
in the
Chapter03/Games
directory on the CD.
Crash Landing a Ninja
Clearly, walking on thin air is not right, so we'll start by putting some limits on Zool's speed and
then we'll get his feet on the ground. We know from the previous chapter that it is necessary to
limit the speed of moving objects to prevent them from moving so fast that they pass through
other objects (one of the problems of discrete time sampling). We'll do this in the
Step
event
because this takes place immediately after the keyboard events that change Zool's speed. We'll
put this
Step
event in
obj_zool
(the parent Zool object) because then the event will be inherited
by all Zool objects and that will save us repeating the same actions in each object.
Limiting Zool's Speed
1.
Define two new constants using the
Define Constants
option on the
Resources
menu
(or two new variables in the
Create
event of
obj_zool
, if you are using Game Maker
Lite). Set both
MAX_HSPEED
and
MAX_FALL_SPEED
to
14
.
Add a
Step, Step
event
to
obj_zool
and include a
Set Variable
action. Set the
Variable
vspeed
and type
min(MAX_FALL_SPEED,
vspeed)
into
Value
. Here,
min
is a Game Maker
function that returns the minimum value of its arguments (the values separated by
commas). So while
vspeed
is smaller than
MAX_FALL_SPEED
, the
min
function will return
the value of
vspeed
and the
action will set
vspeed
to the value of
vspeed
(which changes
nothing). Yet, when
vspeed
is greater than
MAX_FALL_SPEED
, the
min
function will return
MAX_FALL_SPEED
and the action will set
vspeed
to
MAX_FALL_SPEED
. In this way, the
min
function limits Zool's falling speed to a maximum of 14 pixels per step.
2.
Include another
Set
Variable
action.
This time,
set the
Variable
hspeed
and type
median(-MAX_HSPEED,
hspeed,
MAX_HSPEED)
into
Value
. Here,
median
is a different
function that computes the middle value of its arguments when they are arranged in
order of size. So while
hspeed
is less than
MAX_SPEED
and more than
-MAX_HSPEED
, the
middle value will be
hspeed
. However, if
hspeed
is greater than
MAX_HSPEED
, then the
middle value becomes
MAX_HSPEED
and if
hspeed
is smaller than
-MAX_HSPEED
, then the
middle value becomes
-MAX_HSPEED
. In this way, we can keep
hspeed
between -14 and
+14 pixels per step just using a single action.
3.
Caution
We'll need to remember that if we create a
Step
event for any of the other Zool objects (which
have
obj_zool
as a parent), then the new
Step
event will replace this inherited
Step
event. That means
our speed clamping actions won't be called for that object and Zool will potentially move too fast again.
■
If you play the game now, Zool is limited to a sensible running speed—also notice how he
slides to a halt when you release a key when he is moving fast. This is a result of the friction
gradually bringing him to a halt when you are no longer applying a force to move him. Next, we'll
start to implement behaviors for
obj_zool_air
and add a bit of gravity into the equation.
For our purposes, we will consider a “normal” gravity setting to be a downwards force of 2
pixels per step. Logically,
obj_zool_air
should always be subject to this same gravitational pull,
Search WWH ::
Custom Search