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

2016/3/1

Need Help about my Score counter

cgrent cgrent

2016/3/1

#
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;
        leafs = 1;
        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)
    {
        target += score;
        }

    /**
     * Set a new counter value.
     */
    public void setValue(int newValue)
    {
        target = newValue;
        value = newValue;
        updateImage();
    }
    
    /**
     * Return the current counter value.
     */
    public int getnewValue()
    {
        return value;
    }
    /**
     * 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);
    }
}
When ever I try to compile it says cannot find symbol- variable leafs what should I do?
thewisegamer thewisegamer

2016/3/1

#
you should change "private int target" to "private int leafs" (line 15)
cgrent cgrent

2016/3/1

#
Thanks
thewisegamer thewisegamer

2016/3/1

#
you are welcome
cgrent cgrent

2016/3/1

#
It won't add points after my actor eats the leaves and it just shows 1 and doesn't go up. What would be the solution to this problem.
thewisegamer thewisegamer

2016/3/1

#
Have you add: "Counter.add(1) on your public void get leaf?
cgrent cgrent

2016/3/1

#
When I put counter.add(1) in it says cannot find symbol- variable counter
thewisegamer thewisegamer

2016/3/1

#
Try this:
public counter Counter;
    public hunter4(counter pointCounter)
    {
        Counter = pointCounter;
    }
Change the "hunter4" to your main actor
cgrent cgrent

2016/3/1

#
I just did it and it just says invalid method declaration; return type required. By the way the main actor is a wombat if that helps any
Super_Hippo Super_Hippo

2016/3/1

#
In the original code above, you can remove lines 23 and 24. 'leafs' is never used and variable is initialized to 0 by default. How are you trying to add points? When eating a leaf, you will need something like this if you don't have a reference to the counter. Condition for this is that there is always one counter in the world.
((Counter) getWorld().getObjects(Counter.class).get(0)).add(1);
You need to login to post a reply.