Reputation: 2913
So basically i'm making healthbars for my enemies in my C# XNA game. I am getting the percentage of health left so e.g. 80/100 = 0.8 = 80% so my game knows how big to draw the green portion of the health bar. So if there is 80% hp it will draw the healthbar to 80%. The problem I am having is as soon as the health goes down from 100, the percentage automatically gets dropped to 0.0/0% and nothing is drawn. Here is my code:
//calculate the width of the green portion of healbar
//zDay.Game1.hp_top.Width = Original size of image (35px)
float size = (hp/max_hp) * zDay.Game1.hp_top.Width;
//draw percentage of health
sb.DrawString(zDay.Game1.CourierNew, (hp/max_hp).ToString(), new Vector2(50, 80), Color.Black);
//draw size of green portion
sb.DrawString(zDay.Game1.CourierNew, (size).ToString(), new Vector2(50, 50), Color.Black);
//draw green portion of health bar
Rectangle bg = new Rectangle(x - 20, y - 30, (int)size, zDay.Game1.hp_top.Height);
sb.Draw(zDay.Game1.hp_top, bg, Color.White);
Any ideas as to why this is happening?
Upvotes: 0
Views: 111
Reputation: 9382
I would guess that the issue isn't with multiplication, but division; the statement:
(hp/max_hp)
is most likely being evaluated to zero (since you're diving by int's (I assume) and the result will be between 0 and 1, or in Integer terms, 0).
Try using
((float)hp/(float)max_hp)
Instead.
Upvotes: 2