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

2018/12/6

i am trying to add live into my spaceinvaders

zach0531 zach0531

2018/12/6

#
when my health bar hits zero i want the health bar to reset and life counter to subtract. my life counter
    }
     
    /**
     * Animate the display to count up (or down) to the current target value.
     */
    public void act() 
    {
        if (value > target) {
            value--;
            updateImage();
        }
        else if (value < target) {
            value++;
            updateImage();
        }
    }
 
    /**
     * Add a new score to the current counter value.
     */
    public void add(int score)
    {
        target -= score;
    }
 
    /**
     * Return the current counter value.
     */
    public int getValue()
    {
        return value;
    }
 
    /**
     * Set a new counter value.
     */
    public void setValue(int newValue)
    {
        target = newValue;
        value = newValue;
        updateImage();
    }
 
    /**
     * Update the image on screen to show the current value.
     */
    private void updateImage()
    {
        GreenfootImage image = new GreenfootImage(background);
        GreenfootImage text = new GreenfootImage("" + value, 22, Color.BLACK, transparent);
        image.drawImage(text, (image.getWidth()-text.getWidth())/2, 
                        (image.getHeight()-text.getHeight())/2);
        setImage(image);
    }
   
}
danpost danpost

2018/12/6

#
zach0531 wrote...
I am trying to add lives in my space invaders game. when the spaceships health bar hits zero I want a life to be taken away and the health bar to reset. this is my health bar code. << Code Omitted >>
The following should work for resetting the health bar:
HealthBar healthbar = /** get reference to HealthBar object */;
healthbar.setValue(/** max value of health bar */);
And similarly for the counter:
Counter lifeCounter = /** get reference to Counter object */;
counter.add(-1);
You need to login to post a reply.