I am trying to remove an object when I shoot my bullet. But I keep getting a message:
java.lang.IllegalStateException: Actor not in World. An attemp was made to use the actor's location while it is not in the world.
And then it highlights this in red
at Ammo.firstAct(Ammo.java:30)
at Ammo.act(Ammo.java:14)
I tried so many things but it keeps giving me an error. I would appreciate some advice. The ammo class is in line 99. I was not sure if you needed to see my world code
//World Class
public class Worldd extends World
{
private int score;
private int time;
private int numberOfEnemyTouched=0;
private int numberOfEnemyMissed=0;
public Worldd()
{
super(900,537,1);
prepare();
score = 0;
time = 2000;
showScore();
showTime();
}
public void act()
{
if (Greenfoot.getRandomNumber(200) <3 )
{
addObject(new Enemy(), Greenfoot.getRandomNumber(100), Greenfoot.getRandomNumber(500));
}
if (Greenfoot.getRandomNumber(200) <3)
{
addObject(new Rocket(), Greenfoot.getRandomNumber(100), Greenfoot.getRandomNumber(500));
}
countTime();
}
private void setup()
{
addObject(new Plane(), 900, 275);
}
public void addScore(int points)
{
score = (numberOfEnemyTouched);
showScore();
if (score < 0)
{
Greenfoot.stop();
}
}
private void showScore()
{
showText("Score: " + score, 60,20);
}
private void countTime()
{
time--;
showTime();
if (time == 0)
{
showEndMessage();
Greenfoot.stop();
}
}
/**
* Show the remaining game time on screen.
*/
private void showTime()
{
showText("Time: " + time, 700, 25);
}
/**
* Show the end-of-game message on screen.
*/
private void showEndMessage()
{
showText("Time is Up", 390, 150);
showText("Your final score: " + score + " points", 390, 170);
}
private void prepare()
{
Plane plane = new Plane();
addObject(plane, 850, 300);
}
public void incNumberOfEnemyTouched(){
numberOfEnemyTouched+= 1;
}
public void incNumberOfEnemyMissed(){
numberOfEnemyMissed+=1;
}
}
//Ammo Class
public class Ammo extends Actor
{
public int span = Greenfoot.getRandomNumber(20)+60;
public void act()
{
firstAct();
}
private void firstAct()
{
move(-10);
span--;
if (isAtEdge())
{
getWorld().removeObject(this);
}
if(span == 0)
{
getWorld().addObject(new Explosion(), getX(),getY());
getWorld().removeObject(this);
}
if(isTouching(Enemy.class))
{
removeTouching(Enemy.class);
}
}
// Plane Class
public class Plane extends Actor
{
public Plane()
{
GreenfootImage myImage = getImage();
int myNewHeight = (int)myImage.getHeight()/3;
int myNewWidth = (int)myImage.getWidth()/3;
myImage.scale(myNewWidth, myNewHeight);
}
public void act()
{
checkKeys();
checkTouch();
}
private void checkKeys()
{
if (Greenfoot.isKeyDown("up"))
{
setLocation(getX(), getY()-8);
}
if (Greenfoot.isKeyDown("down"))
{
setLocation(getX(), getY()+8);
}
if (Greenfoot.isKeyDown("right"))
{
setLocation(getX()+4, getY());
}
if (Greenfoot.isKeyDown("left"))
{
setLocation(getX()-4, getY());
}
if ("space".equals(Greenfoot.getKey()))
{
shoot();
}
}
private void checkTouch()
{
if (isTouching(Enemy.class))
{
removeTouching(Enemy.class);
Worldd worldde = (Worldd)getWorld();
worldde.incNumberOfEnemyTouched();
worldde.addScore(1);
}
}
private void shoot()
{
Ammo ammo = new Ammo();
getWorld().addObject(ammo, getX(), getY());
ammo.setRotation(getRotation());
ammo.move(20);
}
}
}
