SOLVED Issues with Healthbar

I been working on an health-bar system for my enemy and I got everything working. The issues I have is that the health(the red) of the enemy isn't in the right position on where it should and it dosent fill the healthbar correctly and the health bar isn't following the enemy when he takes damage.

GML:
    var grunt_health = (hp/max_hp) * 100
    bar_x = 700
    bar_y = 500
    c = c_red
    width = sprite_get_width(spr_enemyheathbar)
    height = sprite_get_height(spr_enemyheathbar)
    
    if alarm[1] > 0
    { 
    draw_rectangle_color(bar_x, bar_y - height, bar_x + (width * grunt_health), bar_y + (height), c, c, c, c, false);
    draw_sprite(spr_enemyheathbar, 0, bar_x, bar_y);
    }
 

Slyddar

Member
The issues I have is that the health(the red) of the enemy isn't in the right position on where it should and it dosent fill the healthbar correctly and the health bar isn't following the enemy when he takes damage.
If you want to have the health bar follow the player, you need to draw it relative to it's x and y position, and not a static position. So bar_x should just be x, and bar_y should be y minus the distance you want the bar above the enemies origin. You also need to convert grunt_health into a value between 0 and 1, as you want the width to be between 0 and 100% of the width. You can do this by dividing it by it's maximum health.
 

kingyo

Member
You will get precise and specific information if you show us images of what you expect and the actual result. It would be even better to have information about where the sprite origin is.
As for the position not following the movement of the enemy, you could update bar_x and bar_y to match the x and y of the enemy.
 
If you want to have the health bar follow the player, you need to draw it relative to it's x and y position, and not a static position. So bar_x should just be x, and bar_y should be y minus the distance you want the bar above the enemies origin. You also need to convert grunt_health into a value between 0 and 1, as you want the width to be between 0 and 100% of the width. You can do this by dividing it by it's maximum health.

I got the health bar to follow the player, that is exactly what I needed, thank you so much. However, I still have issues on what you said. "You also need to convert grunt_health into a value between 0 and 1, as you want the width to be between 0 and 100% of the width. You can do this by dividing it by it's maximum health."

How would I set it up so the health covers the healthbar? This is what is happening now.

1638767986340.png
 

Slyddar

Member
How would I set it up so the health covers the healthbar? This is what is happening now.
It looks like you are wanting just a simple healthbar, and if that's the case you could use the function draw_healthbar() instead, otherwise you will need to do some math to position the outside of the bar above the rectangle you are drawing.
 
It looks like you are wanting just a simple healthbar, and if that's the case you could use the function draw_healthbar() instead, otherwise you will need to do some math to position the outside of the bar above the rectangle you are drawing.

I got it to work somewhat, but the healthbar is too long for some reason and it stretches halfway through the room. I need to fix that issue and to shorten the bar itself.

GML:
    var grunt_health = (hp/max_hp) * 100
    bar_x = x
    bar_y = y - 150
    c = c_red
    width = sprite_get_width(spr_enemyheathbar)
    height = sprite_get_height(spr_enemyheathbar)
    
    if alarm[1] > 0
    { 
    draw_healthbar(bar_x, bar_y - height, bar_x + (width * grunt_health), bar_y + (height), grunt_health, c_black, c, c, 0 ,false, false);

    }
 
It's too long because of this
GML:
  draw_healthbar(**** width * grunt_health *****); }
your variable grunt_health goes from 0 to 100, wheras you need it in the 0-1 range.
Just do that tweak
GML:
var grunt_health = (hp/max_hp) // * 100 -> delete this horror
And F.Y.I. 'for some reason' is, in more than 99.99% of cases, the person programming. ;)
 
It's too long because of this


your variable grunt_health goes from 0 to 100, wheras you need it in the 0-1 range.
Just do that tweak
GML:
var grunt_health = (hp/max_hp) // * 100
And F.Y.I. 'for some reason' is, in more than 99.99% of cases, the person programming. ;)
Wow... that was the issue. And it was a simple fix too. Thank you so much for helping me out. :)
 
Top