Hi,
im currently trying to understand jmonkey and im stuck with getting the Nifty gui working, here is the Code:
… //Starts in main
[java]
public class Main extends SimpleApplication {
public static void main(String[] args) {
new Main().start();
}
public void simpleInitApp() {
new Game("Erstes Game", (SimpleApplication) this);
}
}
[/java]
… //travels to main menu
[java]
public class Game extends AbstractAppState {
private SimpleApplication app;
private String name;
public Game(String string, SimpleApplication app) {
this.name = name;
this.app = app;
MainMenu mainMenu = new MainMenu("Main Menu",app);
app.getStateManager().attach(mainMenu);
}
}
[/java]
… //Displays the gui
[java]
public class MainMenu extends AbstractAppState implements ScreenController {
private NiftyJmeDisplay niftyDisplay;
private Nifty nifty;
private SimpleApplication app;
public MainMenu(String name, SimpleApplication app) {
this.app = app;
app.getInputManager().setCursorVisible(true);
//stuff
niftyDisplay = new NiftyJmeDisplay(
app.getAssetManager(), app.getInputManager(), app.getAudioRenderer(), app.getGuiViewPort());
nifty = niftyDisplay.getNifty();
//xml file and template
nifty.fromXml("Interface/GameGui.xml", name);
app.getGuiViewPort().addProcessor(niftyDisplay);
}
public void startGame() {
System.out.println("Start Game");
}
public void quitGame() {
System.out.println("Quit Game");
}
public void onAction(String name, boolean isPressed, float tpf) {
System.out.println(name);
}
public void onStartScreen() {
System.out.println("On start screen");
}
public void onEndScreen() {
System.out.println("On end screen");
}
public void bind(Nifty nifty, Screen screen) {
System.out.println("bind");
}
}
[/java]
None of the System.out.println Messages does show. Heres the XML:
[xml]
… //XML
<useControls filename="nifty-default-controls.xml" />
<useStyles filename="nifty-default-styles.xml" />
<screen id="Main Menu" controller="mygame.MainMenu">
<layer id="background" childLayout="center">
<image filename="Interface/lol.jpg" width="100%" height="100%"/>
</layer>
<layer id="foreground" backgroundColor="#0000" childLayout="vertical">
<panel id="panel_top" height="25%" width="75%" align="center" childLayout="center"
backgroundColor="#f008">
</panel>
<panel id="panel_mid" height="50%" width="75%" align="center" childLayout="center"
backgroundColor="#0f08">
<panel id="panel_mid" height="50%" width="75%" align="center" childLayout="center">
<text text="Here goes some text describing the game and the rules and stuff. Incidentally,
the text is quite long and needs to wrap at the end of lines. …"
font="Interface/Fonts/Default.fnt" width="100%" height="100%" wrap="true" />
</panel>
</panel>
<panel id="panel_bottom" height="25%" width="75%" align="center" childLayout="horizontal"
backgroundColor="#00f8">
<panel id="panel_bottom_left" height="50%" width="50%" valign="center" childLayout="center"
backgroundColor="#44f8">
<control name="button" label="Start" id="StartButton" align="center" valign="center" visibleToMouse="true" >
<interact onClick="startGame()"/>
</control>
</panel>
<panel id="panel_bottom_right" height="50%" width="50%" valign="center" childLayout="center"
backgroundColor="#88f8">
<control name="button" label="Quit" id="QuitButton" align="center" valign="center" visibleToMouse="true" >
<interact onClick="quitGame()"/>
</control>
</panel>
</panel>
</layer>
</screen>
</nifty>
[/xml]
The Package is mygame.
The gui is displayed but the buttons doesnt work. Wat do?
greetings,
drollian
You gotta supply the screenController (“this” in this case) to the nifty instance.
1 Like
You gotta supply the screenController (“this” in this case) to the nifty instance.
Thanks for the quick reply but im Totally clueless how to do this :(
nifty.addScreen(name, this); is not possible since MainMenu is an AbstractAppState and nifty.addScreen(name, nifty.getCurrentScreen()); doesnt work.
nifty.fromXml(“Interface/GameGui.xml”, name, this);
never mind. thanks.