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

2021/12/8

counter not working

vbh vbh

2021/12/8

#
Hi the plan was that when the girl have watered all 15 flowers, the game should end and a GameEnded sign should appere, but its happening before, its like the counter counts more than one. here is my code hope someone can see whats wrong public class Flower extends Animal { private GreenfootImage smallFlower; private GreenfootImage bigFlower; private int counter; public Flower() { smallFlower = new GreenfootImage("lilleBlomst.png"); bigFlower = new GreenfootImage("storBlomst.png"); setImage(smallFlower); counter = 0; } public void act() { grow(); } public void grow() { if(canSee(Girl.class)) { setImage(bigFlower); counter++; if(counter==15) { World world= getWorld(); GameEnded ended = new GameEnded(); world.addObject(ended, world.getWidth()/2,world.getHeight()/2); Greenfoot.stop(); } } } } thanks
danpost danpost

2021/12/8

#
Need another condition for growing:
if (canSee(Girl.class) && getImage() != bigFLower)
However, then the value of each flower's counter will all be 1 when all have been watered. The counter either needs to be in a different class (either in Girl or in World subclass) or the counter needs to be a class field (static) that is initialized in the World subclass constructor
vbh vbh

2021/12/9

#
thanks for Quick replay, i see what you mean, i have tried to add the counter to girl class , thats not working, im not sure how to write the code if i add the counter into world sub class and how to access the counter any ideas
vbh vbh

2021/12/9

#
my code in girl class public void counterUp() { if(canSee(Flower.class)) { counter++; if(counter == 15) { World world= getWorld(); GameEnded ended = new GameEnded(); world.addObject(ended, world.getWidth()/2,world.getHeight()/2); Greenfoot.stop(); } } }
danpost danpost

2021/12/9

#
Use:
if (isTouching(Flower.class) && ((Flower)getOneIntersectingObject(Flower.class)).grows())
Add the following in Flower class:
public boolean grows()
{
    if (getImage() != bigFlower)
    {
        setImage(bigFlower);
        return true;
    }
    return false;
}
Remove any other code to grow flower in Flower class (that is, entire act and grow methods).
vbh vbh

2021/12/9

#
i have solved the problem :-)
You need to login to post a reply.