• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Health Bar with Sprites (HELP!)

MacGregory

Member
people, how can I make health bar sprites? there are 3 sprites sp_HP, sp_Flash, sp_HPback how can I unite them? dimensions for all 368x40 help pzhl code, otherwise I can not find how to do it.
 

3dgeminis

Member
In the DRAW event draw the 3 sprites separately. The order determines which one is drawn on top of each other.
Draw first sp_HPback, then sp_HP, finally sp_Flash.
 
Use the Draw_GUI event and have the layers drawn in order. Here's my code as an example. It draws the black/empty hearts first and then draws full hearts over them dependant on how much health the player has any given frame

GML:
/// @description Draw HUD



//Draw empty hearts

draw_sprite(HP_Heart_Spr, 1, 10, 10); 
draw_sprite(HP_Heart_Spr, 1, 42, 10); 
draw_sprite(HP_Heart_Spr, 1, 74, 10); 
draw_sprite(HP_Heart_Spr, 1, 106, 10); 
draw_sprite(HP_Heart_Spr, 1, 138, 10); 




//Draw and erase full hearts for health meter

if global.HP_Hearts >0 {
draw_sprite(HP_Heart_Spr, 2, 10, 10); 
}

if global.HP_Hearts >1 {
draw_sprite(HP_Heart_Spr, 2, 42, 10); 
}

if global.HP_Hearts >2 {
draw_sprite(HP_Heart_Spr, 2, 74, 10); 
}

if global.HP_Hearts >3 {
draw_sprite(HP_Heart_Spr, 2, 106, 10); 
}

if global.HP_Hearts >4 {
draw_sprite(HP_Heart_Spr, 2, 138, 10); 
}

If you need something flash you could have getting hit trigger a damage timer that sets a variable like flash that's set to -1 (false) at the players creation, ticks up to a certain threshold, and resets itself every time it goes off.

The sub-image being drawn changes based on the ticks of the timer and the current health level. Then make it when the alarm hits that limit it subtracts health instead of resetting itself. You might want to set the tick to 0 at the object's creation

So

GML:
if dam_ticks < 5{
    dam_tick += 1;
    flash = flash*-1
    alarm[0] = global.game_speed*0.2;}

else if dam_ticks = 5{
health -= 1
flash = -1
dam_ticks = 0
}

Then when it draws the health something like this to alter the sub image

GML:
if health > 0 {

    if flash = 1{
        draw_sprite(HP_Heart_Spr, 2, 10, 10)}; 
      
    if flash = -1{
        draw_sprite(HP_Heart_Spr, 1, 10, 10)};

}



But you'd have to play around with this code to make it do exactly what you want
 
Top