### [Solved] Need help with calculations for a HP Bar

#### homeofdeath212

• Posts: 122
As the title says, I need help with calculations for the hp bar I'm working on.

Desired result:
The HP bar for the boss has X(let's say 20) stacks. Each stack is 5k hp. The boss has 100k hp so that means the hp bar would have 20 stacks. Each time the boss losses 5k hp, a stack is deducted and the hp bar fills up again but this time instead of x 20, it would say x 19 and would repeat the process until the boss is dead.

I got most of that to work as intended. My problem is that the hp bar won't fill up to max (only up to half at most) whenever a stack is deducted and it needs to be refilled again.

Bonus: How does coloring work exactly for image instances? Because with the image I use, the colors all don't show up correctly except red. (Example: https://www.youtube.com/watch?v=85tj-pc80A8&feature=youtu.be [look at the hp bar below] )

The code I've used so far:

« Last Edit: July 24, 2019, 11:43:29 pm by homeofdeath212 »
Currently Working On: Galaxiconia (A space shooter/defence game)
https://www.newgrounds.com/dump/item/2af922bd9a873856b9b2274737f80bc5

#### merrak

• Posts: 2526
Divide HP by 5000. The quotient is the number of stacks. The remainder is the HP left in the current stack. Divide the remainder by 50 to get percent of stack remaining.

The remainder block is in the math palette. To get the quotient, take floor of HP / 5000.

#### homeofdeath212

• Posts: 122
Divide HP by 5000. The quotient is the number of stacks. The remainder is the HP left in the current stack. Divide the remainder by 50 to get percent of stack remaining.

The remainder block is in the math palette. To get the quotient, take floor of HP / 5000.

Pardon me if you don't mind but can you elaborate more on this?
Also I've attached additional code in case it helps
Currently Working On: Galaxiconia (A space shooter/defence game)
https://www.newgrounds.com/dump/item/2af922bd9a873856b9b2274737f80bc5

#### merrak

• Posts: 2526
Pardon me if you don't mind but can you elaborate more on this?
Also I've attached additional code in case it helps

You can replace the code in HP_Bar_Update with the following two blocks:

Set Current HP in Stack to remainder of (For Boss get _HP) / Max HP Per Stack
Set HP Bar Stacks to floor of (For Boss get _HP) / Max HP Per Stack

Then you shouldn't need to keep track of HP lost unless you're using that statistic for something else

#### homeofdeath212

• Posts: 122

You can replace the code in HP_Bar_Update with the following two blocks:

Set Current HP in Stack to remainder of (For Boss get _HP) / Max HP Per Stack
Set HP Bar Stacks to floor of (For Boss get _HP) / Max HP Per Stack

Then you shouldn't need to keep track of HP lost unless you're using that statistic for something else

Amazing. I wish I knew all of this earlier. Thanks!

However I do have another problem now though. I can't seem to figure out now "when" exactly is a stack depleted. I wanted to track it so that I could be able to change the color of the hp bar whenever a stack is depleted.

Anyway to do this?
Currently Working On: Galaxiconia (A space shooter/defence game)
https://www.newgrounds.com/dump/item/2af922bd9a873856b9b2274737f80bc5

#### merrak

• Posts: 2526
However I do have another problem now though. I can't seem to figure out now "when" exactly is a stack depleted. I wanted to track it so that I could be able to change the color of the hp bar whenever a stack is depleted.

Anyway to do this?

Any time HP are depleted, do something like this:

1. Make an attribute called Old HP Bar Stacks and set it equal to floor of (For Boss get _HP) / Max HP Per Stack
2. Subtract damage from boss HP
3. Set HP Bar Stacks to floor of (For Boss get _HP) / Max HP Per Stack
4. If HP Bar Stacks < Old HP Bar Stacks then a stack was depleted.

#### homeofdeath212

• Posts: 122
Any time HP are depleted, do something like this:

1. Make an attribute called Old HP Bar Stacks and set it equal to floor of (For Boss get _HP) / Max HP Per Stack
2. Subtract damage from boss HP
3. Set HP Bar Stacks to floor of (For Boss get _HP) / Max HP Per Stack
4. If HP Bar Stacks < Old HP Bar Stacks then a stack was depleted.

I got it to work thanks to this. Thanks again!
Currently Working On: Galaxiconia (A space shooter/defence game)
https://www.newgrounds.com/dump/item/2af922bd9a873856b9b2274737f80bc5