This site requires JavaScript, please enable it in your browser!
Greenfoot back
Arbitrage
Arbitrage wrote ...

2014/12/15

Double Health Bars (as in int double)

Arbitrage Arbitrage

2014/12/15

#
Hi all, I'm running into a problem with the health bar, currently I am drawing it through the draw Rect method, and I am currently using health/100 (which is my max health) x value to draw it, however since it's a int the diving has some problems, is there anyway to make it so that the healthbar doesn't drop in chunks?
public void drawHealth(){
        //gets player info
        int myHealth = ((MyTank) getWorld().getObjects(MyTank.class).get(0)).getHealth();

        //draws bar
        buffer++;
        if(buffer % 2 == 0){
            GreenfootImage image = new GreenfootImage(601, 68);
            image.setColor(Color.BLACK);
            image.fillRect(72, 42, 601, 60); 
            image.setColor(new Color(128, 255, 0));
            image.fillRect(72, 42, myHealth/100*39, 59);  
        }
    }
I've tried casting it to double then back to int, but it still doesn't work, any help will be greatly appreciated.
danpost danpost

2014/12/15

#
When working with int values and a division operation is involved, make sure to do it last.
image.fillRect(72, 42, myHealth*39/100, 59);
You need to login to post a reply.