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

2018/6/19

Error at Object although not existing

fabipfolix fabipfolix

2018/6/19

#
Immediately if i start the program, i'll get a error. And i get it at every act. But the Object, what causes the error isn't existing. The error I get is:
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.isTouching(Actor.java:943)
	at Pfeil.act(Pfeil.java:37)
	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)
And that's the class:
import greenfoot.*;

/**
 * Write a description of class Pfeil here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Pfeil extends Actor
{
    /**
     * Act - do whatever the Pfeil wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    
    public void act() 
    {
        setLocation(getX(),getY()-13);
        Wiese w = (Wiese)getWorld();
       
        if(isAtEdge()){
            int hp=w.getHP();
            if(hp==3){
                w.removeObject(w.getH3());
                w.minusHP();
            } else if(hp==2){
                w.removeObject(w.getH2());
                w.minusHP();
            } else if(hp==1){
                w.removeObject(w.getH1());
                w.minusHP();
                Greenfoot.stop();
            }
            
            w.removeObject(this);
        }
         if(isTouching(Scheibe.class)){
            
            removeTouching(Scheibe.class);
            w.removeObject(this);
        }
    
    }    
    
}
And the second problem is that the arrow stops as soon as it is at Edge.
danpost danpost

2018/6/19

#
fabipfolix wrote...
Immediately if i start the program, i'll get a error. And i get it at every act. But the Object, what causes the error isn't existing. << Error Omitted >> << Code Omitted >> And the second problem is that the arrow stops as soon as it is at Edge.
Add else to the beginning of line 37. Removing an object from the world does not make an object non-existent. Just means it is not in a world any more.
fabipfolix fabipfolix

2018/6/19

#
danpost wrote...
Add else to the beginning of line 37. Removing an object from the world does not make an object non-existent. Just means it is not in a world any more.
Thank you, now it's working
You need to login to post a reply.