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

2017/4/26

How to switch levels after its over?

kursatA kursatA

2017/4/26

#
I have created each level but i need to connect the levels together, for example after defeating/destroying the last enemy on the first level i want a code that allows the game to move onto the next screen. how do i do this? :)
danpost danpost

2017/4/26

#
Just thought of easy way to do that. Create a subclass of actor called 'LevelComplete' and when the level is complete, create and add one into the world. In each level world act method, place code similar to this (example is for a Level2 world):
1
if (!getObjects(LevelComplete.class).isEmpty()) Greenfoot.setWorld(new Level3());
kursatA kursatA

2017/4/26

#
thanks for this code but it doesn't work because i have objects flying towards the character and these objects cannot be destroyed you can only dodge them, so the level wont be 'empty' this creates a fault in the code.
danpost danpost

2017/4/26

#
kursatA wrote...
thanks for this code but it doesn't work because i have objects flying towards the character and these objects cannot be destroyed you can only dodge them, so the level wont be 'empty' this creates a fault in the code.
The code provided is only looking for a LevelComplete object. Having other objects in the world is inconsequential.
kursatA kursatA

2017/4/26

#
what im asking for is after completing one level it will automatically go to the next scene/screen that i have set, thanks for the code but i already have the 'level complete' object done
danpost danpost

2017/4/26

#
kursatA wrote...
what im asking for is after completing one level it will automatically go to the next scene/screen that i have set, thanks for the code but i already have the 'level complete' object done
Maybe you are misunderstanding what MY LevelComplete object is for. It is not used as a way to show the user that the level was completed or anything like that. It is only used as an indicator to your currently active world for when to proceed to the next level. Maybe I should have given it a name like 'NextLevelTime' or 'WorldInformer' instead of 'LevelComplete' to avoid any possible confusion.
kursatA kursatA

2017/4/26

#
ow alright now i understand the code, but now i need help on the bit where a 'level complete' sign pops up. i need this sign to pop up when all the enemies have been destroyed in the level. i have two types of enemies, 1 = "car1" and 2 = "car2". how will i code this?
danpost danpost

2017/4/26

#
kursatA wrote...
now i need help on the bit where a 'level complete' sign pops up. i need this sign to pop up when all the enemies have been destroyed in the level. i have two types of enemies, 1 = "car1" and 2 = "car2". how will i code this?
In you world act method, you could add the following line:
1
if (getObjects(car1..class).isEmpty() && getObjects(car2.class).isEmpty()) addObject(new WorldInformer(), 0, 0);
The thing is, I was expecting detection of level completed to happen in an Actor subclass, not in the world (usually, that is where beginners stumble; so, I was preuming that was the case here). However, since it is not the case, and the detection of level completed is done in the world level classes, you can remove the new Actor subclass (WorldInformer or whatever name it finally ended up with) and use the 'if' condition given here and setting the new world directly instead of adding the "sign" into the world.
kursatA kursatA

2017/4/27

#
thanks for that, it works i also used the first code but is there a way to add a time limit to the first code? so the user can see the levelcomplete sign for a couple of seconds.
danpost danpost

2017/4/27

#
You could probably just add a delay there:
1
2
3
4
5
if (!getObjects(LevelComplete.class).isEmpty())
{
    Greenfoot.delay(100);
    Greenfoot.setWorld(new Level3());
}
kursatA kursatA

2017/4/27

#
your a lifesaver :)
kursatA kursatA

2017/4/27

#
you're*
You need to login to post a reply.