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 Mover.atWorldEdge(Mover.java:70)
at AstroBall.act(AstroBall.java:37)
at greenfoot.core.Simulation.actActor(Simulation.java:565)
at greenfoot.core.Simulation.runOneLoop(Simulation.java:523)
at greenfoot.core.Simulation.runContent(Simulation.java:213)
at greenfoot.core.Simulation.run(Simulation.java:203)
This is my code for AstroBall:
import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot) public class AstroBall extends MovingThing { int hp=15; int dmg=5; public boolean canSee(Class clss) { Actor actor = getOneObjectAtOffset(0, 0, clss); return actor != null; } public AstroBall() { setRotation(Greenfoot.getRandomNumber(360)); } public AstroBall(int direction, int health) { setRotation(direction); hp=health; getImage().scale(getImage().getWidth()-health,getImage().getHeight()-health); } /** * Act - do whatever the Ball wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { if(canSee(Bullet.class)){ destroy(); } move(3); if(atWorldEdge()){ turn(153); } bounce(); } public void bounce() { if(canSee(Ball.class)) { turn(189); } if(canSee(BallMobile.class)) { turn(164); } if(canSee(AstroBall.class)) { turn(178); } } public void destroy() { int nhp=hp+dmg; getWorld().addObject(new AstroBall(315, nhp),getX()+10,getY()-10); getWorld().addObject(new AstroBall(45, nhp),getX()+10,getY()+10); getWorld().addObject(new AstroBall(135, nhp),getX()-10,getY()+10); getWorld().addObject(new AstroBall(225, nhp),getX()-10,getY()-10); getWorld().removeObject(this); } }