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

2016/3/2

Stop Game Once Two Actors are Removed.

smcgee smcgee

2016/3/2

#
The scenario in question was originally the little-crab-5 scenario with edits. I have a shark that is able to eat two fish. I want the entire program to stop once fish1 and fish2 have been eaten by the shark. Right now the game continues even though only the shark is left swimming around.
danpost danpost

2016/3/3

#
In world act method, simply ask:
if (getObjects(fish1.class).isEmpty() && getObjects(fish2.class).isEmpty())
as the condition to stop the scenario (call 'Greenfoot.stop();').
smcgee smcgee

2016/3/3

#
public class Ocean extends World
{
    /**
     * Create the ocean. Our world has a size 
     * of 1000x600 cells, where every cell is just 1 pixel.
     */
    public void act()
    {
        if (getObjects(Fish.class).isEmpty() && getObjects(Fish2.class).isEmpty());
        {
            Greenfoot.stop();
        }
    }
I think I understood your recommendation. But when I place the code in the World class the program immediately stops right after it starts despite the fact that the fish are still in the World. I placed an act() method in the World class - was that your recommendation? And then added the suggested code.
danpost danpost

2016/3/3

#
smcgee wrote...
I placed an act() method in the World class - was that your recommendation? And then added the suggested code.
This should work -- unless you have code somewhere else that is causing the program to stop. One question, however, is whether you are adding the fish objects into the world through the constructor of the world class or -- from where?
smcgee smcgee

2016/3/3

#
from the Constructor of the world class.
smcgee smcgee

2016/3/3

#
No I'm sorry that was not correct. I add the object from the prepare() method.
smcgee smcgee

2016/3/3

#
I add all the objects from the prepare() method than call the prepare method in the World constructor. Hope that is clearer.
Super_Hippo Super_Hippo

2016/3/3

#
Remove the semicolon (;) from the end of line 9.
smcgee smcgee

2016/3/5

#
thank you - it works now
You need to login to post a reply.