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

2014/12/17

How to stop objects at world edge

AA1 AA1

2014/12/17

#
This code below is suppose to get the object to disappear at the world edge but I get a this run time 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.getX(Actor.java:157) at Bullets.disappearAtEdge(Bullets.java:84) at Bullets.act(Bullets.java:60) private void disappearAtEdge() { if (getX() == 0 || getX() == getWorld().getWidth()-1) //if the x location 0 or it is one less than //it is one less than width of the world { World world = getWorld(); //assign world to getWorld NoMansLand noMansLand = (NoMansLand) getWorld(); //creates a cast to call the gameOver() method noMansLand.removeObject(this); } else if (getY() == 0 || getY() == getWorld().getHeight()-1) //if the y location 0 or it is onle less than //it is one less than width of the world { World world = getWorld(); //assign world to getWorld NoMansLand noMansLand = (NoMansLand) getWorld(); //creates a cast to call the gameOver() method noMansLand.removeObject(this); } }
berdie berdie

2014/12/17

#
isAtEdge public boolean isAtEdge() Detect whether the actor has reached the edge of the world. The actor is at the edge of the world if their position is at, or beyond, the cells at the very edge of the world. For example, if your world is 640 by 480 pixels, an actor is at the edge if its X position is <= 0 or >= 639, or its Y position is <= 0 or >= 479. Returns: True if the actor is at or beyond the edge cell of the world, and false otherwise. is probably an easier method from the api to get the objects at Edge.
danpost danpost

2014/12/17

#
The exception is due mainly on your act method and what methods it calls. Somewhere before you call 'disappearAtEdge' in the act method, you are calling another method (probably checking to see if the bullet hit a target) that removes the bullet from the world. You need to be sure the actor is in the world before calling 'disappearAtEdge':
1
if (getWorld() != null) disappearAtEdge();
AA1 AA1

2014/12/18

#
Thanks
You need to login to post a reply.