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

2014/11/25

check boundaries problem

jedijosiah jedijosiah

2014/11/25

#
I have a scenario where an object shoots a bullet, but when the bullet reaches the world boundaries it pulls up this message:
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.getY(Actor.java:170)
	at Bullet.checkBoundaries(Bullet.java:24)
	at Bullet.act(Bullet.java:14)
	at greenfoot.core.Simulation.actActor(Simulation.java:583)
	at greenfoot.core.Simulation.runOneLoop(Simulation.java:541)
	at greenfoot.core.Simulation.runContent(Simulation.java:215)
	at greenfoot.core.Simulation.run(Simulation.java:205)
Here is the code I have currently:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**ITs an egg being shot from a plane
 * 
 * @author jedi
 * @version b v 2.0998
 * 
 */
public class Bullet extends Actor
{
   public void act()  
   {  
       setLocation(getX() + speed, getY());  
       checkBoundaries();  
       destroyEnemies();  
   }  
   //we add a method "checkBoundaries()" that destroys bullets that are off screen.  
   public void checkBoundaries()  
   {  
       if(getX() > getWorld().getWidth() - 1)   
            getWorld().removeObject(this);  
       else if(getX() < 1)   
            getWorld().removeObject(this);  
       if(getY() > getWorld().getHeight() - 1)   
            getWorld().removeObject(this);  
       else if(getY() < 1)   
            getWorld().removeObject(this);  
   }  
   //"destroyEnemies()" destroys enemies.  
   public void destroyEnemies()  
   {  
       //"Enemy" can be any class that you want the bullet to destroy.   
       Actor enemy = getOneIntersectingObject(Enemy.class);  
       if(enemy != null) {  
            getWorld().removeObject(enemy);  
            getWorld().removeObject(this);  
       }  
      
   }  
  
   private int speed = 10;  
}  
davmac davmac

2014/11/25

#
Read this, it describes the problem precisely.
jedijosiah jedijosiah

2014/11/26

#
davmac wrote...
Read this, it describes the problem precisely.
Those solutions partly worked except now it deletes most of the bullets, but the first bullet I fire and if I move up and down where ever the first bullet goes to leave the map will freeze on the screen while the rest will disappear as they are supposed to. They freeze in almost equal distance of each other.
danpost danpost

2014/11/27

#
You may have to post the updated Bullet class for further help.
jedijosiah jedijosiah

2014/11/28

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**ITs an egg being shot from a plane
 * 
 * @author Josiah 
 * @version b v 2.0998
 * This bullet code is not mine I found it in the greefoot discussions and have made changes to make it work with my current game setup.
 */
public class Bullet extends Actor
{
   public void act()  
   {  
       setLocation(getX() + speed, getY());  
       checkBoundaries();  
       destroyEnemies();  
   }  
    private int speed = 10;
   //we add a method "checkBoundaries()" that destroys bullets that are off screen.  
   public void checkBoundaries()  
   {  
       if(getX() > getWorld().getWidth() - 1)   
            getWorld().removeObject(this);  
       else if(getX() < 1)   
            getWorld().removeObject(this);  
       if(getY() > getWorld().getHeight() - 1)   
            getWorld().removeObject(this);  
       else if(getY() < 1)   
            getWorld().removeObject(this);  
   }  
   //"destroyEnemies()" destroys enemies.  
   public void destroyEnemies()  
   {  
       //removes enemy class once bullet hits also plays sound.   
       Actor enemy = getOneIntersectingObject(Enemy.class);  
       if(enemy != null) {  
            getWorld().removeObject(enemy);  
            getWorld().removeObject(this);  
            Greenfoot.playSound("ping.wav");
       }  

{
    setLocation(getX() - 1, getY());
    if (getOneIntersectingObject(Bullet.class) != null) {
        getWorld().removeObject(this);
        return;
    }
    if (getX() <= 0)  {
        getWorld().removeObject(this);
        return;
    }
}


}  
}
I tried both solutions separate and together and the same problem occurs.
danpost danpost

2014/11/28

#
Since you are using method calls to perform the different functions, using the 'return' statement will not work. You will have to use the other method using 'getWorld() != null' in an 'if' statement.
You need to login to post a reply.