I made some function to go from a level to another but the way I made it I can't have more than two levels.. How can I adjust my code to make it work for an infinite number of levels?
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class PlayUniverse here. * * @author (your name) * @version (a version number or a date) */ public class PlayUniverse extends WorldOption { /** * Constructor for objects of class PlayUniverse. * */ public PlayUniverse() { } public void act() { createLevel(); } public void createLevel() { if (currentLevel == "noGameStarted" && gameRunning == false) { currentLevel = "level1"; gameRunning = true; addObject( new Hero(),100,380); } if (checkForNextLevel() == true && gameRunning == false) { currentLevel = "level2"; } if (checkForNextLevel() == true && gameRunning == true) { currentLevel = "level1"; } if (currentLevel == "level1" && gameRunning == true) { addObject( new Enemy("enemy1.png",1,2,10),1000,380); addObject( new Enemy("enemy1.png",1,2,10),1000,500); addObject( new Enemy("enemy1.png",1,2,10),1000,600); addObject( new Enemy("enemy1.png",1,2,10),1000,700); addObject( new Enemy("enemy1.png",1,2,10),1000,100); addObject( new Enemy("enemy1.png",1,2,10),1000,200); addObject( new Enemy("enemy1.png",1,2,10),850,380); addObject( new Enemy("enemy1.png",1,2,10),850,500); addObject( new Enemy("enemy1.png",1,2,10),850,600); addObject( new Enemy("enemy1.png",1,2,10),850,700); addObject( new Enemy("enemy1.png",1,2,10),850,100); addObject( new Enemy("enemy1.png",1,2,10),850,200); gameRunning = false; } if (currentLevel == "level2" && gameRunning == false) { addObject( new Enemy("enemy1.png",1,2,10),1000,380); addObject( new Enemy("enemy1.png",1,2,10),1000,500); addObject( new Enemy("enemy1.png",1,2,10),1000,600); addObject( new Enemy("enemy1.png",1,2,10),1000,700); addObject( new Enemy("enemy1.png",1,2,10),1000,100); addObject( new Enemy("enemy1.png",1,2,10),1000,200); addObject( new Enemy("enemy1.png",1,2,10),800,380); addObject( new Enemy("enemy1.png",1,2,10),800,500); addObject( new Enemy("enemy1.png",1,2,10),800,600); addObject( new Enemy("enemy1.png",1,2,10),800,700); addObject( new Enemy("enemy1.png",1,2,10),800,100); addObject( new Enemy("enemy1.png",1,2,10),800,200); addObject( new Enemy("enemy2.png",2,4,10),1040,420); addObject( new Enemy("enemy2.png",2,4,10),1000,535); addObject( new Enemy("enemy2.png",2,4,10),1080,620); addObject( new Enemy("enemy2.png",2,4,10),1000,700); addObject( new Enemy("enemy2.png",2,4,10),1000,100); addObject( new Enemy("enemy2.png",2,4,10),1000,200); gameRunning = true; if (checkForNextLevel() == true) { currentLevel = "level1"; } } } public boolean checkForNextLevel() { int numberOfEnemies = getObjects( Enemy.class ).size(); if (numberOfEnemies == 0) { return true; } else { return false; } } }