I have been having this problem for the past few days and have been unable to find a solution. I am aware that it is caused due to the actor being called after it has been removed. I tried to create a boolean (called "removed") which would determine whether or not the Actor is present in the game, but it seemed to not work. Any help is greatly appreciated. Here is the 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:663)
at greenfoot.Actor.getOneIntersectingObject(Actor.java:912)
at Actor.hit(Actor.java:69)
at Actor.act(Actor.java:46)
at greenfoot.core.Simulation.actActor(Simulation.java:568)
at greenfoot.core.Simulation.runOneLoop(Simulation.java:526)
at greenfoot.core.Simulation.runContent(Simulation.java:215)
at greenfoot.core.Simulation.run(Simulation.java:205)
Here is the code of the actor:
public class Medium extends Spaceships
{
private int timeMedium = 800;
private int health = 2000;
private boolean removed = true;
public void act()
{
setLocation(getX() - 1, getY());
timeMedium--;
if(timeMedium == 0)
{
getWorld().removeObject(this);
}
if (health <= 0)
{
Boolean removed = false;
}
if (removed = true)
{
hit();
fire();
}
}
private int counter =0;
private void hit()
{
Actor bullet = getOneIntersectingObject(Bullet.class);
if (bullet != null)
{
health = health - 20;
getWorld().removeObject(bullet);
}
if(health <= 0)
{
getWorld().removeObject(this);
}
Actor bullet1 = getOneIntersectingObject(Bullet1.class);
if (bullet1 != null)
{
health = health - 20;
getWorld().removeObject(bullet1);
}
if(health <= 0)
{
getWorld().removeObject(this);
}
}
private void fire()
{
counter++;
if (counter % 10 == 0)
{
EBullet ebullet = new EBullet();
EBullet2 ebullet2 = new EBullet2();
getWorld().addObject(ebullet, getX() -110, getY() +69);
ebullet.setRotation(90);
getWorld().addObject(ebullet2, getX() -110, getY() -69);
ebullet2.setRotation(90);
}
}
}
