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

2021/11/15

Simpel Question About getWorld().removeObject(this);

Monk_08 Monk_08

2021/11/15

#
I have a game where rockets fly in the direction of the player if he shoots them they should disappear but I get an error every time the bullet hits the rocket
    public void die()
    {
        if(isTouching(Bullet.class))
        {
            getWorld().removeObject(this);
        }
    }
Monk_08 Monk_08

2021/11/15

#
My Error is greenfoot.Actor.failIfNotInWorld(Actor.java:722) at greenfoot.Actor.isTouching(Actor.java:987)
danpost danpost

2021/11/15

#
Monk_08 wrote...
My Error is greenfoot.Actor.failIfNotInWorld(Actor.java:722) at greenfoot.Actor.isTouching(Actor.java:987)
Show the entire class code please.
Monk_08 Monk_08

2021/11/15

#
Sure
public class Rocket extends Animal
{
    /**
     * Act - do whatever the Rocket wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        move(7);
        die();
        kill();
    }
    
    public void die()
    {
        if(isTouching(Bullet.class))
        {
            getWorld().removeObject(this);
            return;
        }
    }
    public void kill()
    {
        if(isTouching(Robot.class))
        {
            getWorld().removeObject(this);
            //Greenfoot.stop();
            return;
        }
    }
}
danpost danpost

2021/11/15

#
How can you kill if you already died? That is, if a bullet remove this actor, it is no longer in the world to touch a robot. Change line 11 to:
if (getWorld() != null) kill();
Monk_08 Monk_08

2021/11/16

#
Ohh your Right Thanks ;)
You need to login to post a reply.