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

2018/11/14

Greenfoot Changing Levels - HELP!

Lewis12374 Lewis12374

2018/11/14

#
Hello, I need help with changing levels on my Greenfoot Game. The code works but it only changes one level and then stops working. Here is the code:
    int currentLevel = 1;

    public void changeLevel()
    {
        if (canSee(Emblem.class))
        {
            World thisLevel = getWorld();
            World newLevel = new Level1();
            if(currentLevel == 1)
            {
                newLevel = new Level1();
                currentLevel++;
            }
            if(currentLevel == 2)
            {
                newLevel = new Level2();
                currentLevel++;
            }
            if(currentLevel == 3)
            {
                newLevel = new Level3();
                currentLevel++;
            }
            Greenfoot.setWorld(newLevel);
        }
    }  
Super_Hippo Super_Hippo

2018/11/14

#
You have an object in a world. You set "currentLevel" to 1. When it touches the "Emblem", it's "currentLevel" will be 2. Now a new world is set, a new (=another) object of this class is added to the world (I guess) and its "currentLevel" is set to 1. If your worlds are similar, then use one class for them. Then you can do something like this:
//Level class
private int currentLevel;

public Level(int level)
{
    super(….);
    currentLevel = level;
   //create Level based on the variable
}

public void nextLevel()
{
    Greenfoot.setWorld(new Level(currentLevel+1));
}
//class in which your code currently is, in act method
if (isTouching(Emblem.class))
{
    ((Level) getWorld()).nextLevel());
}
Lewis12374 Lewis12374

2018/11/14

#
Cheers Hippo
Super_Hippo wrote...
You have an object in a world. You set "currentLevel" to 1. When it touches the "Emblem", it's "currentLevel" will be 2. Now a new world is set, a new (=another) object of this class is added to the world (I guess) and its "currentLevel" is set to 1. If your worlds are similar, then use one class for them. Then you can do something like this:
//Level class
private int currentLevel;

public Level(int level)
{
    super(….);
    currentLevel = level;
   //create Level based on the variable
}

public void nextLevel()
{
    Greenfoot.setWorld(new Level(currentLevel+1));
}
//class in which your code currently is, in act method
if (isTouching(Emblem.class))
{
    ((Level) getWorld()).nextLevel());
}
You need to login to post a reply.