How to switch between levels in a game

Hello guys,
I am working on a multi level game.
I created each level in a separate class.
Now, I want to start level 2 after level 1 ends
How can I do that?

Assuming you know how load level and you know your own conditions when level ends:

  1. unload first level
  2. load second level
  3. unload second level
  4. load third level
  5. etc.

you need provide more information, because im not sure what you need.

Thank you for your reply.
Actually, i use Nifty GUI for start menu
I want when Start button is pressed the start menu closed and level 1 starts.
But in screenController class i don’t know how to exit Nifty GUI and start level 1.
Because if i use start() function to start level 1 it will open new window and JME will crash
and if i can access level 1 scene node from screenController class and attached it to rootNode
It will get NullPointer exception because scene node is not initialized.

as i see main issues are with Nifty. Im not using Nifty, but you need:

  • find how to close Nifty GUI elements. (should be easy)
  • post here full NullPointer exception stackstrace (so other people will tell you how to solve “scene node is not initialized”)

based on second, you just need wait for it, did you follow some Nifty tutorial? or some tests?

Please note in main app everything should start from “simpleInitApp” method if you use SimpleApplication, so im not sure what start() function is for you. i belive you mean some level class method that i dont see. Some visible code would help us to help you.

btw.
Also please note, there is also Lemur GUI library that more developers seems to use here.
Also some use JavaFX.

Thank you a lot.
But excuse me I have one more question.
You told me to load first level then unload it then load second level and so on.
How to do that ?
How to deal with classes globally , load and unload them?

it all depends on developer. Each developer can use own way. Depends on how advanced game gonna be.

For example if this gonna be very simple game level loading then you can just do:

some pseudocode:

ArrayList<LevelManagingClass> levelList = new ArrayList<>();
levelList.add(level1);
levelList.add(level2);
levelList.add(level3);
...
int level = 0;

then when each level is done just 

levelList.get(level).unload(); //unload - your method to clear level
level++;
levelList.get(level).load(); //load - your method to load new level

where load and unload in this example means 3D Scene Graph and GUI too.

You can also use JME prepared Abstract classes like “BaseAppState” or “AbstractAppState” and use it in stateManager like stateManager.attach(someAppState); and stateManager.detach(someAppState);

But if game gonna be advanced, then it can be done in more proper way for more advanced level magaging.

Really there are thousand ways to solve this, you need choose one that match your game.

I searched before for “BaseAppState” and “AbstractAppState” classes but i do not excatly understand how to do that with them
Excuse me I am new with jmonkey engine.

here you go:
https://wiki.jmonkeyengine.org/jme3/advanced/application_states.html

nice explained.

but like i said, for level loading, you dont even need them :slight_smile: (i said “You can also” so i mean its alternative way)

There are many ways. You could use one screen and just reload it with new data- which means removing old models and setting up new ones. If you mean like techniques to switch between totally different screens use BaseAppState and attach/dettach or enable/disable them. Also check out this: