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

2012/3/17

How do i use java.util.List's

1
2
IsaacIIV IsaacIIV

2012/3/17

#
hello, i'm fairly new to Greenfoot and haven't used lists before. Could someone please tell me how to use the method 'getObjectsInRange(int radius, java.lang.Class cls)' and how to use the output - maybe some sort of actor array ?
danpost danpost

2012/3/17

#
If you are going throught the "list" to do specific things at that time, you could use
for ((class) a : getObjectsInRange(radius, class))
{
    // code to execute
}
where 'class' is changed to whatever actor class you wish to cast each item in the 'list' that getObjectsInRange returns (or whatever class you specify in that method). Note: for just re-locating, or removing the items in the returned list 'Object' can be used in place of 'class'. (in other words, they would not have to be cast to the specific actor). Hope this helps. If not, please post back with more specifics on what you need.
IsaacIIV IsaacIIV

2012/3/17

#
Thanks but i'm still a little confused. I'm trying to get a single actor in range that is an instance on an actor called 'enemy' and i'm not sure what to put instead of 'class'?
kiarocks kiarocks

2012/3/17

#
for(Enemy e : getObjectsInRange(10,Enemy.class)
{

}
danpost danpost

2012/3/17

#
If you are just checking to see in one is in range, use
if (!getObjectsInRange(10, Enemy.class).isEmpty())
If you need a reference to an Enemy class object in range, continue with
{
    Enemy enemy = getObjectsInRange(10, Enemy.class).get(0);
    // do stuff with 'enemy'
}
IsaacIIV IsaacIIV

2012/3/17

#
I tried 'Enemy enemy = getObjectsInRange(10, Enemy.class).get(0); ' but i got an error 'incompatible types - found java.lang.Object but expected Enemy' ?
Duta Duta

2012/3/17

#
IsaacIIV wrote...
I tried 'Enemy enemy = getObjectsInRange(10, Enemy.class).get(0); ' but i got an error 'incompatible types - found java.lang.Object but expected Enemy' ?
Do this:
Enemy enemy = (Enemy) getObjectsInRange(10, Enemy.class).get(0);
The (Enemy) in brackets is known as a cast and what it is doing is turning getObjectsInRange(10, Enemy.class).get(0) (which is a reference on an Object, which is a class all objects inherit from) into an Enemy
IsaacIIV IsaacIIV

2012/3/17

#
Thanks, thats working great. Is there a way of checking if there is an object in range as i am getting Index out of Bounds errors?
danpost danpost

2012/3/17

#
That error is probably due to the fact that no enemies where found in range and trying to get an item from an empty list will throw that error. If you first check to see if the list 'isEmpty()' as I said above, and then, if not empty, get your reference, you will avoid the error.
IsaacIIV IsaacIIV

2012/3/17

#
THANK YOU ALL - code is working great :)
davmac davmac

2012/3/18

#
Best practice solution is to do the query only once and save the result in a variable:
import java.util.List; // at the top of the source file!

...

        List<Enemy> l = getObjectsInRange(10, Enemy.class);
        if (! l.isEmpty()) {
            Enemy e = l.get(0);
            // ...
        }
IsaacIIV IsaacIIV

2012/3/18

#
How do i cast 'getObjectsInRange(10, Enemy.class)' into an Enemy in a loop ? for (Enemy a : getObjectsInRange(10, Enemy.class)) { // code to execute }
TheNightStrider TheNightStrider

2012/3/18

#
IsaacIIV wrote...
How do i cast 'getObjectsInRange(10, Enemy.class)' into an Enemy in a loop ? for (Enemy a : getObjectsInRange(10, Enemy.class)) { // code to execute }
I am not quite sure what exactly you are trying to do. Using that List<Enemy> l = getObjectsInRange(10, Enemy.class); if(!l.isEmpty()){ for(int i=0;i<=(l.size());i++){ Enemy e = l.get(i); //do stuff with enemy e! }} I think that is what you need to do. (Hopefully I have written that out right!). If you need any more info, check out this link HERE This assumes a lists index starts at 0, not 1.
davmac davmac

2012/3/18

#
How do i cast 'getObjectsInRange(10, Enemy.class)' into an Enemy in a loop ?
for (Enemy a : (List<Enemy>) getObjectsInRange(10, Enemy.class)) {
    // code to execute
}
IsaacIIV IsaacIIV

2012/3/18

#
Thanks Davmac - thats exactly what i wanted.
There are more replies on the next page.
1
2