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

2019/1/16

Problem with the removing of something

Tommes Tommes

2019/1/16

#
    public void act() 
    {
        move();
        tryToEatAlien1();
        raketetod();
     } 
    }
    public void tryToEatAlien1() {
      if (canSee(Alien1.class)) {
        GroundWorld welt = (GroundWorld)getWorld();
        welt.gibZaehler().add(1);
        removeTouching(Alien1.class);
        getWorld().removeObject(this);
      }
   } 
    public void raketetod() {
      if (isAtEdge() ) {
        getWorld().removeObject(this);
      } 
     }
}
If I start this code and try to kill an alien greenfoot is giving me this error: java.lang.IllegalStateException: Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed. at greenfoot.Actor.failIfNotInWorld(Actor.java:714) at greenfoot.Actor.isAtEdge(Actor.java:262) at Rakete.raketetod(Rakete.java:58) at Rakete.act(Rakete.java:24) I would love if someone could help me :)
danpost danpost

2019/1/16

#
Change line 17 to this:
if (getWorld() != null && isAtEdge()) {
This will ensure that the actor is in a world before trying to check for world edges.
Tommes Tommes

2019/1/16

#
Thank you, you are my hero!
danpost danpost

2019/1/16

#
Tommes wrote...
Thank you, you are my hero!
And you are my 500th fan (follower). Thank you.
You need to login to post a reply.