My game is called FirstProject and is published on this website link here: http://www.greenfoot.org/scenarios/12250
What I'm trying to do is code my laser object to remove itself whenever it contacts one of the aliens (SpaceFly.class) and when it reaches the edge of the world. I've tried as many iterations of my current code and other methods I could think of and I can't get it to work properly. I always get a runtime error saying:
"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."
This is my most straightforward attempt:
import greenfoot.*;
public class Laser extends Actor
{
private int speed;
public Laser()
{
speed = 15;
setRotation(270);
}
public void act()
{
move(speed);
checkHit();
if(getY() < 20 || getY() > getWorld().getWidth() - 20)
{
getWorld().removeObject(this);
}
}
private void checkHit()
{
if (isTouching(SpaceFly.class))
{
removeTouching(SpaceFly.class);
getWorld().removeObject(this);
}
}
}

