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

2013/4/27

Creating new levels in infinite numbers.

Solringolt Solringolt

2013/4/27

#
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;
        }
    }
}
Gevater_Tod4711 Gevater_Tod4711

2013/4/27

#
I think the best way would be to use not a string but an int for the level number (instead of currentLevel == "noGameStarted" you just have currentLevel = 0 or any other number). That will make it more easy. Then you check whether there are no enemies in the world. If there are no you count up the level number and start the new level (using the level number: if (level == 1) { } else if (level == 2) ...)
Solringolt Solringolt

2013/4/28

#
Thx for the tips! I added also the checkForNextLevel() in the act method to make it more easy. It looks like that now:
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()
    {
       if (checkForNextLevel() == true)
       {
           createLevel();
       }
    }
    public void createLevel()
    {
       if (currentLevel == 0) 
       {
           addObject( new Hero(),100,380);
           if (checkForNextLevel() == true)
           {
               currentLevel = 1;
           }
           
       }
       else if (currentLevel == 1)
       {
           
           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);

           currentLevel = 2;          
           
       }
       
       else if (currentLevel == 2)
       {
           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);
           
           
           currentLevel = 1;
           
       }
    }
    public boolean checkForNextLevel()
    {
        int numberOfEnemies = getObjects( Enemy.class ).size();
        if (numberOfEnemies == 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}
You need to login to post a reply.