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

2013/12/8

Danpost's Health/Progress Bar Issue.

NotSoPro NotSoPro

2013/12/8

#
I am having the hardest time to subtract hp form the bar when a player is hit. Any code help would be amazing!
FlicknFlack FlicknFlack

2013/12/8

#
This is how i did it in my game
private void showLive() {
        GreenfootImage image = new GreenfootImage(getImage());
        if (ein==false) {
            sleben=(int)Leben; //sleben is the life at the game start and Leben is a double
            ein=true;
        }
        image.setColor(Color.BLACK);
        image.drawRect(0, 25, 25, 3); //"box" for the bar in black
        image.setColor(Color.RED);
        image.fillRect(1, 26, 24, 2); //fill the box with red
        image.setColor(Color.GREEN);
        minusHealth=24.0/sleben*(int)Leben; //thats the important part, my bar is 24 pixels long so you do 24 / the maxvalue * the current value 
        image.fillRect(1, 26, (int)minusHealth, 2); 
        setImage(image);
    }
FlicknFlack FlicknFlack

2013/12/8

#
so you just have to change Leben(life) to show how much health is left
danpost danpost

2013/12/8

#
Please show how and where you declare and create the bar, and then where and what the code around which you are trying to adjust the value of the bar. If you create the bar with
// instance field
Bar healthBar;
// created in constructor or while instance field is declared
healthBar = new Bar("Health", "", 100, 100); // similar
// then code to adjust value would be
healthBar.add(-damageAmount);
where 'damageAmount' is a field set to the correct value or is replaced with the literal numeric value.
NotSoPro NotSoPro

2013/12/8

#
Well what I was doing was declaring a new bar in world and using addObject. Then I was trying to create a reference in my player.class so when the player was hit it would subtract hp but I never got it to work.
danpost danpost

2013/12/8

#
So you should have something like this:
// in your world class constructor (or method it calls)
Bar bar = new Bar("Health", "", 100, 100);
addObject(bar, /* wherever */);
Player player = new Player(bar);
// instance field in Player class
Bar healthbar;
// Player class constructor
public Player(Bar bar)
{
    healthbar = bar;
    /* any other code */
}
// in act method or method it calls when damage taken
healthbar.add(-damageAmount);
NotSoPro NotSoPro

2013/12/8

#
OMG you are the best! thank you sooooooooo much!
You need to login to post a reply.