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

2016/4/30

Illegal State Exception error help

StoveAteMe StoveAteMe

2016/4/30

#
 public void act() 
    {
       setRotation(270);
       move(2);
       setRotation(0);
       Actor missile = getOneIntersectingObject(Missile.class);
    if (missile != null) {
     {
         getWorld().removeObject(missile);
     }    
    }
    if(getY()<20 ) getWorld().removeObject(this);
    hitDetection();
  
    }   
  
   public void hitDetection()  { 
      
   if(isTouching(Rocket.class))  
   {  
       removeTouching(Rocket.class);
       getWorld().removeObject(this);
   }  
}

}
Okay so, whenever I run my program, whenever my "missile" actor comes near the top end of the screen, I have a statement that removes the actor, but then when the hitDetection method is run, I get an error because my "missile" actor is no longer in the world, I'm not quite sure how to work around this error, my program just crashes when this happens. By the way this is the error I get: 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. And these lines of code are where the problem lies: at Missile.hitDetection(Missile.java:33) at Missile.act(Missile.java:27)
danpost danpost

2016/4/30

#
Modify line 13 to perform the necessary check:
if (getWorld() != null) hitDetection();
StoveAteMe StoveAteMe

2016/4/30

#
Oh okay, I just changed it up and my program is working fine, but do you mind explaining what the boolean statement does? I'd like to understand how that one change managed to remove the error.
danpost danpost

2016/5/1

#
Your 'hitDetection' method calls the 'isTouching' method. If the actor is not in a world, it would be impossible for this to even be checked on (the actor will not even know what world to look for collision in -- because there is no world to do it in). Your 'act' method removes the actor when it reaches near the top of the world; then it calls the 'hitDetection' method which requires the actor be in the world before calling the 'isTouching' method, throwing the error. By ensuring that the actor is in a world (the 'getWorld' method returns which world, if any, the object is in), the error can be avoided. If it is not in a world the value returned is 'null', meaning that no world currently has the actor in it. If the value returned is not 'null', then a World object is returned and we can then proceed to call any of the collision checking, location getting and movement methods.
rosh11 rosh11

2016/5/1

#
please help me to write a code to avoid an obstacle by an actor and cover the world(area) by the movements of an actor without repeating his paths.
davmac davmac

2016/5/1

#
rosh11, please start a new discussion topic. You have posted a message in a completely unrelated topic.
You need to login to post a reply.