ASAP!!
I have a problem in my code. Everything works fine in my game, which is about shooting aliens, and if they reach the land, the game ends. When they reach the land, the game ends, but when I shoot a ship, it says
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:663)
at greenfoot.Actor.getY(Actor.java:170)
at AlienShip.checkIfOnLand(AlienShip.java:85)
at AlienShip.onTheLand(AlienShip.java:97)
at AlienShip.act(AlienShip.java:21)
at greenfoot.core.Simulation.actActor(Simulation.java:568)
at greenfoot.core.Simulation.runOneLoop(Simulation.java:526)
at greenfoot.core.Simulation.runContent(Simulation.java:215)
at greenfoot.core.Simulation.run(Simulation.java:205)
Heres the code
HELP!!!
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class aAienShip here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class AlienShip extends Enemy
{
public int lives = 0;
/**
* Act - do whatever the aAienShip wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
moveDownwards();
explodeIfPotatoHits();
onTheLand();
}
/**
* This will make the AlienShip move downwards slowly.
*/
public void moveDownwards()
{
int x = getX();
int y = getY();
int ny = getY()+1;
setLocation(x,ny) ;
}
/**
* This checks if there is a potato on the AlienShip.
* If so, it will remove itself from the world, and create a new ship.
*/
public void explodeIfPotatoHits()
{
if(canSee(Potato.class))
{
spawnNewShip();
getWorld().removeObject(this);
}
}
/**
* This will spawn a new ship if the spawnNewShip method is said.
*/
public void spawnNewShip()
{
{
AlienShip newAlienShip;
newAlienShip = new AlienShip();
World world;
world = getWorld();
int worldWidth = 800;
int worldHeight = 400;
int x = Greenfoot.getRandomNumber(worldWidth);
int y = Greenfoot.getRandomNumber(worldHeight);
world.addObject(newAlienShip, x, y-200);
}
}
/**
* Test if we are close to one of the edges of the world. Return true is we are.
*/
public boolean checkIfOnLand()
{
if(getY() >=280)
return true;
else
return false;
}
/**
* If the bollean atWorldEdge replies true, it will remove the potato.
*/
public void onTheLand()
{
if(checkIfOnLand())
{
gameOver();
return;
}
}
public void gameOver()
{
Greenfoot.stop();
}
}
