Switch AppStates with Nifty?

The app below is simple. Switch appstates when the gui button is pressed.

When the program starts the background color is yellow. When you press the button the background color should turn green.

Moreover, everything works when I switch using keyboard keys when creating another program. But for this program using nifty instead, it doesn’t work.

How am I supposed to switch appstates with Nifty?


Main.java

public class Main extends SimpleApplication implements ScreenController {
    private GameRunningState gameRunningState;
    private StartScreenState startScreenState;
    
    public static void main(String[] args) {
        AppSettings settings = new AppSettings(true);
        settings.setTitle("My Game");
        settings.setResolution(1280, 720);
        settings.setUseJoysticks(true);
        
        Main app = new Main();
        app.setSettings(settings);
        app.setShowSettings(false);
        app.setDisplayStatView(false);
        app.setDisplayFps(false);
        app.start();
    }

    @Override
    public void simpleInitApp() {

        inputManager.clearMappings();
        inputManager.setCursorVisible(true);
        getFlyByCamera().setEnabled(false);
                
        NiftyJmeDisplay niftyDisplay = new NiftyJmeDisplay(assetManager, inputManager, audioRenderer, viewPort);
        Nifty myNifty = niftyDisplay.getNifty();
        //nifty.setDebugOptionPanelColors(true);
        myNifty.addXml("Interface/Start.xml");
        myNifty.gotoScreen("start");
        guiViewPort.addProcessor(niftyDisplay);

        gameRunningState    = new GameRunningState();
        startScreenState    = new StartScreenState();

        stateManager.attach(startScreenState);
    }

    public void newGame()
    {
        stateManager.detach(startScreenState); // Remove Yellow
        stateManager.attach(gameRunningState); // Show Green
    }        
    
        @Override
    public void simpleUpdate(float tpf) {
    }
    @Override
    public void simpleRender(RenderManager rm) {
    }
    public void bind(Nifty nifty, Screen screen) {
    }
    public void onStartScreen() {
    }
    public void onEndScreen() {
    }
}

StartScreen AppState (Yellow color)

public class StartScreenState extends AbstractAppState {
  
    private SimpleApplication app;
    private ViewPort          viewPort;
    private final ColorRGBA backgroundColor = ColorRGBA.Yellow;   
 
    @Override
    public void initialize(AppStateManager stateManager, Application app) {
        super.initialize(stateManager, app);
        
        this.app = (SimpleApplication) app; // can cast Application to something more specific
        this.viewPort     = this.app.getViewPort();
        this.viewPort.setBackgroundColor(backgroundColor);
    }
 
    @Override
    public void update(float tpf) {
    } 

    @Override
    public void cleanup() {
      super.cleanup();
    }   
}

GameRunning AppState (Green Color)

public class GameRunningState extends AbstractAppState {
 
    private SimpleApplication app;
    private ViewPort          viewPort;
    private final ColorRGBA backgroundColor = ColorRGBA.Green;   
 
    @Override
    public void initialize(AppStateManager stateManager, Application app) {
        super.initialize(stateManager, app);
        
        this.app = (SimpleApplication) app; // can cast Application to something more specific
        this.viewPort     = this.app.getViewPort();
        this.viewPort.setBackgroundColor(backgroundColor);
    }
 
  @Override
  public void update(float tpf) {
  } 
  @Override
  public void cleanup() {
    super.cleanup();
  }
}

Start.xml file

<?xml version="1.0" encoding="UTF-8"?>
<nifty xmlns="http://nifty-gui.sourceforge.net/nifty-1.3.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://nifty-gui.sourceforge.net/nifty-1.3.xsd http://nifty-gui.sourceforge.net/nifty-1.3.xsd">
  <useStyles filename="nifty-default-styles.xml" />
  <useControls filename="nifty-default-controls.xml" />
  <screen id="start" controller="co.pixelapp.speed.Main">
    <layer id="foreground" childLayout="vertical">
          
        <panel height="50%" width="100%" valign="center" childLayout="center">
          <control id="start_button" name="button" label="NEW GAME" align="center" valign="center" width="10%" >
          <interact onClick="newGame()"/>   
          </control>
        </panel>
        <panel height="50%" width="100%" valign="center" childLayout="center">
          <control id="quit_button" name="button" label="QUIT" align="center" valign="center" width="10%" > 
            <interact onClick="quitGame()"/>              
          </control>        
        </panel>
 
    </layer>
  </screen>
  
