How do switch between Nifty Gui to game (AppStates?)

Hello :smile:

I have been following the NIFTY GUI tuts and I would like to make it so when the player clicks the start button. It goes to the main level. The level is the class

This is my StartScreen.Java which i followed in the tut and is an app state. The is not an app state so may that affect it?(but is .java)
package mygame;

import com.jme3.asset.AssetManager;
import com.jme3.renderer.ViewPort;
import com.jme3.scene.Node;
import de.lessvoid.nifty.Nifty;
import de.lessvoid.nifty.screen.Screen;
import de.lessvoid.nifty.screen.ScreenController;


  • @author o

public class StartScreen extends AbstractAppState implements ScreenController {

public ViewPort viewport;
public Node rootNode;
public Node guiNode;
public AssetManager assetManager;

public static void main(String[] args) {
    StartScreen app = new StartScreen();

public StartScreen(SimpleApplication app){
    this.rootNode = app.getRootNode();
    this.viewport = app.getViewPort();
    this.guiNode =  app.getGuiNode();
    this.assetManager = app.getAssetManager();

private StartScreen() {


public void initialize(AppStateManager stateManager, Application app) {
    super.initialize(stateManager, app);
    //TODO: initialize your AppState, e.g. attach spatials to rootNode
    //this is called on the OpenGL thread after the AppState has been attached


public void update(float tpf) {
    //TODO: implement behavior during runtime

public void cleanup() {
    //TODO: clean up what you initialized in the initialize method,
    //e.g. remove all spatials from rootNode
    //this is called on the OpenGL thread after the AppState has been detached

public void bind(Nifty nifty, Screen screen) {
 public void onStartScreen() {
  public void onEndScreen() {
 /** custom methods*/
 public void startGame(String Test1) {
 public void quitGame() {
public void start() {

indent preformatted text by 4 spaces

The Nifty Code

<?xml version="1.0" encoding="UTF-8"?>

    <layer id="background" childLayout="center">
      <image filename="Interface/Mainmenu.png" 
             width="100%" height="100%"/>       
    <layer id="foreground" backgroundColor="#0000" childLayout="vertical">
    <panel id="panel_top" height="55%" width="100%" valign="center" childLayout="center">  
      <control name="button" label="Start" id="StartButton" align="center" valign="center" 
          <interact onClick="startGame()"/>
    <panel id="panel_bottom_right" height="15%" width="100%" valign="center" childLayout="center">  
      <control name="button" label="Quit" id="QuitButton" align="center" valign="center"
          visibleToMouse="true" >
          <interact onClick="quitGame()"/>

Thank you

I currently use app states. I detach the main menu app state and attach a new game scene app state.

stateManager.attach(new GameScene());

Thank you Danny
How would i go about including this in the current code?
What should I do?

Also My is not an Appstate but a normal java file would I need to make it into an appstate or is there a way behind this? Thank you

At the moment your code looks a bit odd, you have a StartScreen extending AbstractAppState but it also acts as the static main entry point.

Feels a bit like you’ve forgotten to actually create an Application class.


I have changed from Main to StartScreen if that helps ( I have changed it back)
public static void StartScreen(String[] args) {
StartScreen app = new StartScreen();
It is now saying it does not have a main method.

I am confused can I have two main classes or will they conflict with each other and/or cause problems?
As the Test1 is the main class and contains the main game code.

It seems like you are fairly new to Java. That will be a great hinderance as you continue.

AppStates are for adding things to the Application… but you still need a SimpleApplication subclass with a static main() method. This is how your Java application will start.

Thanks I am new to Java but I will continue and finish this game and get this done.
How would I go about doing this subclass method?

I shall quote a very wise man I once met :wink:

ROFL haha
How would i make the public class extend the simple application and the AbstractAppState?

Do all of the JME tutorials. (And probably several regular Java tutorials, too.)

You wouldn’t. You can’t… and even the question itself shows that you don’t know enough to even start.

Please I would like to solve this issue I can’t stop this project as I have put too much time into this and I won’t stop.

Applications HAVE app states. They are NOT app states.

This is a fundamental disconnect and so we can’t even begin to know how to answer you properly. If you’d done the tutorials then you’d already know this, I guess. AppState != Application. You have ONE Application that has SOME appstates inside of it.

I mean, so far what you are saying is like “I really want to go to the store, can someone help me flip my car upside down?” It makes no sense and has nothing to do with your end goal… but we don’t even know enough to tell you how to correct your thinking.

I just want help to solve this Issue and be done with it.
It is extremely frustating that a (seems) simple menu screen to game does not work . I want you to help me solve this issue not lecture me. Now please help me. And I apologise if I am somehow coming across as aggresive but all I want here is help. As I am sure you were in my shoes asking for help in forms when you were a newbie and the person before you . So please help. Thank you

We don’t have enough information to help you. I’m sorry. You’ve shown us only a tiny part of the problem… we don’t see your main application. We only see that you are somehow trying to start an app state from the command line… which is completely wrong.

Can you explain how we are supposed to help when you seem not to even know how to create a game application in JME? I mean, show us that we are wrong because that’s what it looks like. We are not insulting you or giving you lectures… but really, how can we help?

Thank your for your patience and understanding. :relieved:

What I am trying to do is from following the Nifty Gui tuts is make a main menu screen where there is two buttons a start and quit button. The start button will launch the file (game code) and the quit button will quit to desktop (but for now quits the application)

I heard you can accomplish this with Appstates I may have confused this as your said with being separate things. But as come to may understanding the whole project is an application for which has Appstates already built/coded in.?

I was following the nifty tut and made an Appstate and named it StartScreen and followed the tut until it said

/** custom methods */
public void startGame(String nextScreen) {
nifty.gotoScreen(nextScreen); // switch to another screen
// start the game and do some more stuff…

public void quitGame() {


I would like to make it so when you press the start button it would go to the game and run the which contains the game such as the models/physics etc.

Run your application with the main menu state attached.
Then when they start the game attach the play game app state.

You cannot launch the application after the application has been launched because obviously the application has already been launched.

We’re not trying to be rude to you or anything, with your knowledge the fastest and easiest way for you to solve this problem would really be to go through the tutorials on the wiki and learn how the Application hangs together with AppState’s and I can’t stress this enough, please learn this before even getting into physics and collision and other more advanced topics.

The first two lines of code I gave you pretty much sums up the solution just as @pspeed explained it to you.

  • Create a class MyGame extends SimpleApplication, see the tutorial on how to do this.
  • Create two more classes, MainMenuAppState and GameAppState, both extending AbstractAppState.
  • Use the code I posted at the top of this topic for switching from MainMenuAppState to GameAppState.

Another good page to have a read of would be