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

2014/3/31

Actor not in world issue.

DanpostIloveyou DanpostIloveyou

2014/3/31

#
I have been having this problem for the past few days and have been unable to find a solution. I am aware that it is caused due to the actor being called after it has been removed. I tried to create a boolean (called "removed") which would determine whether or not the Actor is present in the game, but it seemed to not work. Any help is greatly appreciated. Here is the 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:663) at greenfoot.Actor.getOneIntersectingObject(Actor.java:912) at Actor.hit(Actor.java:69) at Actor.act(Actor.java:46) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) Here is the code of the actor:
public class Medium extends Spaceships
{
    private int timeMedium = 800;
    private int health = 2000;
    private boolean removed = true;
    
    public void act() 
    {
       setLocation(getX() - 1, getY());     
       
       timeMedium--;
       if(timeMedium == 0)
       {   
             getWorld().removeObject(this);
       }    
                     
       if (health <= 0)
       {
           Boolean removed = false;
       }
       if (removed = true) 
       {
           hit();
           fire();   
       }
    }
    
    private int counter =0;
    private void hit()
    {   
        Actor bullet = getOneIntersectingObject(Bullet.class);
       if (bullet != null)
       {
           health = health - 20;
           getWorld().removeObject(bullet);
       }
       
       if(health <= 0)
       {
           getWorld().removeObject(this);
       }
       
       Actor bullet1 = getOneIntersectingObject(Bullet1.class);
       if (bullet1 != null)
       {
           health = health - 20;
           getWorld().removeObject(bullet1);
       }
       
       if(health <= 0)
       {
           getWorld().removeObject(this);
       }
    }
    private void fire()
    { 
       counter++;
       if (counter % 10 == 0)
            
       {
        EBullet ebullet = new EBullet();
        EBullet2 ebullet2 = new EBullet2();
        getWorld().addObject(ebullet, getX() -110, getY() +69);
        ebullet.setRotation(90);
        getWorld().addObject(ebullet2, getX() -110, getY() -69);
        ebullet2.setRotation(90);
       }
    }
}

danpost danpost

2014/3/31

#
Two coding problems I saw immediately are: (1) in line 19, you create a local variable called 'removed' which does not relate in any way to your instance field 'removed' declared on line 5; (2) in line 21, your condition will always return 'true' because you are 'setting 'removed' to 'true' instead of checking if 'removed' is 'true' (you are using a single equal sign instead of a double equal sign); also you probably meant to check for a 'false' value there instead of a 'true' one.. The 'IllegalStateException' stems from line 14, where you remove the actor from the world and line 31 of the 'hit' method where the actor must be in the world for 'getOneIntersectingObject'. 'removed' is not being set to 'true' when you remove the actor (that line can be added after line 14). However, you do not need the boolean field at all as 'getWorld() != null' will tell you if the actor is in a world.
DanpostIloveyou DanpostIloveyou

2014/4/2

#
Thanks for your help, but unfortunetaly it still isn't working. Also, the error happens about 50% of the time, the other 50% it works properly. Here is the new 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:663) at greenfoot.Actor.getOneIntersectingObject(Actor.java:912) at Fast.hit(Fast.java:48) at Fast.act(Fast.java:28) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) Here is the current code:
public class Fast extends Spaceships
{
    private int timeFast = 1200;
    private int health = 1000; 
    /**
     * Act - do whatever the Fast wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
       setLocation(getX() - 1, getY());
        
       timeFast--;
       if(timeFast == 0)
       {   
             getWorld().removeObject(this);
       }     
       if (getWorld() != null) 
       {
           hit();
           fire();                           
       }
    }
   
    private int counter =0;
    private void hit()
    {   
       Actor bullet = getOneIntersectingObject(Bullet.class);
       if (bullet != null)
       {
           health = health - 20;
           getWorld().removeObject(bullet);
       }
       
       if(health <= 0)
       {
           getWorld().removeObject(this);  
       }
       
       Actor bullet1 = getOneIntersectingObject(Bullet1.class);
       if (bullet1 != null)
       {
           health = health - 20;
           getWorld().removeObject(bullet1);
       }
       
       if(health <= 0)
       {
           getWorld().removeObject(this); 
       }      
    }
    private void fire()
    { 
       counter++;
       if (counter % 10 == 0)
            
       {
        EBullet ebullet = new EBullet();
        EBullet2 ebullet2 = new EBullet2();
        getWorld().addObject(ebullet, getX() - 85, getY() +43);
        ebullet.setRotation(90);
        getWorld().addObject(ebullet2, getX() - 85, getY()- 43);
        ebullet2.setRotation(90);        
       }
    }
}
danpost danpost

2014/4/2

#
Remove lines 35 through 40 above (they are duplicated at lines 47 through 51). It should have been what was causing the 'IllegalStateException' throw..
You need to login to post a reply.