Hello guys,
i have start to programming with Grennfoot and i have a Problem with my command. I would like to make an command that looks if there are buses in radius. When its so the cars should stop (speed ==0).


1 2 3 4 | if (getObjectsInRange( 50 /*radius to check*/ , Bus. class ).isEmpty()) //if there is no bus { //do something... move, turn and so on } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | public class Car extends Actor { private int speed = 1 ; public void act() { if (isBusInRange()) speed = 0 ; else speed = 1 ; } private boolean isBusInRange(){ return !getObjectsInRange( 40 , Bus. class ).isEmpty(); //finds buses within a 40 pixel radius } } |
1 | public car[]cars= new car[ 17 ]; |
1 2 3 4 5 6 7 8 | public void act() { if (getObjectsInRange( 50 , Bus. class ).isEmpty()) { move( 1 ); // you could perform some other actions here (like turning or "atEdge" or non-bus collision checking) } } |