Game Development Reference
In-Depth Information
4.
Now include the following three lines at the end of the code, before the last curly
bracket but after the end of the block of code for the switch statement:
1: instance_deactivate_object( obj_ledge );
2: mp_potential_step_object( targetx, targety, 8, obj_solid );
3: instance_activate_object( obj_ledge );
The second line uses Game Maker's path finding to find its way to the target position at
a speed of 8 pixels per step while avoiding solid objects. The lines before and after it
effectively make the path finding ignore ledges, as we want Archie to fly straight
through them. Ledges are used for rope bridges in Shadows on Deck , which can
sometimes block the whole path and would otherwise prevent Archie from following
Flynn.
Run the game now and you should have Archie following you around the level as you move
around. He can't go quite as fast as you, but he's relentless in his pursuit—try as hard as you
might, it's (nearly) impossible to shake him off.
Aye Aye, Captain
All of Flynn's behaviors are controlled with the keyboard, but Archie will be controlled with the
mouse. Clicking anywhere on the screen will send Archie to that point, where he will stay until
he falls out of view. If you click on a pirate, then Archie will follow that pirate around (and
eventually distract him) until he disappears from view.
Implementing the Command Behavior for the Parrot Object
1. Add a Mouse, Global Mouse, Global Left Pressed event to obj_parrot . This event is
executed whenever the player clicks with the left mouse button anywhere in the room.
Include an Execute Code action containing the following code:
1: {
2: targetx = mouse_x;
3: targety = mouse_y;
4:
5: state = ASTATE_COMMAND;
6:
7: target_id = collision_point( mouse_x, mouse_y, obj_enemy, true, true );
8:
9: if( target_id != noone )
10: sound_play( snd_archie_command );
11: }
Lines 2 and 3 set Archie's target position to be wherever the mouse was clicked in
the room. This is not actually as straightforward as these two lines of code make it look
because we are using a view that only shows a small section of the room. However,
Game Maker automatically handles all that for us so we don't need to worry about it.
Line 7 uses the collision_point function to see if there are any instances of
obj_enemy at the position the player just clicked. This returns either the id of one object
found at that point, or noone if it finds nothing.
Lines 9 and 10 play a sound effect for Archie if the player has clicked on an enemy
to confirm that he has identified the enemy instance as a target.
 
Search WWH ::




Custom Search