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

2015/2/26

Score counter problem

e.vill e.vill

2015/2/26

#
Hello i am currently new to using greenfoot and i am having problems with the score counter. my game is a pop the balloon so the idea is when the dart clicks on the balloon to 'pop' it, the score value should increase. however i can't seem to make the counter work as the value always stays at 0. here's the code for my 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 Counter 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 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();
        }
    }

    /**
     * Add a new score to the current counter value.
     */
    public void add(int score)
    {
        if (Greenfoot.mouseClicked(this)) {
            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

2015/2/26

#
You can be quite sure (provided you did not make any changes to it) that the Counter class code is okay -- that the problem you are experiencing is not due to its code. Usually, when people have a problem similar to what this appears to be, the problem is in their World subclass. Post its code.
You need to login to post a reply.