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...)
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);
}
}
}
