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

2018/3/5

java.lang.IllegalStateException

Kolyma Kolyma

2018/3/5

#
Hi its me again (its getting embarassing, but i'm a student and a total noob with greenfoot...) my issue is, that the fruit should fall randomly from the sky and disappear as soon as they touch the ground. This should repeat itself in a loop again and again. I wrote the code for the class Apple and no syntax error is displayed. Nevertheless this error code appears: 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:681) at greenfoot.Actor.getOneIntersectingObject(Actor.java:930) at Apple.act(Apple.java:27) at greenfoot.core.Simulation.actActor(Simulation.java:594) at greenfoot.core.Simulation.runOneLoop(Simulation.java:552) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) this is now the code for Apple:
public class Apple extends Actor
{
    public Apple()
    {
        turn(90);
    }
    /**
     *Der Apfel fällt vom Himmel und bringt einen Punkt. 
     */
    
    public void act() 
    {
        move(4);
        if (isAtEdge()) 
           {
            getWorld().addObject( new Apple(), Greenfoot.getRandomNumber (700)+50, 0);
            getWorld().removeObject(this);
           }  
        Actor mädchen = getOneIntersectingObject(Mädchen.class);
        if (mädchen!=null)
        {
            Counter counter = new Counter();
            ((MyWorld)getWorld()).getCounter().addScore();
        }
    }
}
Can anyone please help me?
Yehuda Yehuda

2018/3/5

#
Don't be embarrassed to ask normal questions here, you're not the first. The problem is you're trying to call the "getOneIntersectingObject" method after removing the actor from the world, when this is done Greenfoot throws the above Exception. Move the if-statement that removes the Actor from the World, to be the last part of the act method.
danpost danpost

2018/3/6

#
Also, remove line 22.
Yehuda wrote...
Move the if-statement that removes the Actor from the World, to be the last part of the act method.
Or, insert the following line after line 17:
return;
You need to login to post a reply.