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

2014/2/4

Switching to the next level

BGY12C BGY12C

2014/2/4

#
Hey, I used one of the examples on this site to switch from the current to the next level. Our first problem is that greenfoot doesn't load the first world but it loads the second at start. So we have to start at level2 thats a little problem but our other problem is that we can switch from the current world to the next (from 2 to 3) but than it loads everytime level 3.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
if (getX() == getWorld().getWidth()-1) {
    switch(level){
        case 1:
        level = 2;
        getWorld().removeObject(this);
        Greenfoot.setWorld(new Level2());
        break;
        case 2:
        level = 3;
        getWorld().removeObject(this);
        Greenfoot.setWorld(new Level3());
        break;
        case 3:
        level = 4;
        getWorld().removeObject(this);
        Greenfoot.setWorld(new Level4());
        break;
    }
}
Thanks
danpost danpost

2014/2/4

#
Check the value of 'level' when Level3 is up. Right-click on world anywhere there is no actor (mouse cursor should be an arrow) and select 'Inspect' on the pop-up menu.
BGY12C BGY12C

2014/2/4

#
Thanks, but it doesn't change the level variable, in lvl 3 it's 2 but it should be 3.
danpost danpost

2014/2/4

#
Please show the Level3 constructor ('public Level3()'). Actually, show the entire class. To have your scenario start back at Level1, right click on the Level1 World icon (rectangular button in frame on right side of window) and select 'new Level1()' from the options list. Instantiating a new world manually sets it as the initial world to start with.
danpost danpost

2014/2/4

#
You could just remove the 'level' field altogether and use the following for your code above:
1
2
3
4
5
6
7
8
9
if (getX() == getWorld().getWidth()-1)
{
    switch("123".indexOf(getWorld().getClass().getName().charAt(5)))
    {
        case 0: Greenfoot.setWorld(new Level2()); break;
        case 1: Greenfoot.setWorld(new Level3()); break;
        case 2: Greenfoot.setWorld(new Level4()); break;
    }
}
There is no reason to remove 'this' from the current world when changing worlds. The current world (which becomes the old world) and everything in it will be flagged for garbage collection when the new world takes over.
danpost danpost

2014/2/4

#
Another way is this:
1
2
3
4
5
6
if (getX() == getWorld().getWidth()-1)
{
    if (getWorld() instanceof Level1) Greenfoot.setWorld(new Level2());
    if (getWorld() instanceof Level2) Greenfoot.setWorld(new Level3());
    if (getWorld().instanceof Level3) Greenfoot.setWorld(new Level4());
}
BGY12C BGY12C

2014/2/5

#
Thank you from my whole team, the first way works great :D
You need to login to post a reply.