Hi guys, I'm back, facing another problem with my code. So far, my ship functionally fires a laser. The laser is supposed to be removed and the asteroid it hits should also be removed (also works correctly), but for some reason every time the actors collide, the program pauses so I have to press play again and the terminal window pops up 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.
at greenfoot.Actor.failIfNotInWorld(Actor.java:681)
at greenfoot.Actor.getOneIntersectingObject(Actor.java:930)
at LaserBeam.hitDetectionThree(LaserBeam.java:43)
at LaserBeam.act(LaserBeam.java:21)
at greenfoot.core.Simulation.actActor(Simulation.java:594)
at greenfoot.core.Simulation.runOneLoop(Simulation.java:552)
at greenfoot.core.Simulation.runContent(Simulation.java:215)
at greenfoot.core.Simulation.run(Simulation.java:205)
Here's my coding for actor "LaserBeam". Could someone explain the problem?
import greenfoot.*;
Thanks for your help!
public class LaserBeam extends Actor
{
public void act()
{
setLocation(getX()+25, getY());
hitDetectionOne();
hitDetectionTwo();
hitDetectionThree();
checkSide();
}
public void hitDetectionOne()
{
Actor a = getOneIntersectingObject(AstOne.class);
if(a != null)
{
getWorld().removeObject(a);
getWorld().removeObject(this);
}
}
public void hitDetectionTwo()
{
Actor b = getOneIntersectingObject(AstTwo.class);
if(b != null)
{
getWorld().removeObject(b);
getWorld().removeObject(this);
}
}
public void hitDetectionThree()
{
Actor c = getOneIntersectingObject(AstThree.class);
if(c != null)
{
getWorld().removeObject(c);
getWorld().removeObject(this);
}
}
public void checkSide()
{
Actor d = getOneIntersectingObject(RightWall.class);
if(d != null)
{
getWorld().removeObject(this);
}
}
}

