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

2019/5/26

Cannot find variable 'counter'

AmyIsBadAtCoding AmyIsBadAtCoding

2019/5/26

#
At the moment I am trying to add a counter where the counter will go up when my character collects a collectible. I'm following a tutorial on www.csnewbs.com and I'm nearly finished, but when I get to step 3 and put 'counter' in every instance of 'plane' code in all my worlds relevant (I have multiple worlds-not sure if this is the issue) the game says it can't find variable 'counter'. I've checked my game and really can't see the error. Here is one of my world for example:
import greenfoot.*;

/**
 * Write a description of class Sky here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Sky extends World
{

    /**
     * Constructor for objects of class Sky.
     * 
     */
    public Sky()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1); 

        prepare();
    }

    /**
     * Prepare the world for the start of the program. That is: create the initial
     * objects and add them to the world.
     */
    private void prepare()
    {
        Counter counter3 = new Counter();
        addObject(counter3, 66, 33);
        Plane plane3 = new Plane(counter);
        addObject(plane3, 12, 227);
    }
}
danpost danpost

2019/5/26

#
It cannot find counter because you have nothing named counter. The Counter object created on line 30 was assigned to a variable named counter3.
AmyIsBadAtCoding AmyIsBadAtCoding

2019/5/26

#
K, new issue: for no reason the counter visual is glitched, and the counter works separate for each world. yayyyy…...
CreatorMoon CreatorMoon

2019/5/27

#
See, that's what I'm trying to do.
You need to login to post a reply.