• 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!

Opacity based health system.

Lightmind

Member
Basically, when my player takes damage, i want the health to go down from 100 to 90. Then, based on the health the player is at, i want the opacity of one heart to change. I've got 5 hearts, each representing 20 health. They are all in different objects. The first heart goes from 100 to 0 opacity from health 100-80. The second heart goes from 100 to 0 opacity from health 80-60 etc.
 

Nidoking

Member
Sounds like a job for image_alpha, draw_set_alpha, or passing the appropriate alpha value to whatever you're using to draw the sprites.
 

FoxyOfJungle

Kazan Games
You can always divide the current value by the maximum value, getting a value from 0 to 1, which will be the opacity. Do this for each heart.
 

chamaeleon

Member
Maybe something like
GML:
for (var i = 0; i < 5; i++) {
    var alpha = max(0, health_counter-20*i)/20;
    draw_sprite_ext(spr_heart, 0, x+64*i, y, 1, 1, 0, c_white, alpha);
}
Not very well tested. I might have made a mistake here or there with the math logic.
 
Tried to make it general and intuitive. Haven't tested it yet, but hopefully works.


GML:
var hearts = hearts_max*(health/health_max),   //num hearts filled
swid = sprite_get_width(spr_heart); //width of sprite
for(var alp, h = 0; h < hearts_max; h++){
    alp = min(hearts-h, 1.0);  //alpha 1 if below current hearts
    alp = max(hearts-h. 0.0); //alpha 0 if above current hearts
//alpha between 0 and 1 if less than 1 above current hearts
    draw_sprite_ext(spr_heart, 0, x+h*swid, y, 1, 1, 0, c_white, alp);
}
 

chamaeleon

Member
GML:
    alp = min(hearts-h, 1.0);  //alpha 1 if below current hearts
    alp = max(hearts-h, 0.0); //alpha 0 if above current hearts
The second line overwrites the result of the first. Perhaps you want to use clamp().
GML:
alp = clamp(hearts-h, 0.0, 1.0);
which is equivalent to
GML:
alp = max(min(hearts-h, 1.0), 0.0);
or
GML:
alp = min(hearts-h, 1.0);
alp = max(alp, 0.0);
 
The second line overwrites the result of the first. Perhaps you want to use clamp().
GML:
alp = clamp(hearts-h, 0.0, 1.0);
which is equivalent to
GML:
alp = max(min(hearts-h, 1.0), 0.0);
or
GML:
alp = min(hearts-h, 1.0);
alp = max(alp, 0.0);
Haha, you're right. In my head I was thinking
GML:
alp = min(hears-h, 1.0);
alp = max(alp, 0.0);
Which does the same thing as using clamp, which I forgot existed.
 
Top