This site requires JavaScript, please enable it in your browser!
Greenfoot back
GreenMan15
GreenMan15 wrote ...

2016/3/10

Trying to remove an object

GreenMan15 GreenMan15

2016/3/10

#
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);
        
    }
  }
}
danpost danpost

2016/3/11

#
Just add a 'return' statement after each 'getWorld().removeObject(this)' line. This will cause an immediate break out of the method so that code requiring the actor be in the world are not executed after the actor is removed from the world:
// when you use the following
getWorld().removeObject(this);
// follow it with this:
return;
It is not necessary to do this when no other code is executed within the method after the removal of the actor from the world.
GreenMan15 GreenMan15

2016/3/11

#
Wow can't believe it was a small thing like that. Thank you
You need to login to post a reply.