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

2012/10/6

I need help!

Game/maniac Game/maniac

2012/10/6

#
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.getX(Actor.java:157) at Mover.atWorldEdge(Mover.java:70) at AstroBall.act(AstroBall.java:37) at greenfoot.core.Simulation.actActor(Simulation.java:565) at greenfoot.core.Simulation.runOneLoop(Simulation.java:523) at greenfoot.core.Simulation.runContent(Simulation.java:213) at greenfoot.core.Simulation.run(Simulation.java:203) This is my code for AstroBall:
import greenfoot.*;  // (World, Actor, GreenfootImage, and Greenfoot)

public class AstroBall extends MovingThing
{
    int hp=15;
    int dmg=5;
    public boolean canSee(Class clss)
    {
        Actor actor = getOneObjectAtOffset(0, 0, clss);
        return actor != null;
    }
    public AstroBall()
    {
        setRotation(Greenfoot.getRandomNumber(360));
    }
    public AstroBall(int direction, int health)
    {
        setRotation(direction);
        hp=health;
        getImage().scale(getImage().getWidth()-health,getImage().getHeight()-health);
    }
    /**
     * Act - do whatever the Ball wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        if(canSee(Bullet.class)){
            destroy();
        }
        move(3);
        if(atWorldEdge()){
            turn(153);
        }
        bounce();
    }
    public void bounce()
    {
        if(canSee(Ball.class))
        {
            turn(189);
        }
        if(canSee(BallMobile.class))
        {
            turn(164);
        }
        if(canSee(AstroBall.class))
        {
            turn(178);
        }
    }
    public void destroy()
    {
        int nhp=hp+dmg;
        getWorld().addObject(new AstroBall(315, nhp),getX()+10,getY()-10);
        getWorld().addObject(new AstroBall(45, nhp),getX()+10,getY()+10);
        getWorld().addObject(new AstroBall(135, nhp),getX()-10,getY()+10);
        getWorld().addObject(new AstroBall(225, nhp),getX()-10,getY()-10);
        getWorld().removeObject(this);
    }
}
Gevater_Tod4711 Gevater_Tod4711

2012/10/6

#
if the method destroy is called it will remove your object (59. getWorld().removeObject(this)) if you then try to get the position of this actor (32. atWorldEdge()) Greenfoot throws this error, because you cant get the position of an actor which is not in the world. So either you could change the order of your methods, so that the method destroy always is called at the end of the act method, or you could stop the act method after the destroy using return. You just have to write return; after destroy is called in the act method.
SPower SPower

2012/10/6

#
This is what's wrong: This exception occurs when you're not in a world, but you call something you have to be in a world for. I don't see your atWorldEdge method, but I do know it uses the getX method. But, you are removed from the world:
public void destroy()  
    {  
        int nhp=hp+dmg;  
        getWorld().addObject(new AstroBall(315, nhp),getX()+10,getY()-10);  
        getWorld().addObject(new AstroBall(45, nhp),getX()+10,getY()+10);  
        getWorld().addObject(new AstroBall(135, nhp),getX()-10,getY()+10);  
        getWorld().addObject(new AstroBall(225, nhp),getX()-10,getY()-10);  
        getWorld().removeObject(this); // here!!
    }  
And after that, you can't use things like getX or getY anymore. What you need to do is change act to this:
public void act()
{  
     move(3);  
     if (atWorldEdge()) {  
         turn(153);  
     }  
     bounce();  
     if (canSee(Bullet.class)) {  
        destroy();
     }
}
So you will be removed at the end of your act method.
Game/maniac Game/maniac

2012/10/6

#
thanks to both of you
You need to login to post a reply.