Tutorial 10: Collisions and Health Setup

In this tutorial, we will learn how to setup your player and enemy health and how to destroy them based on some collision rules.

Set the player collision mask

Go to the Resources menu:

  1. Open Sprites. Open your player sprite, sprPlayer
  2. Find Collision Mask in the Sprite window. Open this area by clicking the small arrow beside the words
  3. In the Collision Mask area, from Type, choose Precise (Slow)

A screenshot of a cell phone Description automatically generated

Setup player health

  1. Open the player object, objPlayer, from the Resource menu
  2. Go to the Create event and add the following code:

myLives = 3; //maximum number of lives
maxHealth = 10; //this sets up a counter for you players health. Increase or decrease it if you like

currentHealth = maxHealth;

 

Collision with enemy ship

Create a collision event on the player object with the enemy ship and add this code:

currentHealth -= 2;

collision event in gamemaker studio 2

Setup a collision with the enemy laser

collision event with the enemy laser

  1. On the player object, add a collision event with the enemy laser (as seen above).
  2. Inside the event add this code
currentHealth -= 1; //take some health off the player

with(other){

	instance_destroy(); //destroy the enemy laser when it touches the player

}

Setup a step event to check the status of the players health

In the Step Step event for your player object add the following code:

if(currentHealth<=0){
   myLives-=1; //take 1 life off everytime your health reaches 0

if(myLives <=0){ //only destroy the player when lives reaches 0
  room_goto(EndRoom1); //we will create this in the next tutorial
   instance_destroy();
}else{
currentHealth = maxHealth;
instance_destroy(objEnemy1);
instance_destroy(objEnemyLazer);

  }
}

Enemy health setup

Do the same steps as above to the enemy object – but setup the enemies maxHealth to be smaller than 10, for example make it 5. This will make the enemy easier to destroy. You can setup more difficult enemies and boss level enemies in other rooms later that are more difficult to destroy.

Setup the enemy health

Add a Create event to the enemy object with the following code:

maxHealth = 2; //increase this to make the enemy harder to destroy

currentHealth = maxHealth;

Collision Event with Player object

On your enemy object, add a collision event with the player object. Add the following code to it:

instance_destroy(); //destroy the enemy object when it hits the player

 

Collision Event with Player Lazer or Bullet object

On your enemy object, add a collision event with the player lazer object.

currentHealth -= 1; //take some health off the enemy

with(other){
instance_destroy(); //destroy the player's laser when it touches the enemy
}

if(currentHealth<=0){ //destroy the enemy when its health reaches 0
instance_destroy();
}

To learn more about the with() method watch this video:

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *