This site requires JavaScript, please enable it in your browser!
Greenfoot back
Zestix
Zestix wrote ...

2019/2/19

Error when trying to use Greenfoot.mouseClicked in World Class

Zestix Zestix

2019/2/19

#
Hey, I've been trying for quite a while now to make this work but it just doesn't want to! I tried accessing a class from the world class and create a condition where the game only starts working once I click on that specific class. When I do that though an error appears which doesnt let me start the game anymore! The code looks like this:
if (Greenfoot.mouseClicked(getObjects(Preparation.class).get(0)))
        {
            spawntime1--;
            spawntime2--;
            spawntime3--;
            spawntime4--;
            spawntime5--;
        }
And that's the error:
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:35)
	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)
Regards
danpost danpost

2019/2/20

#
Zestix wrote...
I tried accessing a class from the world class and create a condition where the game only starts working once I click on that specific class. When I do that though an error appears which doesnt let me start the game anymore! << Code Omitted >> << Error Trace Omitted
Apparently, there are times when there is no Preparation actor in the world. Therefore, you must check that there is one before trying to access one from the world:
if (!getObjects(Preparation.class).isEmpty() && Greenfoot.mouseClicked(getObjects(Preparation.class).get(0)))
Zestix Zestix

2019/2/20

#
danpost wrote...
Zestix wrote...
I tried accessing a class from the world class and create a condition where the game only starts working once I click on that specific class. When I do that though an error appears which doesnt let me start the game anymore! << Code Omitted >> << Error Trace Omitted
Apparently, there are times when there is no Preparation actor in the world. Therefore, you must check that there is one before trying to access one from the world:
if (!getObjects(Preparation.class).isEmpty() && Greenfoot.mouseClicked(getObjects(Preparation.class).get(0)))
Thank you very much
You need to login to post a reply.