Use Case 1: Nifty to Game
I have a Nifty Main Menu screen that has buttons
Start(Back to Game)
Import
Export
…
If I click the Start button (<interact onClick="startGame()) I do
[java]
public void startGame() {
ScriptBlocksApplication.getInstance().gameState();
ScriptBlocksApplication.getInstance().setNiftyActive(false);
nifty.exit();
}
[/java]
Use Case 2: Game to Nifty
If I am in game and Hit Esc the game opens up the Main Menu Nifty Screen
I do it by
[java]
public boolean isNiftyActive() {
return niftyActive;
}
public void initNiftyGui() {
Logger.getLogger(“de.lessvoid”).setLevel(Level.OFF);
if (isNiftyActive()) {
return;
} else {
setNiftyActive(true);
}
NiftyJmeDisplay niftyDisplay = new NiftyJmeDisplay(assetManager,
inputManager, audioRenderer, guiViewPort);
nifty = niftyDisplay.getNifty();
guiViewPort.addProcessor(niftyDisplay);
nifty.fromXml(“Interfaces/niftytest.xml”, “start”);
guiState();
…
public void guiState() {
inputManager.setCursorVisible(true);
flyCam.setEnabled(false);
setGameState(1);
}
}
[/java]
Use Case 3: Nifty Screen to Nifty Screen
Import Button (<interact onClick="importGame() )on Nifty Main Menu
[java]
public void importGame() {
nifty.fromXml(“Interfaces/import.xml”, “import”);
}
[/java]
My First Question is this the correct way to do it
should I be calling Nifty.exit() each time I got back to the game?
Trouble Im having is that
Sometimes I get two Main Menus on top of each other
Other times I get Exception
[java]
SEVERE: Uncaught exception thrown in Thread[LWJGL Renderer Thread,5,main]
java.lang.NullPointerException
at de.lessvoid.nifty.screen.Screen.forwardMouseEventToLayers(Screen.java:302)
at de.lessvoid.nifty.screen.Screen.mouseEvent(Screen.java:289)
at de.lessvoid.nifty.Nifty.forwardMouseEventToScreen(Nifty.java:244)
at de.lessvoid.nifty.Nifty.access$1100(Nifty.java:66)
at de.lessvoid.nifty.Nifty$NiftyInputConsumerImpl.processEvent(Nifty.java:1249)
at de.lessvoid.nifty.Nifty$NiftyInputConsumerImpl.processMouseEvent(Nifty.java:1222)
at com.jme3.niftygui.InputSystemJme.onMouseMotionEventQueued(InputSystemJme.java:104)
at com.jme3.niftygui.InputSystemJme.forwardEvents(InputSystemJme.java:167)
at de.lessvoid.nifty.Nifty.update(Nifty.java:226)
at com.jme3.niftygui.InputSystemJme.endInput(InputSystemJme.java:92)
at com.jme3.input.InputManager.processQueue(InputManager.java:551)
at com.jme3.input.InputManager.update(InputManager.java:600)
at com.jme3.app.Application.update(Application.java:463)
at com.jme3.app.SimpleApplication.update(SimpleApplication.java:228)
at com.jme3.system.lwjgl.LwjglAbstractDisplay.runLoop(LwjglAbstractDisplay.java:143)
at com.jme3.system.lwjgl.LwjglDisplay.runLoop(LwjglDisplay.java:171)
at com.jme3.system.lwjgl.LwjglAbstractDisplay.run(LwjglAbstractDisplay.java:217)
at java.lang.Thread.run(Thread.java:662)
[/java]
My Game:
ScriptBlocks can be downloaded at http://code.google.com/p/script-blocks/
Managing the states (pun intended and you’ll see why) of a nifty gui in JME is kind of tricky until you develop some good patterns.
I tend to make AppStates that are also nifty screen controllers so that I can more easily manage this. That way, I can also see if the particular screen’s app state is already enabled/attached and not attach it again (not accidentally bring up the same menu twice, for example).
How you break it up depends largely on screen organization and when they need to connect to each other, etc… and some of that is a matter of taste. But tying my screen controllers to a app states has simplified a ton of my code.