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

2021/3/27

Remove object from world after being shot

SanderNeele SanderNeele

2021/3/27

#
Hello, I want the animal that has been shot to get removed from the world. How and where do I need to put the code for this. Thanks!
danpost danpost

2021/3/27

#
In act method of either class (never both):
if (isTouching(Other.class))
{
    removeTouching(Other.class);
    getWorld().removeObject(this);
    return;
}
If anything more than the removal of the actors needs done upon collision, it may influence where you might want to put the code.
SanderNeele SanderNeele

2021/3/27

#
Thanks Dan, When the spear hits an animal (wolf) the spear disappears (see the code below), but now I want that the Wolf also gets removed from the world. So two actions at once.
public void raaktWolf()
    {
        if (isTouching (Wolf.class))
        {
            World world = getWorld();
            world.removeObject  (this);
        } 
    }
Super_Hippo Super_Hippo

2021/3/27

#
Check line 3 in the code he posted above.
danpost danpost

2021/3/27

#
SanderNeele wrote...
So two actions at once.
Yes:
danpost wrote...
if (isTouching(Other.class))
{
    removeTouching(Other.class);
    getWorld().removeObject(this);
    return;
}
... two actors at once.
You need to login to post a reply.