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

2012/10/2

Worms move

DPD4AU DPD4AU

2012/10/2

#
If i want the worms to move only when the objects come into a certain radius, how could i make this a statement using getObjectsInRange() ?
danpost danpost

2012/10/2

#
Since 'getObjectsInRange(...)' returns a list object, you need to use a method of the List class. 'isEmpty()' or 'size()' would be good candidates for determining the current condition (are there any objects near the worm).
int radius = 50; // adjust to suit
if (!getObjectsInRange(radius, null).isEmpty())
{
    // code to move worm
}
// or:  if (getObjectsInRange(radius, null).size() != 0)...
DPD4AU DPD4AU

2012/10/2

#
Thanks dan!
davmac davmac

2012/10/2

#
Just a note: you should prefer "isEmpty()" to "size() != 0", because it is more descriptive, and it can be (much) more efficient - some types of list have to count their elements when you call size().
You need to login to post a reply.