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

2012/11/20

Remove object in range

Sl3nDeRm4n Sl3nDeRm4n

2012/11/20

#
Hi, how can I get a list of object within a range of 40 and save these objects as a variable or something? And then: how can I remove all these Objects?
SPower SPower

2012/11/20

#
Well, 40 will the the radius of a circle here:
getWorld().removeObjects(getObjectsInRange(40));
This code must be in a subclass of Actor. And really, you should read the greenfoot documentation!
Sl3nDeRm4n Sl3nDeRm4n

2012/11/20

#
my problem: if i do this i remove all Objects within the range. But I want to remove only the Objects of a specific class
SPower SPower

2012/11/20

#
Do this:
// at the top of the file:
import java.util.List;

// somewhere:
List objects = getObjectsInRange(40);
for (Object object : objects) {
    if (object instanceof ClassNameToRemove)
        getWorld().removeObject((Actor)object);
}
Sl3nDeRm4n Sl3nDeRm4n

2012/11/20

#
Doesnt work. 1. thing: I wrote
        List objects = getObjectsInRange(40);
and i got an error which said: method getObjectsInRange in class greenfoot.Actor cannot be applied to given types; requred: int, java.lang.Class found: int. so I wrote then
        List objects = getObjectsInRange(40, Blocker.class);
      
and the rest of code i copyed. After that i tried it but the Obect of the given cass does not remove. Weres my faut?
SPower SPower

2012/11/20

#
Remove the code and do this:
getWorld().removeObjects(getObjectsInRange(40, Blocker.class));
Sl3nDeRm4n Sl3nDeRm4n

2012/11/20

#
thx
You need to login to post a reply.