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

2013/1/11

what do i put in 'getobjectsinrange()'?

behappy2 behappy2

2013/1/11

#
i tried getobjectsinrange, with capitals, but i dont know what to but after it
davmac davmac

2013/1/11

#
The documentation says what parameters you need.
danpost danpost

2013/1/11

#
getObjectsInRange protected java.util.List getObjectsInRange(int radius, java.lang.Class cls) Return all objects within range 'radius' around this object. An object is within range if the distance between its centre and this object's centre is less than or equal to 'radius'. Parameters: radius - Radius of the circle (in cells) cls - Class of objects to look for (passing 'null' will find all objects). This is what you would see if you looked up the 'getObjectsInRange' method in the Actor API.
behappy2 behappy2

2013/1/11

#
please give an example, i dont realy understand
danpost danpost

2013/1/11

#
Example:
if (!getObjectsInRange(5, Worm.class).isEmpty())
{
    Worm worm = (Worm)getObjectsInRange(5, Worm.class).get(0);
// etc. }
Gevater_Tod4711 Gevater_Tod4711

2013/1/11

#
If you want a list of all objects near to you which are from the type Enemy (or any other class) you do this:
List<Enemy> enemies = getObjectsInRange(50, Enemy.class);//now 50 is the radius and Enemy.class is the class you are searching for;
The first part just is the type (java.util.List) and the name of the variable
danpost danpost

2013/1/11

#
If using a statement similar to the code Gevater_Tod4711 supplied, you must add the following at the top of the class:
import java.util.List;
Also, you need to cast the list as follows:
List<Enemy> enemies = (List<Enemy>)getObjectsInRange(50, Enemy.class);
Gevater_Tod4711 Gevater_Tod4711

2013/1/11

#
Oh yes I forgot the import. But do I realy have to cast the list? I think it should work this way.
danpost danpost

2013/1/11

#
@Gevater_Tod4711, it appears you are correct about not having to cast the list; tested and confirmed!
A2O A2O

2016/9/12

#
how do I access a particular object from the list tho?
You need to login to post a reply.