when the bullet hit the enemy it decrese its health by 1 and disapear but after the first disapeared bullet i get this 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 Bullet.act(Bullet.java:29)
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)
it says that the actor bullet is not there what is true. because i wanted that the bullet disapear
heres the code for the Bullet
when i delete
getWorld().removeObject(this);
everything is ok but i want the Bullet to disapear after hitting the enemy
private boolean atWorldEdge() { return (getX()==0 || getY()==0 || getX()==getWorld().getWidth()-1 || getY()==getWorld().getHeight()-1); } public void act() { hitEnemy(); { setLocation(getX() + 36, getY()); if(atWorldEdge()) { getWorld().removeObject(this); return; } } } public void hitEnemy() { gegner1 enemy = (gegner1) getOneObjectAtOffset(0, 0, gegner1.class); if (enemy != null) { enemy.setHealth(-1); getWorld().removeObject(this); } if (enemy == null) { getWorld().removeObject(enemy);