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

2012/11/3

Removing actors when something else is eaten.

hades530 hades530

2012/11/3

#
Hello, I am relatively new to this and I had a problem while programming. I want one of my actors "A" to eat another actor "B", which will result in the disappearance of a third actor "C" as well. How do I do this? Thank you
danpost danpost

2012/11/3

#
There is really not much information given to go on, here. No code snippets. No scenario background info. Is there one, and only one, "C" object in the world? If not, is there a reference to the "C" object that should be removed?
Gevater_Tod4711 Gevater_Tod4711

2012/11/3

#
Well if there is just one actor 'C' it's easy:
//in the same method which eats actor b;
List<C> cObjects = getWorld().getObjects(C.class);
if (!cObject.isEmpty()) {
    getWorld().removeObject(cObjects.get(0));
}
But if there are more than one objects from typ 'C' it's a litle difficult to find the writh one. If you want to remove any of them you can use the same code than abouve but if you want to remove one specivic you must have a variable in the class 'C' which is never the same in an object. (the easyest way is to count them). So if you want to remove the 5th of them:
//in the same method which eats actor b;
public void removeObjectC(int objectsNumber) {
    List<C> cObjects = getWorld().getObjects(C.class);
    if (!cObjects.isEmpty()) {
        for (int i = 0; i < cObjects.size(); i++) {
            if (cObjects.get(i).getNumber() == objectsNumber) {//get number has to be declared in class C and must return the value of the objects counter;
                getWorld().removeObject(cObjects.get(i));
            }
        }
    }
}
Gevater_Tod4711 Gevater_Tod4711

2012/11/3

#
I forgot to say: you have to import java.util.List.
hades530 hades530

2012/11/3

#
thank you!
danpost danpost

2012/11/3

#
If there is only one "C" object in the world then
getWorld().removeObjects(getWorld().getObjects(C.class));
by itself will work. No error will ensue if the list from 'getObjects' is empty. That, and you do not have to 'import java.util.List;'
Gevater_Tod4711 Gevater_Tod4711

2012/11/4

#
@danpost yes that would also work without the if clause but I don't have to import java.util.List??? Did you mean using your code? Because if I use code like this:
public void removeObjectC(int objectsNumber) {  
    List<C> cObjects = getWorld().getObjects(C.class); 
...
}
I always get an error if I don't import it.
danpost danpost

2012/11/4

#
Yes, using the code I provided above, the List class does not need to be imported. With your code, which creates and stores a List object, you will have to import it. I use the following code (simplified, of course) all the time without having to import java.util.List:
for (Object obj : getObjects(ClassName.class))
{
    ClassName classObj= (ClassName) obj;
    classObj.classNameMethod();
}
As long as you do not have a reference to the List object created (that is, it is not stored locally or globally in your scenario) you do not need to import the class.
You need to login to post a reply.