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

2013/2/19

Need help with removing objects please

MSgreen MSgreen

2013/2/19

#
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;
        }
    }
}
danpost danpost

2013/2/19

#
I think you kinda mis-understood how to use the return statement when removing 'this' from the world. The 'return' statement only causes an immediate exit from the method currently executing. Once the program flow returns to the 'act' method, if the object has been removed any method call that requires the object be in a world will cause an error. You can remove line 48, as the method has no more code to execute at that point and will exit the method automatically, anyway. The same goes for line 38. Both lines 18 and 19 call methods that can remove 'this' from the world. Therefore, switching the order will be of no use to prevent the error. The only thing left is to ask if the object has been removed from the world after the first one and before the second one.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public void act()
{
    move();
    checkMonster();
    if (getWorld() == null) return;
    DeleteAtWorldEdge();
}
// the following is an alternative
public void act()
{
    move()
    checkMonster();
    if (getWorld() != null)
    {
        DeleteAtWorldEdge();
    }
}
MSgreen MSgreen

2013/2/19

#
Thanks to danpost for your quick help. It works now.
You need to login to post a reply.