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

2017/5/22

Actor not in world error

Soggy_Diaperz Soggy_Diaperz

2017/5/22

#
Hello. I'm relatively new to this and am currently creating a space invaders style game. However, the code for my Missile().class seems to create an infinite stream of errors when the projectile hits any of the 3 aliens. Here is my code:
public class Missile extends Actor
{
    /**
     * Act - do whatever the Missile wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public GreenfootImage image = getImage();
    public int xSize = image.getWidth();
    public int ySize = image.getHeight();
    public void act() 
    {
        image.scale(xSize - 40, ySize - 60);
        setImage(image);
        setLocation(getX(), getY()-8);            
        World world;
        world = getWorld();
       
        if ((getX() <= 5 || getX() >= world.getWidth()-5 ) || (getY() <= 5 || getY() >= world.getHeight()-5))
        {
            world.removeObject(this);
        }
        
        killAliens();
    }    
    
    public void killAliens()
    {
        Actor alien1, alien2, alien3;
        alien1 = getOneObjectAtOffset(0, 0, alien1.class);
        alien2 = getOneObjectAtOffset(0, 0, alien2.class);
        alien3 = getOneObjectAtOffset(0, 0, alien3.class);
        
        World world;
        world = getWorld();
        
        if (alien1 != null)
        {
            world.removeObject(alien1);
            world.removeObject(this);
            Greenfoot.playSound("murder.wav");
        }
        if (alien2 != null)
        {
            world.removeObject(alien2);
            world.removeObject(this);
            Greenfoot.playSound("murder.wav");
        }
        if (alien3 != null)
        {
            world.removeObject(alien3);
            world.removeObject(this);
            Greenfoot.playSound("murder.wav");
        }
    }
}
I repeatedly receive this error:
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.getOneObjectAtOffset(Actor.java:913) at Missile.killAliens(Missile.java:37) at Missile.act(Missile.java:31) 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)
Any help is much appreciated!
FutureCoder FutureCoder

2017/5/22

#
change the code to
if(isTouching(alien1.class))
{
removeTouching(alien1.class);
getWorld().removeObject(this);
 Greenfoot.playSound("murder.wav");
} 
 else if(isTouching(alien2.class))
{
removeTouching(alien2.class);
getWorld().removeObject(this);
 Greenfoot.playSound("murder.wav");
}
else if(isTouching(alien3.class))
{
removeTouching(alien3.class);
getWorld().removeObject(this);
 Greenfoot.playSound("murder.wav");
}
and take out line 28 to 53 of your code
Soggy_Diaperz Soggy_Diaperz

2017/5/22

#
It works a fair bit better, although I now get this error.
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.isTouching(Actor.java:972) at Missile.killAliens(Missile.java:36) at Missile.act(Missile.java:31) 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)
It's a lot less often than before. Instead of being every time the missile hits an alien, it's now like every 10th time. It's a lot more tolerable, although I still can't figure out why it's happening.
FutureCoder FutureCoder

2017/5/22

#
what lines of code is it saying is the problem, can you send me them
danpost danpost

2017/5/22

#
Cut and paste line 23, inserting it at line 17. That way you are not trying to kill an alien after edge removal.
You need to login to post a reply.