Hi guys,
can somebody please help me, I got a problem removing some Objects. I'm making a game, the player(Person) trys to shoot the monsters. If one monster is shot, he should be removed and also the bullet(Schuss). I always get an Error an Greenfoot stops. It says : "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."
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Schuss here. * * @author (your name) * @version (a version number or a date) */ public class Schuss extends Test { /** * Act - do whatever the Schuss wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { move(); checkMonster(); DeleteAtWorldEdge(); // Schuss wird gelöscht, wenn er ein MOnster trifft oder das Ende der Welt errreicht } void move() { setLocation(getX() + 2 ,getY()); } void checkMonster() { Actor monster = getOneIntersectingObject(Monster. class ); if (monster != null ) { Greenfoot.playSound( "treffer.mp3" ); World world; world = getWorld(); world.removeObject(monster); world.removeObject( this ); return ; } } void DeleteAtWorldEdge() { if (atWorldEdge()) { getWorld().removeObject( this ); return ; } } } |