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

2017/3/14

Remove an actor after another is added

Fifilein Fifilein

2017/3/14

#
Hi! I have a question, is there a method for remove an actor when another is added? In my Game there is a Frog a Snake and Flies.When the frog collects all Flies a YouWinscreen appears. You lose the game when the frog hits the snake. I want the snake to remove or stop moving, when the YouWinscreen appears. I tried getOneIntersectingObject but it does not work. I have no idea how to do it another way. Can you help me? Thanks, Fifilein
Super_Hippo Super_Hippo

2017/3/14

#
Should the whole game stop or just the snake?
Fifilein Fifilein

2017/3/14

#
just the snake
Super_Hippo Super_Hippo

2017/3/14

#
To remove it:
getWorld().removeObject(getWorld().getObjects(Snake.class).get(0));
If you want it to stop moving:
private boolean stop = false;

public void act()
{
    if (stop) return;
    //rest of act method
}

public void stop()
{
    stop = true;
}
((Snake)getWorld().getObjects(Snake.class).get(0)).stop();
You can also keep a reference to the Snake in your world. Then this looks a bit less complicated.
Fifilein Fifilein

2017/3/14

#
Where need I set it?
Super_Hippo Super_Hippo

2017/3/14

#
When you want it to happen. If you want to place it in the world, you have to remove all 'getWorld'. The second code snippet should be placed in Snake class.
Fifilein Fifilein

2017/3/14

#
Thank you now it works!
You need to login to post a reply.