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

2013/5/29

Is there a way to make the Counter count more quickly in my game?

AreegeJendi AreegeJendi

2013/5/29

#
My counter is counting points too slowly. I wanted the total/winning score to be 1000, and there are 20 coins to be collected (50 points each), but by the time the last coin is collected it is around 950 points and counting up, so there is a delay. How can I make the counter count more quickly? Here is the current code for my counter:
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;

    /**
     * Creates a new counter, initialised to 0.
     */
    public Counter()
    {
        background = getImage();  // get image from class
        value = 0;
        target = 0;
        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();
        }
    }

    /**
     * Adds 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

2013/5/29

#
If all changes in the score are multiples of ten, then you could change the 'act' method to the following:
public void act() 
{
    if (value < target) {
        value+=10;
        updateImage();
    }
    else if (value > target) {
        value-=10;
        updateImage();
    }
}
You need to login to post a reply.