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

2013/4/29

I don't understand how to properly use the counter class

1
2
3
Solringolt Solringolt

2013/5/1

#
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;
    private int highest;
    
    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);
    }
     
    public void setHighest()  
    {  
        if (value > highest) highest = value;  
    }
     public int getHighest()  
    {  
        return highest;  
    }
}
danpost danpost

2013/5/1

#
I do not see why it is not seeing the method when called. I do see something, however unrelated, that could be adjusted. Change line 132, to 'if (target> highest) highest = target;'. This is in case 'value' has not reached 'target' yet.
Solringolt Solringolt

2013/5/1

#
http://www.greenfoot.org/scenarios/8191 Maybe if you see the whole project? (I put in commentary what made not compilate to share it)
danpost danpost

2013/5/1

#
OK, I got it downloaded. You can remove it now if you wish.
Solringolt Solringolt

2013/5/1

#
???
danpost danpost

2013/5/1

#
Solringolt wrote...
???
What did not you understand? ------------------------------------- OK, this is what I ended up with:
if (value <= 0)  
{  
    Counter counter = (Counter)getWorld().getObjects(Counter.class).get(0); // gets the counter  
    counter.setHighest(); // adjusts current high, if beaten  
    counter.setValue(counter.getHighest()); // sets value to high score  
    Menu menu = new Menu();  
    menu.setCounter(counter);  
    PlayUniverse playUniverse = (PlayUniverse)getWorld();  
    Hero player = playUniverse.getplayer();  
    getWorld().removeObject(player);  
    Greenfoot.delay(80);  
    Greenfoot.setWorld(menu);  
}
The only problem I foresee now is that once a button on the menu is clicked on, you will be changing worlds again. However, the counter object will be lost unless you pass it (or its value) from world to world. Maybe what you should do, since you only have one counter object, is have the WorldOption class hold a 'static' reference to the counter so you would not have to pass it from world to world. You can, at any time, refer to it with 'WorldOption.counter'.
Solringolt Solringolt

2013/5/1

#
Ha ok thx it's finally a bit the same problem I had with my GreenfootSound and it's the only way I found to resolve it. Thx a lot for everything I learned much with your help :D
danpost danpost

2013/5/1

#
After relocating the counter reference to WorldOption as static, go to your PlayUniverse constructor, and change the code adding the counter to this:
if (counter == null) counter = new Counter("Score:");
counter.setValue(0);
addObject(counter, 130, 755);
Solringolt Solringolt

2013/5/1

#
Great everything's working! I will try to make some easier implements now haha
You need to login to post a reply.
1
2
3