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

2015/1/16

Help With My 'Game Over' after HealthBar is Empty

Manda_x Manda_x

2015/1/16

#
When my Health Bar is empty I want my Game Over Actor for appear, how do I do this??
Super_Hippo Super_Hippo

2015/1/16

#
I would say something like that: - every time you lose health - check if health is completely gone - if yes, add Game Over actor
Manda_x Manda_x

2015/1/16

#
public class HealthBar extends Actor
{
    int health = 10;
    int healthBarWidth = 80;
    int healthBarHeight = 15;
    int pixelsPerHealthPoint = (int)healthBarWidth/health;
    /**
     * Act - do whatever the HealthBar wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public HealthBar()
    {
        update();
    }

    public void act() 
    {
        update();
    } 

    public void update()
    {
        setImage(new GreenfootImage(healthBarWidth + 2, healthBarHeight + 2));
        GreenfootImage myImage = getImage();
        myImage.setColor(Color.WHITE);
        myImage.drawRect(0, 0, healthBarWidth + 1, healthBarHeight + 1);
        myImage.setColor(Color.RED);
        myImage.fillRect(1, 1, health*pixelsPerHealthPoint, healthBarHeight);
    }

    public void loseHealth()
    {
        health--;
    }
}
I have a game over already but as an actor but I want to apply when I lose my health, how would I code what you've said into my health bar?
Super_Hippo Super_Hippo

2015/1/16

#
Take away the act method. Change the loseHealth to this:
public void loseHealth()
{
    health--;
    update();
    if (health<1) getWorld().addObject(new GameOver(),getWorld().getWidth()/2,getWorld().getHeight()/2);
}
Manda_x Manda_x

2015/1/16

#
thanks its works now, you've been a great help to my game.
You need to login to post a reply.