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

2016/5/19

I keep getting a terminal window when my projectile hits something and dissapears.

bjornjulian00 bjornjulian00

2016/5/19

#
I want my projectile to dissapear when it hits something, and it does, but it gives me the terminal window: 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.getOneIntersectingObject(Actor.java:958) at Plasma.shootMissile(Plasma.java:42) at Plasma.act(Plasma.java:11) 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) Here is the "Plasma" (the name of the projectile)'s code:
import greenfoot.*;
import java.awt.Color;

public class Plasma extends Actor
{
    public void act() 
    {
        move();
        shootUFO();
        shootAsteroid();
        shootMissile();
        atEdge();
    }
    public void move()
    {
        move (30);
    }
    public void shootUFO()
    {
        Actor UFO = getOneIntersectingObject(UFO.class);
        if (UFO!=null)
        {
            World Level1 = getWorld();
            Level1.removeObject(UFO);
            Level1 level1 = (Level1) Level1;
            Level1.removeObject(this);
        }
    }
    public void shootAsteroid()
    {
        Actor Asteroid = getOneIntersectingObject(Asteroid.class);
        if (Asteroid!=null)
        {
            World Level1 = getWorld();
            Level1.removeObject(Asteroid);
            Level1 level1 = (Level1) Level1;
            Level1.removeObject(this);
        }
    }
    public void shootMissile()
    {
        Actor Missile = getOneIntersectingObject(Missile.class);
        if (Missile!=null)
        {
            World Level1 = getWorld();
            Level1.removeObject(Missile);
            Level1 level1 = (Level1) Level1;
            Level1.removeObject(this);
        }
    }
    public void atEdge()
    {
        if(isAtEdge())
        {
            World Level1 = getWorld();
            Level1.removeObject(this);
        }
    }
}
danpost danpost

2016/5/19

#
All your methods called from the act method, except for the 'move' method, could potentially remove the actor from the world. If any but the last one does, you will get that error. You could either place the following line between each method:
if (getWorld() == null) return;
or you can preceed each method call like this example
if (getWorld() != null) shootAsteroid();
You need to login to post a reply.