Hello,
I'm re-creating the Atari Centipede game. I'm working on making my actor classes be subclasses of more general superclasses (for example, the subclasses "Centipede" and "Mushroom" would be subclasses of "Hittable"). However, when I shoot the laser and it touches a mushroom, an illegal state exception is thrown for the laser class. Can anyone please help? Thank you.
Here's the code for the laser class:
public void act()
{
// Add your action code here.
MyWorld k = (MyWorld)getWorld();
Score scTxt = k.getObjects(Score.class).get(0);
move();
if (getWorld() != null){
Hittable c = (Centipede)getOneIntersectingObject(Centipede.class);
if (c != null){
c.onHit();
onHitTarget();
}
Hittable f = (Flea)getOneIntersectingObject(Flea.class);
if (f != null){
f.onHit();
onHitTarget();
}
Hittable m = (Mushroom)getOneIntersectingObject(Mushroom.class);
if (m != null){
m.onHit();
onHitTarget();
}
Hittable p = (Poison)getOneIntersectingObject(Poison.class);
if (p != null){
p.onHit();
onHitTarget();
}
Hittable sp = (Spurter)getOneIntersectingObject(Spurter.class);
if (sp != null){
sp.onHit();
onHitTarget();
}
Hittable sc = (Scorpion)getOneIntersectingObject(Scorpion.class);
if (sc != null){
sc.onHit();
onHitTarget();
}
if (getWorld() != null && shouldBeRemoved()){
remove();
}
}
}
public void move(){
setLocation(getX(), getY() - speed);
}
public boolean inWorld(){
boolean isInWorld = true;
if (getWorld() != null){
isInWorld = true;
}
return isInWorld;
}
public void onHitTarget(){
remove();
// return;
}
public void remove(){
getWorld().removeObject(this);
return;
}
public boolean shouldBeRemoved(){
boolean shouldRemove = false;
if (getY() - getImage().getHeight()/2 > getWorld().getHeight()){
shouldRemove = true;
}
return shouldRemove;
}
}
And here's the exception:
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:714)
at greenfoot.Actor.getOneIntersectingObject(Actor.java:965)
at Laser.act(Laser.java:47)
at greenfoot.core.Simulation.actActor(Simulation.java:567)
at greenfoot.core.Simulation.runOneLoop(Simulation.java:530)
at greenfoot.core.Simulation.runContent(Simulation.java:193)
at greenfoot.core.Simulation.run(Simulation.java:183)

