In my highway scenario I have a two-lane highway with sand and trees on either side. I would like to program the vehicle so that it brings up a dust cloud object when it goes on the sand (X coordinates <= 185 and >= 430) and the dust cloud disappears when it gets back on the highway ( between coordinates 185 and 430). I thought that a while loop would work but I am sure I am not programming correctly since I am new at this. It compiles but gives an error when the vehicle touches the sand. This is what I have (don't laugh...)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 | public class Ambulance extends Actor { /** * Act - do whatever the Ambulance wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { drive(); collision(); makeDust(); } /** * This method keeps the ambulance's rotation to 0 * degrees unless the right key or left key is * pressed. If so, the ambulance will turn 10 * degrees and move in that direction. If the up or * down keys are pressed, the ambulance will move 1 * pixel in that direction. */ private void drive() { if (Greenfoot.isKeyDown( "right" )) { setRotation( 10 ); move( 1 ); } else if (Greenfoot.isKeyDown( "left" )) { setRotation ( 350 ); move(- 1 ); } else if (Greenfoot.isKeyDown( "up" )) { setLocation(getX(), getY()- 1 ); } else if (Greenfoot.isKeyDown( "down" )) { setLocation(getX(), getY()+ 1 ); } else { setRotation( 0 ); } } public void collision() { if (isTouching (NorthCar. class )) { getWorld().addObject ( new Crash(), getX(), getY()); removeTouching(NorthCar. class ); } if (isTouching (SouthCar. class )) { getWorld().addObject ( new Crash(), getX(), getY()); removeTouching(SouthCar. class ); } if (isTouching (Trees. class )) { getWorld().addObject ( new Crash(), getX(), getY()); removeTouching(Trees. class ); } } private void makeDust() { int i = getX(); while (i <= 185 ) { getWorld().addObject( new Cloud(), getX(), getY()+ 60 ); } while ( i >= 430 ) { getWorld().addObject( new Cloud(), getX(), getY() + 60 ); } } } |