How do I set a proper health bar?

Get help using Construct 2

Post » Mon Sep 28, 2015 5:45 am

Message: caad8user can only post plain text URLS until they have 500 rep. 1 URLS modified. Why?
It's been haunting me. I've read this tutorial: https://www.scirra.com/tutorials/449/cr ... -healthbar. I followed every step and I still can't figure out how to make a proper health bar for my game. In the tutorial: "Of course, in your games you do not always enter the number of damage or health or maximum health. You will kep'em in variables. And apply this method to the variables is easy." How do I apply this method from the tutorial to my player health instance variable?

Here's my dilemma: I set my player's instance variable health at start to 100 --> "set barHP sprite width to player.Heath". He defeats a boss, gets a power up, and the power up sets his health to 300 (or some arbritary number). I just want my player's health bar width set steady to just 100px width not 300px. I've been searching everywhere online on how to do it and I still can't figure it out. Can anyone please help me?

Thank you in advance!
B
28
S
11
G
15
Posts: 47
Reputation: 9,533

Post » Mon Sep 28, 2015 6:03 am

Hey, I actually think this recent question of mine could help you with this issue :)
viewtopic.php?f=147&t=154104
B
60
S
36
G
23
Posts: 341
Reputation: 17,157

Post » Mon Sep 28, 2015 6:13 am

@caad8user
In your specific case, you need two variables. Player_HP and Player_HPMax. Both starts at 100.

Set HP bar sprite width to ((Player_HP / Player_HPMax) * 100).

When you upgrade, raise the Player_HPMax to 300. So if the player health is 100/300, the bar width is 1/3 of 100 pixels only. Until you raise the Player_HP.

You can also apply that to enemies or npcs too if you want to scale their health but keep the bar the same width max.

Hope that helps you.
B
70
S
24
G
19
Posts: 1,757
Reputation: 17,614

Post » Mon Sep 28, 2015 6:25 am

Thank you so much your help guys! @Silverfroce, "Set HP bar sprite width to ((Player_HP / Player_HPMax) * 100)" was the formula that was missing! Thanks a lot! Health bar works properly now. :D
B
28
S
11
G
15
Posts: 47
Reputation: 9,533

Post » Mon Sep 28, 2015 6:40 am

@Silverforce, I'm sorry to bother you. But one more question. Since Player_HP and Player_HPMax are global? How can I "connect" those global variables to my player_Health instance variables? That way, when the player gets hit by an enemy bullet, it subtracts from the player_Health. Is there a way to do this?

Thank you.
B
28
S
11
G
15
Posts: 47
Reputation: 9,533

Post » Mon Sep 28, 2015 8:46 am

You can do the bar to any variable, global or instance.

If instance, you have 2 instance variables for your player sprite.

1. Health
2. HealthMax

Set the bar width to ((Player.Health / Player.HealthMax) * 100)
B
70
S
24
G
19
Posts: 1,757
Reputation: 17,614

Post » Mon Sep 28, 2015 11:17 am

@Silverforce, Thank you so much for your time and help. Your solution worked perfectly. :)
B
28
S
11
G
15
Posts: 47
Reputation: 9,533


Return to How do I....?

Who is online

Users browsing this forum: blue visionary, namaewayukidesu and 13 guests