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

2018/6/26

Cannot find symbol error

luc123 luc123

2018/6/26

#
I am trying to make a snake game for school and now i am trying to add a counter into my game, but i followed the steps on how to make that counter however i got this error. it gives an error by the: SnakeWorld.getCounter.
/**
 * Write a description of class SnakeHead here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class SnakeHead extends Animal
{
    public int count;
    public int countTurn;
    public int numberOfDiamonds  = 0;
    public int speed = 15;
    public int score;
    public void act() 
    {
        count += 1;
        countTurn += 1;
        if (count > 15){
            move();
            count = 0;
        }
       
        if (countTurn > 10){
            if (Greenfoot.isKeyDown("right")){
                turn(90);
                countTurn = 0;
            }
        }
      
        if (countTurn > 10){
            if (Greenfoot.isKeyDown("left")){
                turn(270);
                countTurn = 0;
            }
        }
      
        if (isTouching(Wall.class)){
            Greenfoot.stop();
        }
        
        if (isTouching(SnakeTail.class)){
            Greenfoot.stop();
        }
       
        if(isTouching(Diamond.class)){
            removeTouching(Diamond.class);
            numberOfDiamonds += 1;
            getWorld().addObject (new Diamond(), Greenfoot.getRandomNumber(27)+3, Greenfoot.getRandomNumber(27)+3);
            SnakeWorld SnakeWorld = (SnakeWorld) getWorld();
            Counter counter = SnakeWorld.getCounter;
            counter.add(10);
        }
        
        if(count >= speed){
            getWorld().addObject (new SnakeTail(numberOfDiamonds*6), getX(), getY());
        }
        
        if(numberOfDiamonds < 1){
            count += 1;
        }else if (numberOfDiamonds < 3){
            count += 2;
        }else if (numberOfDiamonds < 5){
            count += 4;
        }else if (numberOfDiamonds < 7){
            count += 4;
        }else if (numberOfDiamonds >= 7){
            count += 4.5;
        }
    }
    
    
}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * 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);
    }
}
luc123 luc123

2018/6/26

#
the error is at line 50 of the first code
danpost danpost

2018/6/26

#
luc123 wrote...
the error is at line 50 of the first code
If getCounter is a method, you must indicate so by placing a pair of parenthesis after it (in line 50).
luc123 luc123

2018/6/26

#
Thanks you so much
You need to login to post a reply.