Why does my health bar disappear whenever my character takes amage?

Posted by iQue on Game Development See other posts from Game Development or by iQue
Published on 2012-09-05T14:01:56Z Indexed on 2012/09/05 15:52 UTC
Read the original article Hit count: 276

Filed under:
|
|

Im making health bar for my game that looks like this:

  public void healthBar(Canvas canvas)
{
    float healthScale = happy.getHP() / happy.getMaxHP();
    Rect rect = new Rect(20, 20,(120 * (int)healthScale), 40);
    Paint paint = new Paint();
    paint.setColor(Color.RED);
    canvas.drawRect(20, 20, 220 * healthScale, 40, paint)
}

this is called every time my game renders. When the game starts it's where I want it, but as soon as my character (happy) takes any damage, it dissapears. And I know that his hp only gets subtracted by 5 every time he gets hit. So this should not happen?

example:

    @Startup: happy.getHP() == 100, happy.getMaxHP == 100. 
    when damaged HP -=5, -> happy.getHP() == 95 -> healthscale == 0,95 -> 220 * 0,95 == new width for Rect(?)

© Game Development or respective owner

Related posts about android

Related posts about graphics