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

2013/11/6

Removing Objects within an Actor Class

tartags11 tartags11

2013/11/6

#
I'm trying to remove an object within an Actor Class. Can someone help me figure out what I am doing wrong with the code. Says that the getObjects method is not known.
1
2
3
World world;
world = getWorld();
world.removeObjects(getObjects(Horse1.class));
tartags11 tartags11

2013/11/6

#
Never mind. Figured it out.
tartags11 tartags11

2013/11/6

#
Actually, I found another problem. Once again the getObject method is not known. It worked for Horse 1 (since it is in the horse1 actor class). But now its not working. Please Help.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public void getLocation1()
    {
        Actor Horse2;
        Actor Horse3;
        Actor Horse4;
        Horse2 = getObject(Horse2.class);
        Horse3 = getObject(Horse3.class);
        Horse4 = getObject(Horse4.class);
        if ( getX() >= 975)
        {
            World world;
            world = getWorld();
            world.removeObject(this);
            world.removeObject(Horse2);
            world.removeObject(Horse3);
            world.removeObject(Horse4);
        }
    }
danpost danpost

2013/11/6

#
That is because there is no 'getObject' method in the 'World' class; only a 'getObjects' method (with the 's' at the end).
tartags11 tartags11

2013/11/6

#
Its not in the world class, its in an actor class.
danpost danpost

2013/11/6

#
tartags11 wrote...
Its not in the world class, its in an actor class.
There is no 'getObject' method in any greenfoot class.
davmac davmac

2013/11/6

#
Actually, I found another problem. Once again the getObject method is not known. It worked for Horse 1 (since it is in the horse1 actor class). But now its not working.
Are you saying that you have a getObject() method in your Horse1 class, but that the code you posted above is in a different class?
tartags11 tartags11

2013/11/7

#
Here is the full (and Updated) code. This is in the Horse1 actor class, not the world class. removeObject() which is a working method, does not recognize Horse2, Horse3, or Horse4, since they are in a list. Not sure how to get it to work any other way.
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
import greenfoot.*;
import java.util.List;
 
 
public class Horse1 extends Horse
    public void act()
    {
        move(Greenfoot.getRandomNumber(15));
        getLocation1();
    }
     
    public void getLocation1()
    {
        if ( getX() >= 975)
        {
            World world;
            world = getWorld();
            List<Horse2> Horse2 = getObjectsInRange(600, Horse2.class);
            List<Horse3> Horse3 = getObjectsInRange(600, Horse3.class);
            List<Horse4> Horse4 = getObjectsInRange(600, Horse4.class);
            world.removeObject(this);
            world.removeObject(Horse2.class);
            world.removeObject(Horse3.class);
            world.removeObject(Horse4.class);
        }
    }
}
Zamoht Zamoht

2013/11/7

#
Change the last four lines: world.removeObject(this); world.removeObject(Horse2.class); world.removeObject(Horse3.class); world.removeObject(Horse4.class); To this: world.removeObjects(Horse2); world.removeObjects(Horse3); world.removeObjects(Horse4); world.removeObject(this); This should work please correct me if I'm wrong.
tartags11 tartags11

2013/11/7

#
That Works! Thank You!!
You need to login to post a reply.