Game Development Reference
In-Depth Information
going on in your game. This is why we have been reinventing the wheel by creating an
obj_solid
object to act as our replacement for the
Solid
option, as well as including our own
Set Variable
actions that set
x
to
xprevious
and
y
to
yprevious
at the start of each collision event. For advanced
games, it is an approach that will prove far less problematic in the long run than using Game
Maker's built-in method.
A Scripted Rescue
This is all very well, but we haven't actually solved our original problem of collectible objects
getting in the way of collisions. Unfortunately, the truth is that this can't be solved just using
Game Maker's built-in drag-and-drop actions. What we need is the equivalent of a
Move to
Contact
action, which allows us to specify contact with
obj_solid
, rather than just
all objects
. We
can achieve this fairly easily by creating our own script and using the
Execute Script
action to
execute it. We'll call the script
move_to_contact_with
and it will provide all the same options as
the
Move to Contact
action (a direction, a maximum distance to move, and the type of thing to
move against). However, the final option will not just be a choice between solid or all objects, but
you will be able to provide the name of an object or even a specific instance. We're not going to
explain how the script works just now, but we will come back to it in Chapter 11. For now, simply
follow these instructions to set up the script.
Loading the Alternative Move to Contact Script
1.
From the
Scripts
menu, select
Import Scripts
.
2.
Select
move_to_contact_with.gml
from the
Chapter05/Resources
directory on the CD.
This will add this script to the
Scripts
folder in the resource tree. Double-click on it if
you're curious to take a look, but be careful not to change anything.
3.
Reopen
obj_zool_air
and select the
Collision
event with
obj_solid
. Locate the
Move
to Contact
action and reopen it. Notice the settings (
direction
,
-1
,
all objects
) and
then close it again. Now delete this action from the action list (click on it and press
Delete).
4.
In its place (directly after the second
Set Variable
action), include an
Execute Script
action (
control
tab). Set
Script
to
move_to_contact_with
,
Argument0
to
direction
,
Argument1
to
-1
, and
Argument2
to
obj_solid
. Notice how these settings are in the
same order as
Move to Contact
, but have generic names (
Argument0
, and so forth). As
you can use the
Execute Script
action to call any script, you have to remember the
order (or refer back to the script). The new script also allows us to use
obj_solid
instead of
all objects,
making our action only move into contact with solid objects and
not with collectibles.
If you run the game now, you will find that our problem seems to be fixed. Nonetheless, there
are many more places where we use the
Move to Contact
action with
all objects
and any of these
could cause similar problems with our game at some point. So, we now need to go through and
systematically change all of the rest over too. You can either follow the steps here or skip to the
end and load the file
zool8.gmk
from the
Chapter05/Games
directory on the CD.
Replacing the Remaining Move to Contact Actions
1.
Select and
Copy
the
Execute Script
action that you just created in the
obj_solid
Collision
event of
obj_zool_air
.
Select the
obj_ledge
Collision
event. Highlight the
Move to Contact
action and
Paste
the
Execute Script
action in its place. Delete the old
Move to Contact
action afterward.
2.
Search WWH ::
Custom Search