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

2016/10/16

Incrementing counter - going crazy!

jcw jcw

2016/10/16

#
Going crazy trying to figure out why my counter (the one imported from within Greenfoot) wont work: I have the following in MyWorld: Counter counter = new Counter();
1
2
3
4
public Counter getCounter()
   {  
       return counter;
   }
I have the following in my Player (to get a point when it eats something)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public void act()
    {
        Actor eat = getOneObjectAtOffset(0,0, Food.class);
         
        if (eat != null)
        {
            getWorld().removeObject(eat);
             
            //World world;
            //world = getWorld();
            //Counter counter = (Counter) (getWorld().getObjects(Counter.class).get(0));
            //counter.add(1);
             
            MyWorld world;
            world = (MyWorld)getWorld();
            Counter counter = world.getCounter();
            counter.add(1);
             
        }
Note, that the commented out code works. But why does it not work as it stands now?
Super_Hippo Super_Hippo

2016/10/16

#
Show the line with which you add the Counter to the world. Maybe you add a different one to the world than the one which you have a reference for.
jcw jcw

2016/10/17

#
I'm not sure what you mean, The code where I add the counter to the world was in the first part of my posting. Basically, the code that should work (but doesn't) was copied from a tutorial but will not change the score. The one that I show commented out from a different tutorial but does work. I have looked at about 5 examples of a similar (and very simple) operation and they all seem to mirror my none-working code.
Super_Hippo Super_Hippo

2016/10/17

#
The line creates a new Counter objects and save it as 'counter'. It is unclear if this line is outside methods or not. Also, this line does not add it to the world. If it is correctly, it should look like this:
1
2
3
4
5
//outside methods
Counter counter = new Counter();
 
//adding it to the world
addObject(counter, x-position, y-position);
jcw jcw

2016/10/17

#
Sorry, no I have the Counter counter = new Counter(); inside the MyWorld (my original post failed to include it in the comment block) and I also have the line to add object. Like I said, the lines of code I have commented out DO work, it's just that a similar exercise / tutorial I have uses the other code that doesn't work.
Super_Hippo Super_Hippo

2016/10/17

#
It's hard to guess what's wrong if you don't show the MyWorld's code.
danpost danpost

2016/10/17

#
jcw wrote...
Like I said, the lines of code I have commented out DO work, it's just that a similar exercise / tutorial I have uses the other code that doesn't work.
Open your MyWorld class editor and "find" all occurrences of "new Counter"; you should find one -- and ONLY one.
jcw jcw

2016/10/17

#
Solved it.... There were two occurrences of the counter being defined! The first (which I had in the MyWorld) is shown at the top of my original post. I added this myself. The second occurrence I think has appeared when I saved the world. It was in the prepare method...
1
2
Counter counter = new Counter();
addObject(counter,71,46);
The moment I removed it my code worked as intended. Thanks for your help guys, I know this is a stupid and basic issue, but I am just learning this from scratch.
You need to login to post a reply.