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

2013/1/12

how to make a life counter

ctgreenfoot ctgreenfoot

2013/1/12

#
hi, so i am working on the little crab scenario and i want to give the crab five lives. one life is taken away each time the crab is eaten by a lobster. is this the correct code for the life counter?
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;

/**
 * A simple counter with graphical representation as an actor on screen.
 * 
 * @author mik
 * @version 1.0
 */
public class LifeCounter extends Actor
{
    private static final Color transparent = new Color(0,0,0,0);
    private GreenfootImage background;
    private int value;
    private int target;

    /**
     * Create a new counter, initialised to 0.
     */
    public LifeCounter()
    {
        background = getImage();  // get image from class
        value = 5;
        target = 5;
        updateImage();
    }
    
    /**
     * 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);
    }
  
}
if no, then what do i need to change? if yes, then how do i get it to work? thanks
danpost danpost

2013/1/12

#
This class would work fine; except for the fact that the 'add' method is really subtracting. It would make more sense to write it as follows:
public void add(int score)  
{
    target += score;
}
// you could add this message to the class
public void subtract(int score)
{
    target -= score;
}
ctgreenfoot ctgreenfoot

2013/1/12

#
thanks i changed that! but i just need to know how to link it so that the crab starts out with five lives and then loses one each time the lobster eats it. how do i implement that and where?
danpost danpost

2013/1/12

#
The class is already set up to start your counter with 5 lives, all you have to do is create a counter object. To link it to your actor, you will need an instance Counter field in that actor's class. You can create and assign an object to the field in the field declaration statement and add a method to retrieve the counter object from the class:
// instance field to add
Counter lifeCounter = new Counter();
// methods to add
public Counter getLifeCounter{)
{
    return lifeCounter;
}
Then in the world constructor, when adding your objects to the world:
Crab crab = new Crab();
addObject(crab, x1, y1); // location of choice
addObject(crab.getLifeCounter(), x2, y2); // location of choice
Now, both the crab and the lifeCounter are in the world and the crab has a reference to the lifeCounter object to work with. Anytime the Lobster object 'eats' the crab, you can use 'lifeCounter.subtract(1);' (or 'lifeCounter.add(-1);') to adjust the life counter. It might be best to check the lifeCounter's value first using:
if (lifeCounter.getValue() == 1)
{
    lifeCounter.setValue(0);
    getWorld().removeObject(this);
    Greenfoot.stop();
    return;
}
lifeCounter.subtract(1);
// set random location for crab
I only suggest coding it this way because of the way the Counter object operates (updating 'target' immediately and not updating 'value' until act cycle(s) later).
You need to login to post a reply.