Incrementally filling/draining bars

For questions about using Classic.

Post » Wed Apr 17, 2013 11:10 am

Hello! I'm making a side-scroller that uses the following health bar (as an example):



Here it is at full health. When the player takes damage, it goes down one notch, like so:



The way I do this is by setting a "health" variable for the player because they can only take a maximum of four hits. I have a set width for the color inside the bar, a separate object called "healthbar_color", for each increment from 4 to 0.

My question is, how do I make it so that it doesn't just jump from one width to the other? Here is an example of what I want it to look like that I made in Photoshop:



Ideally, when the player loses 1 health, the bar will slide down to the next notch. Anyone out there able to help?Doomish2013-04-17 13:27:04
B
3
G
2
Posts: 17
Reputation: 759

Post » Wed Apr 17, 2013 12:43 pm

Maybe try something like this:

+Always
->Set playerhealthbar.width to

clamp(playerhealthbar.width - 1, (player.value('health') / 100) * playerhealthbar.value('maxwidth'), playerhealthbar.value('maxwidth'))

That should work (assuming player has 100 as health max)
"Construct 4 lets YOU make advanced games! (maybe)" Construct Classic - Examples Kit
B
86
S
28
G
13
Posts: 2,092
Reputation: 15,009

Post » Wed Apr 17, 2013 12:44 pm

I don't know if this will work. Set two variables, OldHP and HP, if OldHP is greater than HP then every X milliseconds subtract 0.1 from OldHP. And set the hp bar's width to OldHP.


Ha, jayjay's method is better and simple.poewar2013-04-17 12:51:08
B
7
S
2
G
1
Posts: 21
Reputation: 692

Post » Wed Apr 17, 2013 1:04 pm

[QUOTE=Jayjay] Maybe try something like this:

+Always
->Set playerhealthbar.width to

clamp(playerhealthbar.width - 1, (player.value('health') / 100) * playerhealthbar.value('maxwidth'), playerhealthbar.value('maxwidth'))

That should work (assuming player has 100 as health max) [/QUOTE]

Holy diver! I dunno what kind of arcane tome you had to decode to come up with that but it worked like a charm. I was even able to modify it a little to get it to incrementally refill when the player heals. Thanks!

Though... I have no idea WHY it actually works. A brief explanation of what any of that actually means would be very much appreciated. Doomish2013-04-17 13:11:47
B
3
G
2
Posts: 17
Reputation: 759

Post » Wed Apr 17, 2013 2:15 pm

+Always
->Set playerhealthbar.width to

clamp(playerhealthbar.width - 1, (player.value('health') / 100) * playerhealthbar.value('maxwidth'), playerhealthbar.value('maxwidth'))

--
This will always set hpbar.width-1 every ticks.
But if you only set this the hpbar will decrease continuously. So use the clamp to set the max and min hpbar.width value.

Clamp(Value, Lower bound, Upper bound)

Lower bound = hpbar.width you want it to be. You change this value the hpbar.width will -1 until this value. And hpbar.width-1 will stop when hpbar.width hit this value. This value is controlled by player health.
Player get damaged -> the lower bound changed -> hpbar.width start to decrease.
B
7
S
2
G
1
Posts: 21
Reputation: 692

Post » Sat Jun 29, 2013 12:11 pm

[QUOTE=Jayjay] Maybe try something like this:

+Always
->Set playerhealthbar.width to

clamp(playerhealthbar.width - 1, (player.value('health') / 100) * playerhealthbar.value('maxwidth'), playerhealthbar.value('maxwidth'))

That should work (assuming player has 100 as health max) [/QUOTE]

I cant get to work keep getting syntax errors
B
7
S
2
G
2
Posts: 34
Reputation: 1,682

Post » Sat Jun 29, 2013 7:07 pm

@SquareMation Ah are you trying to use it in C2?
"Construct 4 lets YOU make advanced games! (maybe)" Construct Classic - Examples Kit
B
86
S
28
G
13
Posts: 2,092
Reputation: 15,009

Post » Sat Jun 29, 2013 7:18 pm

[QUOTE=Jayjay] @SquareMation Ah are you trying to use it in C2?[/QUOTE]
yes
B
7
S
2
G
2
Posts: 34
Reputation: 1,682

Post » Mon Jul 01, 2013 2:06 am

@SquareMation Okay C2 will use this syntax:

+Always
->Set playerhealthbar.width to

clamp(playerhealthbar.width - 1, (player.health / 100) * playerhealthbar.maxwidth, playerhealthbar.maxwidth)

This is due to variables in CC working as "Object.Value('VariableName')" and C2 is just "Object.VariableName"

Make sure to add Health as variable to player, and MaxWidth as variable to health bar object
"Construct 4 lets YOU make advanced games! (maybe)" Construct Classic - Examples Kit
B
86
S
28
G
13
Posts: 2,092
Reputation: 15,009


Return to Help & Support using Construct Classic

Who is online

Users browsing this forum: No registered users and 0 guests