I have a scenario where an object shoots a bullet, but when the bullet reaches the world boundaries it pulls up this message: Here is the code I have currently:
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)
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; }