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

2014/3/5

Help using a while loop and getObjectsInRange

kzb94 kzb94

2014/3/5

#
I'm trying to get the worms in my game to move away from the crabs when they're within a certain range. I have to use getObjectsInRange and a while loop. Here's my code: public class Worm extends Animal { public void act() { runFromCrab(); } public void runFromCrab() { while (getObjectsInRange(15, Crab.class)) { move(15); } } } I keep getting errors when I try to compile. Any advice?
danpost danpost

2014/3/5

#
The Actor class method 'getObjectsInRange' returns a List object. 'while' needs a boolean value (not a list) for its condition.
kzb94 kzb94

2014/3/5

#
How can I do a boolean with a while loop?
danpost danpost

2014/3/5

#
What true/false question can you ask about the list that is the determining factor in whether the worm runs or not? Check what methods are available in the List class API documentation.
You need to login to post a reply.