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

2017/3/17

Help with counter

W.Godfrey W.Godfrey

2017/3/17

#
import greenfoot.*;
public class mouse extends Actor
{
    private Counter counter;
    /**
     * Act - do whatever the mouse wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public mouse(Counter pointCounter)
    {
        counter = pointCounter;
    }
    public void act() 
    {
        moveAbout();
        eat();
    }    
    private void moveAbout()
    {
        if (Greenfoot.isKeyDown("right"))move(3);
        turn(90);
        if(Greenfoot.isKeyDown("down"))move(3);
        turn(90);
        if (Greenfoot.isKeyDown("left"))move(3);
        turn(90);
        if(Greenfoot.isKeyDown("up"))move(3);
        turn(90);
    }
    private void eat()
    {
        Actor bread;
        bread = getOneObjectAtOffset(0,0, bread.class);
        if (bread != null);
            World world;
            world = getWorld();
            world.removeObject(bread);
            counter.add(1);
            }
   }
The issue with this is that rather than adding to the counter when bread is eaten, the counter is constantly changed by 1 when run is pressed.
Fifilein Fifilein

2017/3/17

#
You can try to make a counterclass.
danpost danpost

2017/3/17

#
W.Godfrey wrote...
< Code Omitted > The issue with this is that rather than adding to the counter when bread is eaten, the counter is constantly changed by 1 when run is pressed.
The problem code is most likely located in your World subclass.
Fifilein Fifilein

2017/3/17

#
You need to Show your Worldclass Code here
Nosson1459 Nosson1459

2017/3/17

#
your if statement on line 33 isn't doing anything, you need to remove the semi-colon after the if statement and enclose lines 34 through 37 in curly brackets { }.
if (bread != null)
{
            World world = getWorld();
            world.removeObject(bread);
            counter.add(1);
}
danpost danpost

2017/3/17

#
Nosson1459 wrote...
your if statement on line 33 isn't doing anything, you need to remove the semi-colon after the if statement and enclose lines 34 through 37 in curly brackets { }. < Code Omitted >
Nice catch. That still does not explain why:
W.Godfrey wrote...
the counter is constantly changed by 1 when run is pressed.
That is why:
danpost wrote...
The problem code is most likely located in your World subclass.
and:
Fifilein wrote...
You need to Show your Worldclass Code here
Super_Hippo Super_Hippo

2017/3/17

#
Actually, because of the semicolon, line 37 adds one to the counter every act cycle (if a mouse object exist which I guess is the case).
danpost danpost

2017/3/17

#
Super_Hippo wrote...
Actually, because of the semicolon, line 37 adds one to the counter every act cycle (if a mouse object exist which I guess is the case).
Right again. I guess I am not thinking things through like I should (not like I used to -- hope my mind is not going!).
Nosson1459 Nosson1459

2017/3/19

#
Super_Hippo wrote...
(if a mouse object exist which I guess is the case).
Line 37 will add one to the counter with the above code. With my edit it will add one when Bread isn't null meaning there is an actor of the bread class touching the middle of mouse. I don't understand what you mean by "if a mouse object exist(s)"?
Super_Hippo Super_Hippo

2017/3/19

#
It referred to the original code of the mouse class where the statement was in the act method without a condition. So the only condition that this code is executed is that a mouse object is in the active world so its act method is called.
Nosson1459 Nosson1459

2017/3/19

#
OK, if you say so.
William_Godfrey William_Godfrey

2017/3/24

#
Thanks all around! It is now working. Sorry that I wasn't able to reply sooner, I only have Computing nearer the end of the week.
You need to login to post a reply.