Hello. I'm relatively new to this and am currently creating a space invaders style game. However, the code for my Missile().class seems to create an infinite stream of errors when the projectile hits any of the 3 aliens. Here is my code:
I repeatedly receive this error:
public class Missile extends Actor
{
/**
* Act - do whatever the Missile wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public GreenfootImage image = getImage();
public int xSize = image.getWidth();
public int ySize = image.getHeight();
public void act()
{
image.scale(xSize - 40, ySize - 60);
setImage(image);
setLocation(getX(), getY()-8);
World world;
world = getWorld();
if ((getX() <= 5 || getX() >= world.getWidth()-5 ) || (getY() <= 5 || getY() >= world.getHeight()-5))
{
world.removeObject(this);
}
killAliens();
}
public void killAliens()
{
Actor alien1, alien2, alien3;
alien1 = getOneObjectAtOffset(0, 0, alien1.class);
alien2 = getOneObjectAtOffset(0, 0, alien2.class);
alien3 = getOneObjectAtOffset(0, 0, alien3.class);
World world;
world = getWorld();
if (alien1 != null)
{
world.removeObject(alien1);
world.removeObject(this);
Greenfoot.playSound("murder.wav");
}
if (alien2 != null)
{
world.removeObject(alien2);
world.removeObject(this);
Greenfoot.playSound("murder.wav");
}
if (alien3 != null)
{
world.removeObject(alien3);
world.removeObject(this);
Greenfoot.playSound("murder.wav");
}
}
}
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:711)
at greenfoot.Actor.getOneObjectAtOffset(Actor.java:913)
at Missile.killAliens(Missile.java:37)
at Missile.act(Missile.java:31)
at greenfoot.core.Simulation.actActor(Simulation.java:604)
at greenfoot.core.Simulation.runOneLoop(Simulation.java:562)
at greenfoot.core.Simulation.runContent(Simulation.java:221)
at greenfoot.core.Simulation.run(Simulation.java:211)
Any help is much appreciated!