I have five Actors. I want to make it so when two of them collide (two cars), everything stops, except the Ambulance Actor. I made a variable (stopAll) to make everything but the Ambulance stop when the variable is true (when the cars collide it becomes true). Can someone please try to help. My teacher tried with me and we couldn't figure it out.
Here is the code for carWorld (the place where the variable was created).
Here is the code for Car (the controlled actor)
Here is the code for Vehicle (the thing car collides with)
Here is the code for the Ambulance (this is supposed to go to the Car after the collision and stop
Here is the code for the Line (which just makes the illusion of the Car moving)
All I need is to find out why the program is giving us an error.
public class CarWorld extends World
{
/**
* Constructor for objects of class CarWorld.
*
*/
private boolean stopAll;
private int counter;
public CarWorld()
{
// Create a new world with 600x600 cells with a cell size of 1x1 pixels.
super(600, 600, 1);
setPaintOrder (Ambulance.class, Car.class, Vehicle.class);
addObject(new Car(),305,550);
addObject(new Line(),300,0);
addObject(new Line(),300,120);
addObject(new Line(),300,240);
addObject(new Line(),300,360);
addObject(new Line(),300,480);
addObject(new Line(),300,600);
addObject(new Line(),300,720);
stopAll=false;
}
public void act()
{
if (Greenfoot.getRandomNumber(100) < 1)
{
addObject(new Vehicle(), Greenfoot.getRandomNumber(200)+200, 0);
}
if (counter == 60)
{
addObject(new Line(),300,0);
counter=0;
}
counter++;
}
public void setStopAll(boolean flag){
stopAll=flag;
}
public boolean getStopAll(){
return stopAll;
}
}
public class Car extends Actor
{
/**
* Act - do whatever the Car wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
private boolean collideFlag;
CarWorld carWorldObj = new CarWorld();
public Car() // construtor to initialize private variables
{
collideFlag=false;
}
public void act()
{
if (!carWorldObj.getStopAll())
{
if (Greenfoot.isKeyDown("right"))
{
moveRight();
}
if (Greenfoot.isKeyDown("left"))
{
moveLeft();
}
if (Greenfoot.isKeyDown("up"))
{
moveUp();
}
if (Greenfoot.isKeyDown("down"))
{
moveDown();
}
}
if (!collideFlag)
checkCollision();
}
public void moveRight()
{
setLocation( getX()+2, getY() );
}
public void moveLeft()
{
setLocation( getX()-2, getY() );
}
public void moveUp()
{
setLocation( getX(), getY()-2 );
}
public void moveDown()
{
setLocation( getX(), getY()+2 );
}
public void checkCollision()
{
Actor collided;
collided = getOneIntersectingObject(Vehicle.class);
if (collided != null)
{
getWorld().addObject(new Ambulance(), 300, 724);
Greenfoot.playSound("Explosion.wav");
collideFlag=true;
carWorldObj.setStopAll(true);
}
}
}public class Vehicle extends Actor
{
/**
* Act - do whatever the Vehicle wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
int x = Greenfoot.getRandomNumber(3)+1;
public void act()
{
moveDown();
if (getY()>= getWorld().getHeight()-1)
{
getWorld().removeObject(this);
}
}
public void moveDown()
{
setLocation( getX(), getY()+(x) );
}
}public class Ambulance extends Actor
{
public void addedToWorld(World world)
{
if(!getWorld().getObjects(Car.class).isEmpty())
{
Actor car = (Actor)(getWorld().getObjects(Car.class).get(0));
turnTowards(car.getX(),car.getY());
}
}
/**
* 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()
{
move(4);
}
public void checkCollision()
{
Actor collided;
collided = getOneIntersectingObject(Car.class);
if (collided != null)
{
Greenfoot.stop();
}
}
}public class Line extends Actor
{
/**
* Act - do whatever the Line wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
CarWorld carWorldObj = new CarWorld();
public void act()
{
if (!carWorldObj.getStopAll())
{
moveDown();
}
if (getY()>= getWorld().getHeight()-1)
{
getWorld().removeObject(this);
}
}
public void moveDown()
{
setLocation( getX(), getY()+2 );
}
}


