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

2012/5/7

Actor is not in world??

Blackmac Blackmac

2012/5/7

#
After i remove an object, greenfoot stops an give me the error : IllegalStatException: Actor is not in world. An attempt.... my code of this class: public void act() { zerstören(); move(); } public void zerstören() { if (getOneIntersectingObject(Schuss.class)!= null) {getWorld().removeObject(this);} if ( getX() <= 0 ) {getWorld().removeObject(this);} } public void move() { setLocation(getX() - 5, getY()); } }
danpost danpost

2012/5/7

#
Change 'if ( getX() <= 0 )' to 'if ( getWorld() != null && getX() <=0)'.
Blackmac Blackmac

2012/5/7

#
But the error also appears if I shoot the Alien. But I will try your idea
trash1000 trash1000

2012/5/7

#
You can only move an actor when it's in the world. Otherwise you will get that runtime error you posted. In your class you remove the object when an object from Schuss.class is intersecting it. The problem is Greenfoot won't stop executing the act method after the object is deleted - so the call of move() causes the error. Simple solution:
public void act() { 
move();
zerstören(); 
}
Just swap the method calls so you first move the object and then eventually delete it.
Blackmac Blackmac

2012/5/7

#
Thanks a lot!!! very stupid mistake...
You need to login to post a reply.