Hello, I'm trying to create an object whenever I press the space key, but I keep on getting an error in the terminal window 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."
My main issue is that the object isn't being created whenever it should be, instead the program stops working.
Here is my code (please help improve it so I can fix my issue):
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
public class Spaceship1 extends Actor
{
World world= getWorld();
public void shoot() {
//Space1 x = new Space1();
if (Greenfoot.isKeyDown("space")==true){
Spaceship1 x = new Spaceship1();
world.addObject(new lazer(), x.getX(), x.getY());
}
else {
Spaceship1 x = new Spaceship1();
//world.addObject(new lazer(), x.getX(), x.getY());
}
//addObject(new lazer(), x.getX(), x.getY());
}
public void act()
{
// Add your action code here.
shoot();
checkKeyPress();
}
}
thanks!