</nifty>

The Error

Mar 20, 2015 7:54:51 AM de.lessvoid.nifty.NiftyMethodInvoker callMethod
WARNING: Exception: java.lang.reflect.InvocationTargetException
Mar 20, 2015 7:54:51 AM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: sun.reflect.NativeMethodAccessorImpl invoke0 (NativeMethodAccessorImpl.java:-2)
Mar 20, 2015 7:54:51 AM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: sun.reflect.NativeMethodAccessorImpl invoke (NativeMethodAccessorImpl.java:57)
Mar 20, 2015 7:54:51 AM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: sun.reflect.DelegatingMethodAccessorImpl invoke (DelegatingMethodAccessorImpl.java:43)
Mar 20, 2015 7:54:51 AM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: java.lang.reflect.Method invoke (Method.java:606)
Mar 20, 2015 7:54:51 AM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: de.lessvoid.nifty.NiftyMethodInvoker callMethod (NiftyMethodInvoker.java:145)
Mar 20, 2015 7:54:51 AM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: de.lessvoid.nifty.NiftyMethodInvoker performInvoke (NiftyMethodInvoker.java:104)
Mar 20, 2015 7:54:51 AM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: de.lessvoid.nifty.Nifty$DelayedMethodInvoke perform (Nifty.java:1174)
Mar 20, 2015 7:54:51 AM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: de.lessvoid.nifty.Nifty invokeMethods (Nifty.java:1152)
Mar 20, 2015 7:54:51 AM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: de.lessvoid.nifty.Nifty handleDynamicElements (Nifty.java:354)
Mar 20, 2015 7:54:51 AM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: de.lessvoid.nifty.Nifty access$1700 (Nifty.java:77)
Mar 20, 2015 7:54:51 AM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: de.lessvoid.nifty.Nifty$NiftyInputConsumerImpl processEvent (Nifty.java:1374)
Mar 20, 2015 7:54:51 AM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: de.lessvoid.nifty.Nifty$NiftyInputConsumerImpl processMouseEvent (Nifty.java:1329)
Mar 20, 2015 7:54:51 AM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: com.jme3.niftygui.InputSystemJme handleMouseEvent (InputSystemJme.java:124)
Mar 20, 2015 7:54:51 AM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: com.jme3.niftygui.InputSystemJme onMouseButtonEventQueued (InputSystemJme.java:232)
Mar 20, 2015 7:54:51 AM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: com.jme3.niftygui.InputSystemJme forwardEvents (InputSystemJme.java:296)
Mar 20, 2015 7:54:51 AM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: de.lessvoid.nifty.Nifty update (Nifty.java:288)
Mar 20, 2015 7:54:51 AM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: com.jme3.niftygui.InputSystemJme endInput (InputSystemJme.java:113)
Mar 20, 2015 7:54:51 AM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: com.jme3.input.InputManager processQueue (InputManager.java:819)
Mar 20, 2015 7:54:51 AM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: com.jme3.input.InputManager update (InputManager.java:883)
Mar 20, 2015 7:54:51 AM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: com.jme3.app.Application update (Application.java:604)
Mar 20, 2015 7:54:51 AM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: com.jme3.app.SimpleApplication update (SimpleApplication.java:231)
Mar 20, 2015 7:54:51 AM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: com.jme3.system.lwjgl.LwjglAbstractDisplay runLoop (LwjglAbstractDisplay.java:151)
Mar 20, 2015 7:54:51 AM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: com.jme3.system.lwjgl.LwjglDisplay runLoop (LwjglDisplay.java:185)
Mar 20, 2015 7:54:51 AM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: com.jme3.system.lwjgl.LwjglAbstractDisplay run (LwjglAbstractDisplay.java:228)
Mar 20, 2015 7:54:51 AM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: java.lang.Thread run (Thread.java:744)
Mar 20, 2015 7:54:51 AM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: Root Cause: java.lang.NullPointerException
Mar 20, 2015 7:54:51 AM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: com.jme3.app.state.AppStateManager attach (AppStateManager.java:133)
Mar 20, 2015 7:54:51 AM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: co.pixelapp.speed.Main newGame (Main.java:62)
Mar 20, 2015 7:54:51 AM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: sun.reflect.NativeMethodAccessorImpl invoke0 (NativeMethodAccessorImpl.java:-2)
Mar 20, 2015 7:54:51 AM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: sun.reflect.NativeMethodAccessorImpl invoke (NativeMethodAccessorImpl.java:57)
Mar 20, 2015 7:54:51 AM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: sun.reflect.DelegatingMethodAccessorImpl invoke (DelegatingMethodAccessorImpl.java:43)
Mar 20, 2015 7:54:51 AM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: java.lang.reflect.Method invoke (Method.java:606)
Mar 20, 2015 7:54:51 AM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: de.lessvoid.nifty.NiftyMethodInvoker callMethod (NiftyMethodInvoker.java:145)
Mar 20, 2015 7:54:51 AM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: de.lessvoid.nifty.NiftyMethodInvoker performInvoke (NiftyMethodInvoker.java:104)
Mar 20, 2015 7:54:51 AM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: de.lessvoid.nifty.Nifty$DelayedMethodInvoke perform (Nifty.java:1174)
Mar 20, 2015 7:54:51 AM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: de.lessvoid.nifty.Nifty invokeMethods (Nifty.java:1152)
Mar 20, 2015 7:54:51 AM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: de.lessvoid.nifty.Nifty handleDynamicElements (Nifty.java:354)
Mar 20, 2015 7:54:51 AM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: de.lessvoid.nifty.Nifty access$1700 (Nifty.java:77)
Mar 20, 2015 7:54:51 AM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: de.lessvoid.nifty.Nifty$NiftyInputConsumerImpl processEvent (Nifty.java:1374)
Mar 20, 2015 7:54:51 AM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: de.lessvoid.nifty.Nifty$NiftyInputConsumerImpl processMouseEvent (Nifty.java:1329)
Mar 20, 2015 7:54:51 AM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: com.jme3.niftygui.InputSystemJme handleMouseEvent (InputSystemJme.java:124)
Mar 20, 2015 7:54:51 AM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: com.jme3.niftygui.InputSystemJme onMouseButtonEventQueued (InputSystemJme.java:232)
Mar 20, 2015 7:54:51 AM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: com.jme3.niftygui.InputSystemJme forwardEvents (InputSystemJme.java:296)
Mar 20, 2015 7:54:51 AM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: de.lessvoid.nifty.Nifty update (Nifty.java:288)
Mar 20, 2015 7:54:51 AM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: com.jme3.niftygui.InputSystemJme endInput (InputSystemJme.java:113)
Mar 20, 2015 7:54:51 AM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: com.jme3.input.InputManager processQueue (InputManager.java:819)
Mar 20, 2015 7:54:51 AM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: com.jme3.input.InputManager update (InputManager.java:883)
Mar 20, 2015 7:54:51 AM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: com.jme3.app.Application update (Application.java:604)
Mar 20, 2015 7:54:51 AM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: com.jme3.app.SimpleApplication update (SimpleApplication.java:231)
Mar 20, 2015 7:54:51 AM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: com.jme3.system.lwjgl.LwjglAbstractDisplay runLoop (LwjglAbstractDisplay.java:151)
Mar 20, 2015 7:54:51 AM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: com.jme3.system.lwjgl.LwjglDisplay runLoop (LwjglDisplay.java:185)
Mar 20, 2015 7:54:51 AM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: com.jme3.system.lwjgl.LwjglAbstractDisplay run (LwjglAbstractDisplay.java:228)
Mar 20, 2015 7:54:51 AM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: java.lang.Thread run (Thread.java:744)

Search the forum for something like register nifty controller, I suspect you have the same ‘problem’

You do not register your screen controller with nifty.
Nifty finds this "controller=“co.pixelapp.speed.Main”
What should Nifty do? It will do new co.pixelapp.speed.Main() and use that as a screen controller. So you end up with 2 instances of Main, one initialized as a SimpleAppliation and one that Nifty created itself.

1 Like

@jmaasing Right on! It works, thanks. I did any of the following:

myNifty.registerScreenController(this); or myNifty.fromXml("Interface/“Interface/Start.xml”);

Both work fine. Yay!