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

2013/10/18

Removing an object after obtained item.

bananafin bananafin

2013/10/18

#
"Hello world!" I am trying to remove an object that blocks off the end of my world after i pickup a "Key" item. I know I am doing something wrong but i can't figure out how i should make it work. I know "getWorld().removeObject(LevelDetect.class);" does not work, because i need an actor for that. Does that mean i need to list the LevelDetect's in the world and then call them or is it entirely something else? Thanks in advance! I have the following code
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
public class Alice extends Mover
{
 private boolean hasKey = false;
 
public void act()
    {
        checkCollisionKey();
        checkCollisionLevel();
        checkHasKey();
    }  
 
public void checkCollisionKey()
    {
        Actor collided;
        collided = getOneIntersectingObject(Key.class);
        if (collided !=null)
        {
            hasKey=true;
            getWorld().removeObject(collided);
            getWorld().addObject(new Key2(), 180,10);
        }
 
    }
 
private void checkHasKey()
    {
        if(hasKey !=true)
        {
            getWorld().removeObject(LevelDetect.class);
        }
    }
 
private void checkCollisionLevel()
    {
        Actor collided;
        collided = getOneIntersectingObject(LevelDetect.class);
        if(collided !=null)
        {
            setLocation(getX()-1, getY());
        }
 
    }
SPower SPower

2013/10/18

#
Change this:
1
getWorld().removeObject(LevelDetect.class);
into this:
1
getWorld().removeObjects(getWorld().getObjects(LevelDetect.class));
SPower SPower

2013/10/18

#
And some advice to make your code more readable:
1
2
3
4
5
6
7
8
9
10
// when checking booleans, this will, of course, work:
if(hasKey !=true)
 
// but this is more readable:
if(hasKey == false)
 
// and this is my personal favourite:
if(!hasKey)
 
// these 3 options all do the same, but look different and may be easier to read and write
bananafin bananafin

2013/10/18

#
Thanks for the reply. The problem with that is that i can run to the next level without the key, eventhough "Alice" gives "hasKey=false" And thanks for the tips!
danpost danpost

2013/10/18

#
You just have the wrong case. You want to remove the object if Alice does have the key, not if she does not (remove the preceeding '!' or change '!=' to '==' or toggle the 'false'/'true').
bananafin bananafin

2013/10/18

#
That was the problem indeed. It works! How did i even mis that? Thank you for the help SPower and danpost.
danpost danpost

2013/10/18

#
Once you reach line 18, you KNOW that the key has been obtained. You can remove the DetectLevel object (as an immediate action) right there instead of adding another method (the 'checkHasKey' method) and calling it from the act method. With that, you may be able to remove the 'hasKey' field altogether; although, I do not know what part the Key2 object plays (whether immediate action can be taken when that key is obtained or not). You would then have this:
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
public class Alice extends Mover
{
    public void act()
    {
        checkCollisionKey();
        checkCollisionLevel();
    }  
 
    public void checkCollisionKey()
    {
        Actor collided;
        collided = getOneIntersectingObject(Key.class);
        if (collided !=null)
        {
            getWorld().removeObject(collided);
            getWorld().removeObjects(getWorld().getObjects(DetectLevel.class));
            getWorld().addObject(new Key2(), 180,10);
        }
    }
 
    private void checkCollisionLevel()
    {
        Actor collided;
        collided = getOneIntersectingObject(LevelDetect.class);
        if(collided !=null)
        {
            setLocation(getX()-1, getY());
        }
    }
}
bananafin bananafin

2013/10/18

#
Thanks! It is now set in one method
1
2
3
4
5
6
7
8
9
10
11
12
public void checkCollisionKey()
{
    Actor collided;
    collided = getOneIntersectingObject(Key.class);
    if (collided !=null)
    {
        getWorld().removeObject(collided);
        getWorld().addObject(new Key2(), 180,10);
        getWorld().removeObjects(getWorld().getObjects(LevelDetect.class));
    }
 
}
And the checkHasKey is gone alltogether. All the Key2 object does is spawn on the X and Y as a smaller object. It's just so the player can see they have obtained the key.
danpost danpost

2013/10/18

#
Well done!
You need to login to post a reply.