Game Development Reference
In-Depth Information
1: {
2: switch ( direction )
3: {
4: case 0: sprite_index = spr_mummy_right; break;
5: case 90: sprite_index = spr_mummy_up; break;
6: case 180: sprite_index = spr_mummy_left; break;
7: case 270: sprite_index = spr_mummy_down; break;
8: }
9: image_speed = path_speed/8;
10: }
Using a switch statement, we check the direction and change the sprite accordingly: a
value of 0 means we're going to the right, 90 is upward, 180 is left, and 270 is downward.
Finally, we set the sprite's animation to be relative to the path speed. This means that if
you ever change the path speed, the animation pace changes as well.
10. Right-click this Execute a piece of code (Execute Code) action and select Copy from
the context menu. Now open obj_scorpion , add a Step , Step event, right-click in the
Actions list, and select Paste . Next, open the event and replace every occurrence of
mummy with scorpion , and set image_speed to path_speed/4 .
Result: Reference/Result/patrolling_enemies1.gmk
It's easy to make more than one enemy share a path, so that a particular area is patrolled
more heavily.
Putting Multiple Enemies on a Single Path
1. Open room_test and place three other instances of obj_scorpion in the room.
2. Hold the Control key and right-click the first of the new instances of obj_scorpion .
Select Creation Codeā€¦ from the menu and insert the following lines:
1: {
2: path_start(path_scorpion1,2,1,true);
3: path_position = 0.25;
4: }
The path position indicates where on the path this instance should be placed, a
number between 0 and 1 inclusive. If not specified, it defaults to 0 (the start of the
path). We set it at 0.25 , which means it is a quarter of the way along the path.
3.
Set similar creation codes for the third and the fourth instance, with path positions at
0.5 and 0.75 , respectively.
Result: Reference/Result/patrolling_enemies2.gmk
If you've been watching the different enemies long enough, you may have noticed that they
sometimes walk into each other. The easy solution to this, of course, is to make sure the paths
never cross. If that's not always an option, we can make sure that enemies check ahead for a
possible collision, and turn around if necessary.
 
 
Search WWH ::




Custom Search