Game Development Reference
In-Depth Information
2.
Return to the
Step
event of
obj_parrot
and edit the code again. This time, expand the
second
case
statement to include the following code:
1:
case ASTATE_COMMAND:
2:
3:
if( target_id != noone )
4:
{
5:
targetx = target_id.x;
6:
targety = target_id.bbox_top;
7:
}
8:
9:
if( x > targetx )
10:
facing = FACE_LEFT;
11:
else if( x < targetx )
12:
facing = FACE_RIGHT;
13:
14:
break;
Although the
Mouse
event will already have set
targetx
and
targety
values for Archie
to move toward, an enemy would probably have moved by the time he gets there.
Therefore, lines 3-7 look to see if there is a target and update the target coordinates
accordingly.
3.
Add a new
Other, Views, Outside View 0
event and include an
Execute Code
action
containing the following code. Basically, this just plays a sound effect (saying “wait for
Archie”) and then reverts back to the follow state.
1:
{
2:
if( state == ASTATE_COMMAND )
3:
sound_play( snd_archie_wait );
4:
state = ASTATE_FOLLOW;
5:
}
Now run the game again and try out Archie's new skill. It's a bit difficult to tell if he is
following pirates as they don't move yet, but he should go wherever you click and return to your
side when he goes out of view.
Flapping Distraction
Archie's final (and most useful) skill is to be able to distract and lure enemies away from Flynn
and into vulnerable situations. To make the animation look a bit more realistic, we have
cr e a te d a pa th r e sour ce ca l l e d
path_distract
, which defines a pretty wild path for Archie to take
(see Figure 12-6). When you assign a path to an instance in Game Maker, you can either use
absolute
or
relative
coordinates. Absolute coordinates treat points on the path as exact positions
in the room, and relative coordinates will map the start position of the path (green square) to
the instance's current position and move it relative to the path from there.
Unfortunately, neither of these works for us, as we actually want Archie to follow a path
and
an enemy at the same time. In other words, we want it to be relative, but relative to the moving
position of the pirate. The easiest way to do this is simply to create another object that follows
the path starting at
X
=
0
,
Y
=
0
(the top left corner of the room) and then add its
x
and
y
posi ti on
onto the moving position of the pirate. We'll even feed this back into the path finding target
position so that Archie doesn't move through the landscape too much during his dance.
Search WWH ::
Custom Search