Skip to content

Enemies

  • Lesson
  • 1 min read
  • 9 of 9 in this chapter

Enemy behavior

We are now going to set how our enemies will behave. In this case, we are going to make them reset our room when we touch them and make them move until they meet a wall and go in the opposite direction.

Making them move

Now go to the enemy object and go to "Add Event". Add the "Create" Event and put:

Text
hspd = 2;

Make another event that is a step event and put this code now:

Text
if (place_meeting(x + hspd, y, obj_wall))
{
 hspd = -hspd;
}
x += hspd;

This code will check if there is a wall ahead and bounce back into the opposite direction and repeat again infinitely.

Resetting the room

Now we are going back to our player object and adding this code in our step event:

Text
if( place_meeting( x, y, obj_enemy ) ) { 
room_restart(); 
}

If you touch the enemy, the room should reset back, therefore sending you back to the spawn point.

That's all!

Thank you for reading through this course! If you found an error or found this course hard to understand please contact us.