I have started to add a menu to my game but failed completely.
None of my two buttons are working. The menu renders correctly at least…
XML:
[xml]<?xml version="1.0" encoding="UTF-8"?>
<nifty xmlns="http://nifty-gui.sourceforge.net/nifty.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://nifty-gui.sourceforge.net/nifty.xsd http://nifty-gui.sourceforge.net/nifty.xsd">
<useControls filename="nifty-default-controls.xml" />
<useStyles filename="nifty-default-styles.xml" />
<screen id="menu">
<layer id="foreground" backgroundColor="#66af" childLayout="horizontal">
<panel id="panel_bottom_left" height="100%" width="50%" align="center" childLayout="center"
backgroundColor="#0000">
<control name="button" label="Start" id="StartButton" align="center" valign="center"
visibleToMouse="true" >
<interact onClick="chooseFile()"/>
</control>
</panel>
<panel id="panel_bottom_right" height="100%" width="50%" valign="center" childLayout="center">
<control name="button" label="Quit" id="QuitButton" align="center" valign="center"
visibleToMouse="true" >
<interact onClick="quitGame()"/>
</control>
</panel>
</layer>
</screen>
<screen id="hud">
<layer id="foreground" backgroundColor="#0000" childLayout="horizontal">
<panel id="panel_top" height="20" width="100%" align="center" childLayout="center"
backgroundColor="#0004">
<text id="score" text="zero" align="center" size="16" font="Interface/Fonts/Default.fnt"/>
</panel>
</layer>
</screen>
</nifty>[/xml]
Controller:
[java]/*
- To change this template, choose Tools | Templates
- and open the template in the editor.
/
package mygame;
import Audio.Analysis.Analyze;
import com.jme3.app.Application;
import com.jme3.app.SimpleApplication;
import com.jme3.app.state.AbstractAppState;
import com.jme3.app.state.AppStateManager;
import de.lessvoid.nifty.Nifty;
import de.lessvoid.nifty.screen.Screen;
import de.lessvoid.nifty.screen.ScreenController;
import java.util.logging.Level;
import java.util.logging.Logger;
public class NiftyScreen extends AbstractAppState implements ScreenController {
private Nifty nifty;
private Screen screen;
private SimpleApplication app;
public NiftyScreen() {
}
public NiftyScreen(SimpleApplication app) {
this.app = app;
}
public void chooseFile() {
System.out.println("Click");
Analyze analyze = null;
try {
analyze = new Analyze("assets/Sounds/Fly.mp3");
} catch (Exception ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
PlayingState state = new PlayingState(app, analyze);
app.getStateManager().attach(state);
}
public void quitGame() {
app.stop();
}
/* Nifty GUI ScreenControl methods /
public void bind(Nifty nifty, Screen screen) {
this.nifty = nifty;
this.screen = screen;
}
public void onStartScreen() { }
public void onEndScreen() { }
/* jME3 AppState methods /
@Override
public void initialize(AppStateManager stateManager, Application app) {
super.initialize(stateManager, app);
this.app=(SimpleApplication)app;
}
@Override
public void update(float tpf) {
/* jME update loop! */
}
}[/java]
Main.java:
[java]package mygame;
import com.jme3.app.SimpleApplication;
import com.jme3.niftygui.NiftyJmeDisplay;
import de.lessvoid.nifty.Nifty;
/**
- test
-
@author Rasmus Eneman
/
public class Main extends SimpleApplication {
Nifty nifty;
public static void main(String[] args) {
Main app = new Main();
app.start();
}
@Override
public void simpleInitApp() {
flyCam.setEnabled(false);
NiftyJmeDisplay niftyDisplay = new NiftyJmeDisplay(
assetManager, inputManager, audioRenderer, guiViewPort);
/* Create a new NiftyGUI object /
nifty = niftyDisplay.getNifty();
/* Read your XML and initialize your custom ScreenController */
nifty.fromXml("Interface/screens.xml", "menu", new NiftyScreen(this));
// attach the Nifty display to the gui view port as a processor
guiViewPort.addProcessor(niftyDisplay);
}
@Override
public void simpleUpdate(float tpf) {
}
}
[/java]