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

2017/4/25

move from level 2 to level 3

ali18 ali18

2017/4/25

#
Hello, i am creating a game, and i am unable to move from level 2 to level 3. The game moves from level 1 to 2, however, it doesn't move from level 2 to level 3 after all objects have been collected. I am using this code at the moment, however, the problem is that after level 2, it repeats level 2 again.
public void nextlevel2()
    {
        if (getWorld().getObjects(person.class).isEmpty())
        {
            level2 level2 =new level2();
            Greenfoot.setWorld(level2);
        }
    }
    public void nextlevel3()
    {
        if (getWorld().getObjects(person.class).isEmpty())
       {
            level3 level3 =new level3();
            Greenfoot.setWorld(level3);
       } 
    }
Super_Hippo Super_Hippo

2017/4/25

#
You can check in which world the object is currently in and then advance to the next one:
private void nextLevel()
{
    if (getWorld().getObjects(Person.class).isEmpty())
    {
        if (getWorld() instanceof Level1) Greenfoot.setWorld(new Level2());
        else if (getWorld() instanceof Level2) Greenfoot.setWorld(new Level3());
        else if (getWorld() instanceof Level3) Greenfoot.setWorld(new Level4());
        //...to be continued...
    }
}
Your class names should start with an uppercase letter.
ali18 ali18

2017/4/25

#
Thank you for your help, its all working outstandingly.
You need to login to post a reply.