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

2019/5/27

Someone has already asked this, but my counter won't work across multiple worlds.

AmyIsBadAtCoding AmyIsBadAtCoding

2019/5/27

#
Trying to make a counter that count how many balls you pick up, but it resets to 0 when I change world. How do I go about fixing this? Here is the counter code, tell me if you need any more code:
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);
    }
}
Super_Hippo Super_Hippo

2019/5/27

#
What did you try from the solutions I suggested in the other discussion?
AmyIsBadAtCoding AmyIsBadAtCoding

2019/5/27

#
I didn't understand
danpost danpost

2019/5/27

#
Follow these steps: * create new world * set score in new world to that of current world * set new world active
AmyIsBadAtCoding AmyIsBadAtCoding

2019/5/27

#
how do I do the second one I made a new world but how am I meant to make score equal, there isn't any counter code in the new world is my counter code strange?
nccb nccb

2019/5/27

#
AmyIsBadAtCoding -- while the Greenfoot forum is intended for help with Greenfoot, and it's great that everyone is being so helpful, be mindful that you are asking for a lot of time from all the people here who are helping you (in their own time), and that you are posting a lot of new topics which can crowd out other people's questions. See if it makes sense to continue threads rather than posting a new one each time.
AmyIsBadAtCoding AmyIsBadAtCoding

2019/5/27

#
ok ill go back and use the other thread
You need to login to post a reply.