Hello,
I’m working on a game right now, and I’m trying to move to Nifty GUI. I have a basic XML file working, but when I try to modify one of my other AppStates, I get an enormous amount of exceptions. When I call a simple method such as System.out.println() using my button in the XML file, that works. But when I try to call something like:
app.getStateManager().getState(GamePlayAppState.class).chargeTower();
I don’t get the intended result, and I get the aforementioned angry red exception pile. It seems strange that I can call this exact line of code from a different AppState and have the intended effect, but from my ScreenController class, I get errors. I’ve tested it on Android, PC, and Linux and none seem to work properly.
Relevant code from Main class:
[java]
public void startGame() {
Start = new NiftyStart();
GameState = new GamePlayAppState();
PauseState = new PauseState();
OverState = new GameOverAppState();
InputState = new GamePlayInput();
GameState.setEnabled(true);
NiftyJmeDisplay niftyDisplay = new NiftyJmeDisplay(assetManager,
inputManager,
audioRenderer,
guiViewPort);
Nifty nifty = niftyDisplay.getNifty();
nifty.fromXml("Interface/newNiftyGui.xml", "start", Start);
nifty.registerScreenController(Start);
guiViewPort.addProcessor(niftyDisplay);
inputManager.setCursorVisible(true);
stateManager.attach(GameState);
stateManager.attach(InputState);
stateManager.attach(Start);
}
[/java]
NiftyStart.java
[java]package mygame;
import com.jme3.app.Application;
import com.jme3.app.SimpleApplication;
import com.jme3.app.state.AbstractAppState;
import com.jme3.app.state.AppStateManager;
import com.jme3.scene.Node;
import de.lessvoid.nifty.Nifty;
import de.lessvoid.nifty.screen.Screen;
import de.lessvoid.nifty.screen.ScreenController;
/**
*
-
@author clarence
*/
public class NiftyStart extends AbstractAppState implements ScreenController {
public Node rootNode;
public Nifty nifty;
public Screen screen;
public GamePlayAppState GameState;
public GameOverAppState GameOverAppState;
public PauseState PauseState;
private SimpleApplication app;
private GamePlayInput InputState;public NiftyStart() {
}
@Override
public void initialize(AppStateManager stateManager, Application app) {
super.initialize(stateManager, app);
this.app = (SimpleApplication) app;
}@Override
public void update(float tpf) {}
public void charge() {
nifty.removeScreen(“start”);
}public void bind(Nifty nifty, Screen screen) {
this.nifty = nifty;
this.screen = screen;
}public void onStartScreen() {
//throw new UnsupportedOperationException(“Not supported yet.”); //To change body of generated methods, choose Tools | Templates.
}public void onEndScreen() {
//throw new UnsupportedOperationException(“Not supported yet.”); //To change body of generated methods, choose Tools | Templates.
}@Override
public void cleanup() {
super.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
}
}[/java]
XML file:
[xml]
<?xml version=“1.0” encoding=“UTF-8”?>
-<nifty xsi:schemaLocation=“http://nifty-gui.sourceforge.net/nifty-1.3.xsd http://nifty-gui.sourceforge.net/nifty-1.3.xsd” xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance” xmlns=“http://nifty-gui.sourceforge.net/nifty-1.3.xsd”>
<useStyles filename=“nifty-default-styles.xml”/>
<useControls filename=“nifty-default-controls.xml”/>
-<screen controller=“mygame.NiftyStart” id=“start”>
-<layer id=“foreground” childLayout=“center” backgroundColor="#0000">
-<panel id=“panel_mid” childLayout=“center” backgroundColor="#0f08" align=“center” width=“75%” height=“50%”>
-<control id=“StartButton” align=“center” visibleToMouse=“true” valign=“center” label=“Start” name=“button”>
<interact onClick=“charge()”/>
</control>
</panel>
</layer>
</screen>
</nifty>
[/xml]
If someone could enlighten me as to what I’m doing wrong, that would be greatly appreciated.
Thanks!