Struggling with a math formula

Just started using Construct 2? Post your questions here

Post » Sun Apr 20, 2014 1:55 am

Okay I have been thinking really hard about this but it's making my brain explode and it is driving me nuts. I want to have a boss with a megaman health bar. the bar is a sprite with 29 frames, frame 0 is an empty bar so a full bar is frame 28 with 28 "mini bars" in it. I want the boss to be allowed to have any amount of maximum health I ever choose at any given time (it will be a set number chosen by me for each boss, but perhaps different for different bosses, however the max health of a boss won't change during the game), hundreds of points even, possibly thousands, and I want each "mini bar" to represent 1/28 of the boss' health, rounded up to the nearest possibility (so if it has only 1 or 2 health out of a gazillion, it will show frame 1). What formula would I need to set the bar's frame number? I have been thinking about this round and round and I just can't come up with it. I am sure it's simple once thought of, so could somebody please help me? Most Megaman games seem to do one hit point per mini bar, or out of always 100 health, so I don't think many of those examples out there would help, even if they are made in MMF or Game Maker (if they did what I was describing then they would indeed give me the answer).

edit: I figured it out. It's ceil(currenthealth/maxhealth*28).
I actually had explored this before, but it was with the width of a sprite, rather than the frame number of an animation, so I kinda got confused. sorry if this topic caused any trouble! :)
B
15
S
2
Posts: 103
Reputation: 1,178

Post » Sun Apr 20, 2014 2:19 am

deleted
Last edited by shirokuma on Sun Apr 20, 2014 1:14 pm, edited 1 time in total.
B
17
S
4
G
3
Posts: 172
Reputation: 1,933

Post » Sun Apr 20, 2014 2:20 am

So the frame and health ratios are the same so:
Health/maxHealth = frame/28

Solving for frame:
Frame = Health*28/maxHealth

Next we need to round it to a whole number, and since we want frame 0 to be used only if the health is 0 we will use ceil() to round up.

So the formula is:
Frame = ceil(Health*28/maxHealth)
B
94
S
33
G
114
Posts: 5,359
Reputation: 73,779

Post » Sun Apr 20, 2014 2:24 am

R0J0hound wrote:So the frame and health ratios are the same so:
Health/maxHealth = frame/28

Solving for frame:
Frame = Health*28/maxHealth

Next we need to round it to a whole number, and since we want frame 0 to be used only if the health is 0 we will use ceil() to round up.

So the formula is:
Frame = ceil(Health*28/maxHealth)


I figured it out on my own, but thanks anyway! It seems like the majority of the people here are nice! That's another newbie problem solved! Wish me luck as I move along with my game, I might post more questions along the way! :)
B
15
S
2
Posts: 103
Reputation: 1,178

Post » Thu May 08, 2014 10:22 am

yes we are here to help if we can
B
256
S
39
G
15
Posts: 1,984
Reputation: 41,038


Return to Beginner's Questions

Who is online

Users browsing this forum: No registered users and 1 guest