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

2011/8/1

getNeighbours()?

nooby123 nooby123

2011/8/1

#
How and what do you use it for?
davmac davmac

2011/8/2

#
As it says in the documentation:
Return the neighbours to this object within a given distance. This method considers only logical location, ignoring extent of the image. Thus, it is most useful in scenarios where objects are contained in a single cell. All cells that can be reached in the number of steps given in 'distance' from this object are considered. Steps may be only in the four main directions, or may include diagonal steps, depending on the 'diagonal' parameter. Thus, a distance/diagonal specification of (1,false) will inspect four cells, (1,true) will inspect eight cells.
nooby123 nooby123

2011/8/2

#
Yes, but how do you use it?
davmac davmac

2011/8/2

#
You call it in the same way you call any method. The return is a List; you should add an import statement for List at the top of your class:
import java.util.List;
Then you can do:
List neighbours = getNeighbours(1, true, null);
You can check if a list is empty using "isEmpty()":
if (neighbours.isEmpty()) {
    // do something - code here
}
You can do something for each actor in the list using a "for" loop:
for (Object o : l) {
    Actor a = (Actor) o;
    // do something here!
}
... and so on.
You need to login to post a reply.