Hi,
I try to switch to another world using an ArrayList
This section of the code can be compiled but when my "Rocket" is colliding my "Exit", then the error pops up.
java.lang.IndexOutOfBoundsException: Index: 2, Size: 2
at java.util.ArrayList.rangeCheck(ArrayList.java:604)
at java.util.ArrayList.get(ArrayList.java:382)
at Exit.changeLevel(Exit.java:43)
at Exit.act(Exit.java:32)
This is the code of the method "changeLevel" in the class "Exit" which errored:
PS:
-"WorldManager" is a subclass of World. It's function is to manage all the worlds. In the "WorldManager" is the
ArrayList<World> level (containing the 2 levels; LevelOne (ArrayList index 0) and LevelTwo (ArrayList index 1)
-isOfClass is a boolean in the "WorldManager"
private void changeLevel(Rocket tempRocket) { for (int i = 0; i <= WorldManager.level.size(); ++i) { if (WorldManager.isOfClass(WorldManager.level.get(i), getWorld().getClass())) { nextLevel = WorldManager.level.get(i+1); } } getWorld().removeObject(tempRocket); Greenfoot.setWorld(nextLevel);
public static boolean isOfClass(World w, Class c) { return w.getClass() == c; }