Tutorial 13: Creating a Player Healthbar

In this tutorial we will learn how to create a player health bar by setting up a variable for the player health. The number assigned to this variable are the number of frames on the GIF. This tutorial could be replicated for lives.

Create a Health Bar Sprite

  1. Create a new sprite called sprHealthBar and import sprHealthBar.gif  below.
    (right click and save this image to your computer)
    Healtbar sprite

Setup Health for your Player object

  1. In the Create event of your player object you will need a variable to track the players health.

Set the variable to the amount of frames your health bar or lives GIF has:

(Note you may already have coded this in a previous tutorial. If so skip to the Create a HealthBar object section )

maxHealth = 10; 

currentHealth = maxHealth;

Create a collision event on the player object with the enemy & the enemy’s laser objects and deduct 1 from the currentHealth variable:

currentHealth -=1;

Create a Health Bar Object

  1. Create an object for your health bar and associate the sprHealthBar sprite with it
  2. Add a Create Event and initialise the object as follows:
    image_speed =0; //stop the health bar animating
    image_index=0; // start on the frame index of 0
  3. Add a Step event to it and insert this code:
    image_index = objPlayer.currentHealth;
    image_index = 11;
  4. Drag the healthbar object into the game room.
    Place it either at the top or bottom of the screen so that it not in the way of the player.

Additional Step:

There is a tutorial on how to create a camera room, if you want your healthbar to follow the camera use the following code

x = camera_get_view_x(view_camera[0])+900;
y = camera_get_view_y(view_camera[0])+600;

Shaun Spalding explains this well in the following video:

<Previous Tutorial 12: Create a start screen

Next>Tutorial 14: Create a light effect

Similar Posts


  1. There’s a problem with the step event code.
    “ objHealthBar.x = view_xport[0].x + 15; ”
    This code will only function if you add “//“ at the start of the code.
    This is what the code should look like for the code to function.
    //objHealthBar.x = view_xport[0].x + 15;

    1. Yes that is true, but that line of code is useful when you have a room with a view enabled.

  2. Problem with the code for the additional step, shouldn’t be

    x = camera_get_view_x(view_camera[0])+900;
    y = camera_get_view_y(view_camera[0])+600;

    it should be

    xDifference = camera_get_view_x(view_camera[0])+900;
    yDifference = camera_get_view_y(view_camera[0])+600;

Leave a Reply

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