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

2015/5/4

Counter Problem

Tommy99 Tommy99

2015/5/4

#
Hello. I am having a problem with my counter. I imported the Greenfoot Counter class (made by Neil Brown and Michael Kölling), but when I try to give one of my counters a starting value, there's always a zero added on to it (5 becomes 50, etc). Here's the Counter class:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;

/**
 * A Counter class that allows you to display a numerical value on screen.
 * 
 * The Counter is an actor, so you will need to create it, and then add it to
 * the world in Greenfoot.  If you keep a reference to the Counter then you
 * can adjust its value.  Here's an example of a world class that
 * displays a counter with the number of act cycles that have occurred:
 * 
 * <pre>
 * class CountingWorld
 * {
 *     private Counter actCounter;
 *     
 *     public CountingWorld()
 *     {
 *         super(600, 400, 1);
 *         actCounter = new Counter("Act Cycles: ");
 *         addObject(actCounter, 100, 100);
 *     }
 *     
 *     public void act()
 *     {
 *         actCounter.setValue(actCounter.getValue() + 1);
 *     }
 * }
 * </pre>
 * 
 * @author Neil Brown and Michael Kölling 
 * @version 1.0
 */
public class Counter extends Actor
{
    private static final Color transparent = new Color(0,0,0,0);
    private GreenfootImage background;
    private int value;
    private int target;
    private String prefix;
    
    public Counter()
    {
        this(new String());
    }

    /**
     * Create a new counter, initialised to 0.
     */
    public Counter(String prefix)
    {
        background = getImage();  // get image from class
        value = 0;
        target = 0;
        this.prefix = prefix;
        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.  This will animate
     * the counter over consecutive frames until it reaches the new value.
     */
    public void add(int score)
    {
        target += score;
    }

    /**
     * Return the current counter value.
     */
    public int getValue()
    {
        return target;
    }

    /**
     * Set a new counter value.  This will not animate the counter.
     */
    public void setValue(int newValue)
    {
        target = newValue;
        value = newValue;
        updateImage();
    }
    
    /**
     * Sets a text prefix that should be displayed before
     * the counter value (e.g. "Score: ").
     */
    public void setPrefix(String prefix)
    {
        this.prefix = prefix;
        updateImage();
    }

    /**
     * Update the image on screen to show the current value.
     */
    private void updateImage()
    {
        GreenfootImage image = new GreenfootImage(background);
        GreenfootImage text = new GreenfootImage(prefix + value, 22, Color.BLACK, transparent);
        
        if (text.getWidth() > image.getWidth() - 20)
        {
            image.scale(text.getWidth() + 20, image.getHeight());
        }
        
        image.drawImage(text, (image.getWidth()-text.getWidth())/2, 
                        (image.getHeight()-text.getHeight())/2);
        setImage(image);
    }
}
Here's my Space (World) class (where I create the counters):
import greenfoot.*;  // (World, Actor, GreenfootImage, and Greenfoot)
import java.awt.Color;

/**
 * Space. Something for rockets to fly in.
 * 
 * @author Michael Kolling
 * @version 1.0
 */
public class Space extends World
{
    private Counter scoreCounter1;
    private Counter scoreCounter2;
    private Counter scoreCounter3;
    private Counter scoreCounter4;
    private int startAsteroids = 3;

    //Space station goes across screen.

    public Space() 
    {
        super(600, 400, 1);
        GreenfootImage background = getBackground();
        background.setColor(Color.BLACK);
        background.fill();

        Rocket rocket = new Rocket();
        addObject(rocket, getWidth()/2 + 100, getHeight()/2);

        addAsteroids(startAsteroids);

        scoreCounter1 = new Counter("Score: 0");
        addObject(scoreCounter1, 60, 380);

        scoreCounter2 = new Counter("Fuel: 500");
        addObject(scoreCounter2, 60, 20);

        scoreCounter3 = new Counter("Bullets: 100");
        addObject(scoreCounter3, 540, 20);

        scoreCounter4 = new Counter("Health: 5");
        addObject(scoreCounter4, 540, 380);

        Explosion.initializeImages();
        ProtonWave.initializeImages();
    }

    
         
         

    public void act()
    {
        addSpaceStations();
        scoreCounter1.setValue(scoreCounter1.getValue() + 1);

    }

    public void addSpaceStations()
    {
        if(Greenfoot.getRandomNumber(200)==1){
            addObject(new SpaceStation(),getWidth()/2, getHeight()/2);
        }
    }

    /**
     * Add a given number of asteroids to our world. Asteroids are only added into
     * the left half of the world.
     */
    private void addAsteroids(int count) 
    {
        for(int i = 0; i < count; i++) 
        {
            int x = Greenfoot.getRandomNumber(getWidth()/2);
            int y = Greenfoot.getRandomNumber(getHeight()/2);
            addObject(new Asteroid(), x, y);
        }
    }

    /**
     * This method is called when the game is over to display the final score.
     */
    public void gameOver() 
    {
        // TODO: show the score board here. Currently missing.
    }

}
Thank you for your help.
azazeel azazeel

2015/5/4

#
where is the code to add the score ? my suggestion first change without 0 scoreCounter1 = new Counter("Score: ");
danpost danpost

2015/5/4

#
azazeel is correct in that there should be no numeric values in the String passed to the constructor of the Counter class. After creating the counters, use the 'setValue' method of the Counter class to set their initial values. For example:
scoreCounter1 = new Counter("Score: ");
scoreCounter2 = new Counter("Fuel: ");
scoreCounter2.setValue(500);
scoreCounter3 = new Counter("Bullets: ");
scoreCounter3.setValue(100);
scoreCounter4 = new Counter("Health: ");
scoreCounter4.setValue(5);
They still need to be added into the world (I did not include those lines).
Tommy99 Tommy99

2015/5/4

#
Oh okay. Thanks to both of you for the help. It works well now.
You need to login to post a reply.