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

2016/5/10

Can't call worm class

xlRenn xlRenn

2016/5/10

#
Hello, So currently, I'm trying to link my score counter to my worms instead of crabs. I have this piece of code
public Worm(Counter pointCounter)
    {
        counter = pointCounter;
        
    }
and I assumed to call it in my world, I would just do
Worm worm = new Worm(counter);
        addObject(worm, Greenfoot.getRandomNumber(460)+50, Greenfoot.getRandomNumber(460)+50);
However, I'm getting an error message: "cannot find symbol - variable counter". Why is that? I'm really confused because a similar scenario works perfectly fine with my lobster:
public Lobster(Lives pointCounter)
    {
        lives = pointCounter;
        
    }
and calling it in myWorld with
Lobster lobster = new Lobster(lives);
        addObject(lobster, 400, 400);
and no error message pops up. I think I'm not understanding something, but I'm not quite sure what. Any help would be appreciated. Thanks!
xlRenn xlRenn

2016/5/10

#
I forgot to add, even when I previously had it linked to crabs, no error message came up. I must be missing something that I had put in crab, but I can't find it :(
danpost danpost

2016/5/10

#
Look above the constructor in the Crab class (or near the top of the class). You will probably find a line that declares the counter field. It will look something like this:
private int counter;
This is what gives the actor the counter field.
xlRenn xlRenn

2016/5/10

#
Hi, Yes, I had that in my crab class, but I did copy over it to my worm already:
private Counter counter;
Honestly can't find anything different :/
danpost danpost

2016/5/11

#
xlRenn wrote...
Honestly can't find anything different :/
Maybe the difference is in the number of that type object you are creating. You were only creating only one lobster, but are creating multiple worms. Unless you are giving the same counter to each and every worm created or given the field to the class itself instead of each worm (or somewhere else where only one is referenced at all times), you will end up having problems.
You need to login to post a reply.