Game Development Reference
In-Depth Information
Avoiding Enemy Collisions by Turning Around
1.
First, create a new object called
obj_enemy
. We'll use this object as a parent to help us
check for collisions with all enemies, as well as define the required turn-around
behavior for all of them.
2.
Add a
Collision
event and include a
Set Variable
action with
Variable
path_speed
and
value
-path_speed
(note the minus sign).
3.
Now, open
obj_mummy
and set the parent to
obj_enemy
. Do the same for
obj_scorpion
.
4.
To test this, add another
obj_mummy
to the room and set its
Creation Code
to read as
follows:
1:
{
2:
path_start(path_mummy1,2,3,true);
3:
path_position = 0.5;
4:
}
Result:
Reference/Result/patrolling_enemies3.gmk
Of course, you may want the enemies to try and complete their path regardless of what
crosses the road. So let's make the enemy wait for other enemies passing through.
Avoiding enemy collisions by waiting
1.
Open
obj_enemy
and remove the collision event with
obj_enemy
.
2.
Add a
Step
,
End Step
event and include an
Execute Code
action. Insert the following
lines:
1:
{
2:
dx = x+lengthdir_x(2,direction);
3:
dy = y+lengthdir_y(2,direction);
4:
if ( collision_rectangle(dx,dy,dx+sprite_width,
5:
dy+sprite_height,obj_enemy,false,true) )
6:
{
7:
path_position = path_positionprevious;
8:
image_speed = 0;
9:
}
10:
}
Lines 2-3 calculate the x and y position just in front of the object if it would continue in
its current direction. The first parameter in each
lengthdir
function specifies the
distance—the larger it is, the further away it waits.
Search WWH ::
Custom Search