The Crash explosion object is not working as I intend. What I want it to do is when the Ambulance collides with a car or tree I want the Crash image to grow gradually 12 sizes and stop the game, showing a large, size 12 Crash image object on the screen.
It currently grows and shrinks the Crash image (I want it to grow but not shrink again). Also, for some unknown reason to me, as the image grows, it places the newer, larger image below the previous one. It should be placing the newer, larger image on top of the the previous smaller image.
It's also giving this error:
java.lang.ArrayIndexOutOfBoundsException: 12
at Crash.act(Crash.java:45)
Your help and insights are much appreciated. Thank you!
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* The image that appears after a collision.
*
* @author Poul Henriksen
* @author Gabby Macias
* @version xx-xx-xxxx
*/
public class Crash extends Actor
{
//Images to be used in the crash scene
private final static int IMAGE_COUNT= 12;
/**
* A variable to hold the 12 images in an array.
* This is static so the images are not recreated
* for every object.
*/
private static GreenfootImage[] images;
//Start with image size 0
private int imageNo = 0;
//Increases image size by 1
private int increment = 1;
/**
* Constructor for the crash.
*/
public Crash()
{
initializeImages();
setImage(images[0]);
//Greenfoot.playSound("MetalExplosion.wav");
}
/**
* += means take the variable before + current value
* and add what is on the right of the equals sign
* to the current value of what is before the + sign.
*/
public void act()
{
setImage(images[imageNo]);
imageNo += increment;
if(imageNo >= IMAGE_COUNT)
{
increment = -increment;
imageNo += increment;
}
if(imageNo < 0)
{
getWorld().removeObject(this);
}
}
/**
* This method uses a for-loop to create the various
* sized images of the crash image when the images
* variable is not empty.
*/
public synchronized static void initializeImages()
{
if(images == null)
{
GreenfootImage baseImage = new GreenfootImage("Crash.png");
images = new GreenfootImage[IMAGE_COUNT];
for (int i = 0; i < IMAGE_COUNT; i++)
{
int size = (i+1) * ( baseImage.getWidth() / IMAGE_COUNT );
images[i] = new GreenfootImage(baseImage);
images[i].scale(size, size);
}
}
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Ambulance here.
*
* @author (your name)
* @version (a version number or a date)
*/
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();
}
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"))
{
}
else
{
setRotation(0);
}
}
public void collision()
{
if (isTouching (NorthCar.class))
{
getWorld().addObject (new Crash(), getX(), getY());
}
if (isTouching (SouthCar.class))
{
getWorld().addObject (new Crash(), getX(), getY());
}
if (isTouching (Trees.class))
{
getWorld().addObject (new Crash(), getX(), getY());
}
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Highway class
* This is the world for the Highway scenario, a simulation of a highway.
*
* @author Gabby Macias
* @version xx-xx-xxxx
*/
public class Highway extends World
{
private int counter = 0;
/**
* Constructor for objects of class Highway. Sets object order and adds objects to world.
*
*/
public Highway()
{
// Create a new world with 600x600 cells with a cell size of 1x1 pixels. Sets order of objects. Places objects.
super(600, 600, 1);
setPaintOrder (NorthCar.class, SouthCar.class, Ambulance.class, Divider.class);
addObject (new Ambulance(), 300, 550);
placeDividers();
placeLeftTrees();
placeRightTrees();
}
/**
* Act method calls the three methods to add new objects to the scenario.
*/
public void act()
{
addTrees();
addNorthCars();
addSouthCars();
}
/**
* This method places the initial dividers on the starting screen perfectly aligned.
*/
private void placeDividers()
{
int i = 0;
while (i < 601)
{
addObject (new Divider(), 300, i);
i = i + 50 + 50;
}
}
/**
* This method randomly places trees on the left side of the starting scene.
*/
private void placeLeftTrees()
{
int j = 0;
while (j < 7)
{
int x = Greenfoot.getRandomNumber(150);
int y = Greenfoot.getRandomNumber(600);
addObject (new Trees(), x, y);
j++;
}
}
/**
* This method randomly places trees on the right side of the starting scene.
*/
private void placeRightTrees()
{
int k = 0;
while (k < 5)
{
int x = Greenfoot.getRandomNumber(150) + 450;
int y = Greenfoot.getRandomNumber(600);
addObject (new Trees(), x, y);
k++;
}
}
/**
* This method randomly adds trees as the scene scrolls.
*/
public void addTrees()
{
if (Greenfoot.getRandomNumber(100) < 4)
{
addObject(new Trees(), Greenfoot.getRandomNumber(150), 0);
}
if (Greenfoot.getRandomNumber(100) < 4)
{
addObject(new Trees(), Greenfoot.getRandomNumber(150) + 450, 0);
}
}
/**
* This method adds red cars traveling up/north.
*/
public void addNorthCars()
{
if (counter > Greenfoot.getRandomNumber(1000) + 500)
{
counter = 0;
addObject(new NorthCar(), Greenfoot.getRandomNumber(90) + 320, 600);
}
counter++;
}
/**
* This method adds blue cars traveling down/south.
*/
public void addSouthCars()
{
if (counter > Greenfoot.getRandomNumber(1000) + 500)
{
counter = 0;
addObject(new SouthCar(), Greenfoot.getRandomNumber(80) + 200, 0);
}
counter++;
}
}
