Problem in switching between two AbstractAppStates

hello guys,
I have created two classes that extend AbstractAppState, Main menu and LevelBuilder (each level extends LevelBuilder). The problem is that although the first time that LevelBuilder is attached it’s initialized fine, when I go back to main menu and try to load levelbuilder again, it shows nothing but a blue screen (because i’ve set the background color of viewport to blue).

I call stateManager only in simpleUpdate() in my Main class (Main class extends SimpleApplication) because I want to keep things simple for start. So I have this:

[java]
public void simpleUpdate(float tpf) {

    if(levelOne.isEscape()){
        levelOne.setEscape(false);
        stateManager.detach(levelOne);
        stateManager.attach(levelManager);
    }

    if(levelManager.isLevelOne()){
        levelManager.setLevelOne(false);
        stateManager.detach(levelManager);
        stateManager.attach(levelOne);

//LevelOne extends LevelBuilder
}
}
[/java]

in LevelBuilder I override cleanUp like this:

[java]
@Override
public void cleanup() {
super.cleanup();

    rootNode.detachAllChildren();
    viewPort.clearProcessors();
    viewPort.clearScenes();
   
}

[/java]

in Main menu I override cleanUp like this:

[java]
@Override
public void cleanup() {
super.cleanup();
guiNode.detachAllChildren();
nifty.unsubscribeScreen(screen);
guiViewPort.clearProcessors();
guiViewPort.clearScenes();

    app.getFlyByCamera().setDragToRotate(false);       
    
}

[/java]

So do I miss something when i override cleanUp() or is the problem somewhere else?

Step through in a debugger or put some printlns in. The problem is in your logic somewhere but we can’t see it from here.

I can assure you that attaching and detaching app states works just fine. It’s some other thing in your code that you’ve missed. For example, if the state in simpleUpdate hasn’t changed then you’ll be potentially detaching it again right away. (ie: if levelOne.isEscape() is still returning true.)

None of this we can see from here so it will be up to you to find the fault in your logic.

I ve run it in the debugger but I found nothing suspicious. Also when the blue screen is shown, the game is running but nothing is renderred. Should I look into the RenderManager? Maybe if I call clearQueue or something like that?

Also when I go back to main menu I have this warning:

WARNING: An event service by the name NiftyEventBusalready exists. Perhaps multiple threads tried to create a service about the same time?
org.bushe.swing.event.EventServiceExistsException: An event service by the name NiftyEventBusalready exists. Perhaps multiple threads tried to create a service about the same time?
at org.bushe.swing.event.EventServiceLocator.setEventService(EventServiceLocator.java:123)

Well, I can assure you that it is not a bug in JME. I have about a dozen different applications that all do this sort of thing and they work fine. So something in your code is doing something funny.

re: nifty, it sounds like you are initializing nifty twice. I don’t use nifty at all but I remember something about that you can only initialize it once per app.

I found the problem. I shouldn’t have called viewPort.clearScenes(); because it removes the rootNode from the viewPort. Also I now initialize the nifty in the SimpleApplication like you said so I don’t get any warnings. Thank you!