Game Development Reference
In-Depth Information
Notice the use of a
switch
and
case
statements for each state instead of
if
statements.
Although the structure of a
switch
statement is a little different, it has the same effect
as a series of
if
statements in most situations. Just like an
if
statement, the
parenthesis of a
switch
statement contains the expression being evaluated. However,
case
statements are used to make different things happen when the expression
evaluates to different values. Each case is followed by a colon (
:
) and then a number of
statements, the last of which is a
break
statement to indicate the end of the statements
in that
case
. You can also leave off the
break
statement, in which case the contents of
the following
case
will be executed as well.
However, what's more important here is that we are setting the
sprite_index
variable
in each case so that Game Maker knows which sprite we are using. Finally, on line 42,
we draw that
sprite_index
sprite using the current
image_index
(subimage). This
produces is a neater and more effective solution for controlling sprites in our game.
The Ninja Elbow Walk
You may have already noticed that the level design in the two GML versions of Zool is slightly
different from the D&D version we made in Chapter 6. It has an additional green jelly ledge
platform on top of the first set of ramps. This is just a little taller than the existing blue one (see
Figure 11-3) and it reveals a problem with the way our existing code detects if Zool is standing
on top of a solid platform.
At the moment, we use the
place_meeting
function in
obj_zool_land
to check if Zool would
collide with an instance of
obj_platform
if he was moved down by a single pixel. However, our
new platform creates a situation where Zool is still colliding with an instance of
obj_ledge
(whi ch ha s
obj_platform
as a parent) if he is moved down by a single pixel, but it is his upper
body that is colliding—not his feet! If you try this out in the
zooldrag.gmk
version, then you'll see
that this allows Zool to run along the new platform when he clearly should not. We have
avoided this happening in the past by simply not creating this kind of situation in our level
design, but it is possible to fix this problem properly using GML.
Figure 11-3.
Zool is walking along the ledge supported by the collision with his upper body! Zool's collision
rectangle is shown in black and the ledge's collision mask is shown in purple
Search WWH ::
Custom Search