Hey all, have a problem that's driving me nuts. When I try to remove a bullet when it hits the worlds edge, I get the following 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.getX(Actor.java:157)
at EnemyBullet.hitEdges(EnemyBullet.java:26)
at EnemyBullet.act(EnemyBullet.java:11)
My bullet code is as follows -
I've read the solutions already posted but nothing seems to work. What have I missed?
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
public class EnemyBullet extends Mover
{
//int counter = 0;
public void act()
{
move(1.0);
hitPlayer();
hitEdges();
if(getWorld() == null) return;
}
private void hitPlayer()
{
Actor player = getOneIntersectingObject(Turret.class);
if (player !=null)
{
getWorld().removeObject(player);
getWorld().removeObject(this);
return;
}
}
private void hitEdges()
{
if((this.getX() < 10 || this.getX() > this.getWorld().getWidth() - 10) && (this != null))
{
this.getWorld().removeObject(this);
return;
}
if((getY() < 10 || getY() > getWorld().getWidth() - 10) && (this != null))
{
getWorld().removeObject(this);
return;
}
}
}

