Please help, I was trying to start my game once I click on a specific Object but it seems like it doesn't work..
This is my code for the world the necessary coding is in public void act():
The error message looks like this:
Greetings
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
public class PlantsVsZombies extends World
{
int Points = 200;
int spawntime1 = 800+ Greenfoot.getRandomNumber(601);
int spawntime2 = 800+ Greenfoot.getRandomNumber(602);
int spawntime3 = 800+ Greenfoot.getRandomNumber(603);
int spawntime4 = 800+ Greenfoot.getRandomNumber(604);
int spawntime5 = 800+ Greenfoot.getRandomNumber(605);
static boolean spawn = false;
public static boolean getSpawn()
{
return spawn;
}
/**
* Constructor for objects of class MyWorld.
*
*/
public PlantsVsZombies()
{
// Create a new world with 600x400 cells with a cell size of 1x1 pixels.
super(1400, 600,1);
setBackground("BackGround.jpg");
prepare();
}
public void act()
{
showText(""+spawntime1,700,300);
Preparation prep = getObjects(Preparation.class).get(0);
if (!getObjects(Preparation.class).isEmpty()
&& Greenfoot.mousePressed(prep))
{
spawn = true;
}
if (spawn == true)
{
spawntime1--;
spawntime2--;
spawntime3--;
spawntime4--;
spawntime5--;
}
spawnlane1();
spawnlane2();
spawnlane3();
spawnlane4();
spawnlane5();
}
/**
* Prepare the world for the start of the program.
* That is: create the initial objects and add them to the world.
*/
private void prepare(){
Bar bar = new Bar();
addObject(bar,94,302);
bar.setLocation(64,238);
bar.setLocation(91,300);
bar.setLocation(91,300);
Mower mower = new Mower();
addObject(mower,270,136);
mower.setLocation(214,143);
Mower mower2 = new Mower();
addObject(mower2,214,217);
Mower mower3 = new Mower();
addObject(mower3,214,315);
Mower mower4 = new Mower();
addObject(mower4,214,403);
Mower mower5 = new Mower();
addObject(mower5,214,506);
mower5.setLocation(212,512);
mower4.setLocation(215,410);
Green green = new Green();
addObject(green,117,127);
Sun sun = new Sun();
addObject(sun,115,234);
Points points = new Points();
addObject(points,58,40);
showText("= "+ Points,120,40);
Preparation preparation = new Preparation();
addObject(preparation,1255,49);
}
private void spawnlane1()
{
if (spawntime1 < 0)
{
addObject(new Zombie(),1400,135);
spawntime1 = 800+ Greenfoot.getRandomNumber(600);
}
}
private void spawnlane2()
{
if (spawntime2 < 0)
{
addObject(new Zombie(),1400,217);
spawntime2 = 800+ Greenfoot.getRandomNumber(600);
}
}
private void spawnlane3()
{
if (spawntime3 < 0)
{
addObject(new Zombie(),1400,315);
spawntime3 = 800+ Greenfoot.getRandomNumber(600);
}
}
private void spawnlane4()
{
if (spawntime4 < 0)
{
addObject(new Zombie(),1400,410);
spawntime4 = 800+ Greenfoot.getRandomNumber(600);
}
}
private void spawnlane5()
{
if (spawntime5 < 0)
{
addObject(new Zombie(),1400,512);
spawntime5 = 800+ Greenfoot.getRandomNumber(600);
}
}
}
java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 at java.util.ArrayList.rangeCheck(ArrayList.java:657) at java.util.ArrayList.get(ArrayList.java:433) at PlantsVsZombies.act(PlantsVsZombies.java:37) at greenfoot.core.Simulation.actWorld(Simulation.java:573) at greenfoot.core.Simulation.runOneLoop(Simulation.java:506) at greenfoot.core.Simulation.runContent(Simulation.java:193) at greenfoot.core.Simulation.run(Simulation.java:183) java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 at java.util.ArrayList.rangeCheck(ArrayList.java:657) at java.util.ArrayList.get(ArrayList.java:433) at PlantsVsZombies.act(PlantsVsZombies.java:37) at greenfoot.core.Simulation.actWorld(Simulation.java:573) at greenfoot.core.Simulation.runOneLoop(Simulation.java:506) at greenfoot.core.Simulation.runContent(Simulation.java:193) at greenfoot.core.Simulation.run(Simulation.java:183) java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 at java.util.ArrayList.rangeCheck(ArrayList.java:657) at java.util.ArrayList.get(ArrayList.java:433) at PlantsVsZombies.act(PlantsVsZombies.java:37) at greenfoot.core.Simulation.actWorld(Simulation.java:573) at greenfoot.core.Simulation.runOneLoop(Simulation.java:506) at greenfoot.core.Simulation.runContent(Simulation.java:193) at greenfoot.core.Simulation.run(Simulation.java:183) java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 at java.util.ArrayList.rangeCheck(ArrayList.java:657) at java.util.ArrayList.get(ArrayList.java:433) at PlantsVsZombies.act(PlantsVsZombies.java:37) at greenfoot.core.Simulation.actWorld(Simulation.java:573) at greenfoot.core.Simulation.runOneLoop(Simulation.java:506) at greenfoot.core.Simulation.runContent(Simulation.java:193) at greenfoot.core.Simulation.run(Simulation.java:183) java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 at java.util.ArrayList.rangeCheck(ArrayList.java:657) at java.util.ArrayList.get(ArrayList.java:433) at PlantsVsZombies.act(PlantsVsZombies.java:37) at greenfoot.core.Simulation.actWorld(Simulation.java:573) at greenfoot.core.Simulation.runOneLoop(Simulation.java:506) at greenfoot.core.Simulation.runContent(Simulation.java:193) at greenfoot.core.Simulation.run(Simulation.java:183) java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 at java.util.ArrayList.rangeCheck(ArrayList.java:657) at java.util.ArrayList.get(ArrayList.java:433) at PlantsVsZombies.act(PlantsVsZombies.java:37) at greenfoot.core.Simulation.actWorld(Simulation.java:573) at greenfoot.core.Simulation.runOneLoop(Simulation.java:506) at greenfoot.core.Simulation.runContent(Simulation.java:193) at greenfoot.core.Simulation.run(Simulation.java:183) java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 at java.util.ArrayList.rangeCheck(ArrayList.java:657) at java.util.ArrayList.get(ArrayList.java:433) at PlantsVsZombies.act(PlantsVsZombies.java:37) at greenfoot.core.Simulation.actWorld(Simulation.java:573) at greenfoot.core.Simulation.runOneLoop(Simulation.java:506) at greenfoot.core.Simulation.runContent(Simulation.java:193) at greenfoot.core.Simulation.run(Simulation.java:183) java.lang.NullPointerException at Preparation.act(Preparation.java:24) at greenfoot.core.Simulation.actActor(Simulation.java:567) at greenfoot.core.Simulation.runOneLoop(Simulation.java:530) at greenfoot.core.Simulation.runContent(Simulation.java:193) at greenfoot.core.Simulation.run(Simulation.java:183) java.lang.NullPointerException at Preparation.act(Preparation.java:25) at greenfoot.core.Simulation.actActor(Simulation.java:567) at greenfoot.core.Simulation.runOneLoop(Simulation.java:530) at greenfoot.core.Simulation.runContent(Simulation.java:193) at greenfoot.core.Simulation.run(Simulation.java:183) java.lang.NullPointerException at Preparation.act(Preparation.java:25) at greenfoot.core.Simulation.actActor(Simulation.java:567) at greenfoot.core.Simulation.runOneLoop(Simulation.java:530) at greenfoot.core.Simulation.runContent(Simulation.java:193) at greenfoot.core.Simulation.run(Simulation.java:183) java.lang.NullPointerException at Preparation.act(Preparation.java:27) at greenfoot.core.Simulation.actActor(Simulation.java:567) at greenfoot.core.Simulation.runOneLoop(Simulation.java:530) at greenfoot.core.Simulation.runContent(Simulation.java:193) at greenfoot.core.Simulation.run(Simulation.java:183) java.lang.NullPointerException at Preparation.act(Preparation.java:27) at greenfoot.core.Simulation.actActor(Simulation.java:567) at greenfoot.core.Simulation.runOneLoop(Simulation.java:530) at greenfoot.core.Simulation.runContent(Simulation.java:193) at greenfoot.core.Simulation.run(Simulation.java:183) java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 at java.util.ArrayList.rangeCheck(ArrayList.java:657) at java.util.ArrayList.get(ArrayList.java:433) at PlantsVsZombies.act(PlantsVsZombies.java:37) at greenfoot.core.Simulation.actWorld(Simulation.java:573) at greenfoot.core.Simulation.runOneLoop(Simulation.java:506) at greenfoot.core.Simulation.runContent(Simulation.java:193) at greenfoot.core.Simulation.run(Simulation.java:183)
