Maths trouble for sprite_part health bar

hdarren

Member
Hi. I am trying to make a health bar sprite using draw_sprite_part so that the less health the player has the less of the health sprite is shown.

The sprite goes from bottom to top and the max health is changeable. So at 100% I need a value of 0 and at 0 health I need a value of the full sprite height based on global.var_MaxHealth.

I am struggling with the equation to work this out though so I am hoping I can get help with that formula. Thank you.
 
D

DestroyTheRunner

Guest
This can be easily adapted to your own variables, here´s the prototype:
Create Event:
lifebar_spr_max_height = sprite_get_height(spr_lifebar); //Get the real/max height of the sprite (lifebar) and stores it to a local variable
lifebar_spr_height = 0; //This will be calculate on step event, it will be the real height of the bar
life_val_max = 100; //This is the maximum 'health points' the instance can have (I think it´s your global.var_MaxHealth)
life_val_actual = 50; //This is your 'health point' in the moment (Let´s pretend the player started with half of life)

lifebar_spr_width = sprite_get_width(spr_lifebar); //Stores the width of the sprite this won´t need to change.

Step Event:
lifebar_spr_height= (life_val_actual*lifebar_spr_max_height)/life_val_max; //This is the calculation

Draw Event:
draw_sprite_part(spr_lifebar,0,0,0,lifebar_spr_width, lifebar_spr_height,x,y);
 
A

Aura

Guest
Pretty simple.

Code:
((global.var_MaxHealth - global.var_Health) / global.var_MaxHealth) * 100
 
L

leonfook29

Guest
Or
Code:
(1 - (hp_current / global.var_MaxHealth)) * sprite_height
Or just throw out the multiply sprite height and you get 0 when full health, 1 when no health.
 
Top