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

2015/8/11

Messing around with multiple worlds....

WG95 WG95

2015/8/11

#
Hello, I am fairly new to Greenfoot, and I'm trying to set up a platformer where the player can walk through a door to get to the next World, or level. What I've done is I've made a World array containing all the levels in the game (currently 2), and then tried to switch between them by using this code:
public void levelUp()
    {
        Actor door = getOneObjectAtOffset(0, 0, Door.class);
        if (door != null)
        {
            World world = getWorld();
            int currentLevel = world.getLevel();
            if (currentLevel != LEVEL_COUNT)
            {
                Greenfoot.setWorld(levelArray[currentLevel + 1]);
            }
            else
            {
                victory();
            }
        }
    }
What happens, though, is that it claims it can't find the method getLevel, which is a public method in all my (2) World calsses, Level1 and Level2. This despite the fact that I've used the getWorld() method to access the current World... I get the feeling htere's something obvious I'm missing here. Please help!
Super_Hippo Super_Hippo

2015/8/11

#
Your 'world' variable holds a 'World' object. The 'World' class does not have a 'getLevel' method. There are some ways to work with this. For example you could arrange your classes like this: -World ---Levels -----Level1 -----Level2 Then you can put the 'getLevel' method into the 'Levels' class and replace lines 6 and 7 with this line:
int currentLevel = ((Levels) getWorld()).getLevel();
Another way is to have all levels in one class. It depends on the differences between the levels if this could be a good idea. If (almost) only the creation of the world is different, then it is worth a try to keep it in one class code. With this, it is much easier to create new levels.
You need to login to post a reply.