- Create new canvas actor and drag its edges to make it thin and long.
- Yet, create another regular actor "gui_meter" and add it some futuristic animation. This will be one of our decorations for the health meter that will be centered.
I'll be using this. - Optionally, create one more decorative GUI actor for the outer design (borders). This is mine.
- You'd also like to set it as a view's child.
- Now create a local variable called "percentage" type "Real"
So we can have a percentage of health for each actor we create. Isn't that a bit realistic? Theoretically everything has a health xD - Now navigate to global code and put this:
- Code: Select all
void set_health (Actor myactor, float value)
{
float p = value;
if(p > 99.0) p = 100.0;
if(p < 1.0) p = 0.0;
myactor.percentage = p;
}
void update_health_meter (Actor myactor)
{
int i;
double fraction = myactor.percentage * width / 100.0;
// Background texture of the healthmeter
for(i = 0; i < width; i += 2)
{
// Initializing our health gradient
setpen(0, 0, 64, 0, 1);
// Drawing our health gradient
moveto(i, 0);
lineto(i, height);
}
// Drawing health according to the fraction, skipping each 2nd pixel
for(i = 0; i < fraction; i += 2)
{
// Initializing our health gradient
setpen(255 - i, i * 2, 0, 0, 1);
// Drawing our health gradient
moveto(i, 0);
lineto(i, height);
}
// Drawing our hybrid-alike decoration at the center of the healthmeter (OPTIONAL)
//draw_from(gui_meter.name, width / 2, (height / 2) + 35, 1);
}
Note that it is not intended to be much fps-wise.
Now when something happen for instance the player gets in collision with an explosion, simply do that:
- Code: Select all
myplayer.percentage -= 10;
To decrease 10% HP of myplayer's initial health.
and along that, update the health meter with
- Code: Select all
update_health_meter(myplayer);
To save unneeded FPS consumption create one global variable "myplayer_suffers"
assign 1 to it when player gets injured and do that in the health meter's draw actor:
- Code: Select all
if(myplayer_suffers)
{
myplayer_suffers = 0;
update_health_meter(myplayer);
}
In the demo you can also decrease HP with down/up arrow key.