I have some problems with interaction between java and Nifty GUI. I worked through the Examples of Nifty and coded a small Nifty which has two buttons. the Beenden button should close the Nifty and the Window. Here is the code of the Nifty and Screen Controller. I hope that someone can find the mistake and solve my problem.
control id=“Beenden” type=“button” label=“Beenden” wiidth=“100px” height=“30px”
interact onClick=“beenden()”/
/control
[java]import de.lessvoid.nifty.Nifty;
import de.lessvoid.nifty.screen.Screen;
import de.lessvoid.nifty.screen.ScreenController;
import de.lessvoid.nifty.controls.button.controller.ButtonControl;
public class StartController implements ScreenController{
private Nifty nifty;
private Screen screen;
public void bind(Nifty nifty, Screen screen) {
this.nifty = nifty;
this.screen = screen;
}
public void onStartScreen() {
}
public void onEndScreen() {
}
public void starten() {
nifty.fromXml(“Interface/hauptmenu.xml”, “start”);
}
public void beenden() {
nifty.exit();
}[/java]
Have you tried the endScreen method on the screen before exiting Nifty?
On a side note, there is 2 ‘i’ in your “width”.
No I didn´t. But the problem is that no function is working. It seems like the Nifty loads the ScreenController but can´t see the functions. Because in the console the nothing happens if I click on the Button. I tested it by using a System.out.println(“Test”); command in the function starten(). It is very important for me that it works because I need it for project at University.
I had this problem too. You should put the ScreenController in a seperate class, otherwise the ScreenController class can’t be initiated from the NiftyGUI library.
The only problem that I have now is that if the ScreenController class calls the main class, the main class is started again. It looks like some concurrency problem.
I put the Screen in a separate class from the beginning, or do you mean a separated package?
I mean this:
XML
-screen id=“start” controller=“CarGame.MainMenuScreenController”-
XML
[java]
public class MainMenuState extends AbstractAppState implements ActionListener{
private NiftyJmeDisplay niftyDisplay = null;
private Nifty nifty = null;
private void loadMenu() {
niftyDisplay = new NiftyJmeDisplay(game.getAssetManager(),
game.getInputManager(),
game.getAudioRenderer(),
game.getGUIViewPort());
nifty = niftyDisplay.getNifty();
nifty.fromXml(“Interface/MainMenu.xml”, “start”);
// attach the nifty display to the gui view port as a processor
game.getGUIViewPort().addProcessor(niftyDisplay);
}
[/java]
[java]
public class MainMenuScreenController implements ScreenController {
public void bind(Nifty nifty, Screen screen) {
//throw new UnsupportedOperationException(“Not supported yet.”);
System.out.println(“bind”);
}
public void onStartScreen() {
//throw new UnsupportedOperationException(“Not supported yet.”);
System.out.println(“onScreenStart”);
}
public void onEndScreen() {
//throw new UnsupportedOperationException(“Not supported yet.”);
System.out.println(“onScreenEnd”);
}
public void start() {
//throw new UnsupportedOperationException(“Not supported yet.”);
System.out.println(“start”);
}
public void exit() {
//throw new UnsupportedOperationException(“Not supported yet.”);
System.out.println(“exit”);
CarGame.getApp().stop();
}
}
[/java]
I don’t know if this is the correct way, because I still get the concurrency error.
I done it like this but it doesnt´t work. The Nifty initializes the Screen Controller but is does not call the functions
Maybe it is incorrectly set in the nifty XML? Also, is bind() called for the ScreenController?
@Momoko_Fan: I think it would be a good idea to have an example of the ScreenController in the jME3 tests. It’s a little bit difficult to find out yourself how the ScreenController works. It took me some time too to get it working.
@Momoko Fan: Where do I have to call the bind() I thought the xml-file initializes the ScreenController and then it works.
It would be nice if someone could give me a working example because I do not find the problem.
Thank you in advance
Maybe like this
[java]
public class StartController implements ScreenController{
private NiftyJmeDisplay niftyDisplay = null;
private Nifty nifty;
private Screen screen;
public StartController(Application game) {
niftyDisplay = new NiftyJmeDisplay(game.getAssetManager(),
game.getInputManager(),
game.getAudioRenderer(),
game.getGUIViewPort());
nifty = niftyDisplay.getNifty();
nifty.fromXml(“Interface/MainMenu.xml”, “start”,this);
// attach the nifty display to the gui view port as a processor
game.getGUIViewPort().addProcessor(niftyDisplay);
}
public void bind(Nifty nifty, Screen screen) {
this.nifty = nifty;
this.screen = screen;
}
public void onStartScreen() {
}
public void onEndScreen() {
}
public void starten() {
nifty.fromXml(“Interface/hauptmenu.xml”, “start”,this);
}
public void beenden() {
nifty.exit();
}
[/java]
When I write game.getGUIViewPort() I get failure: cannot find method getGuiViewPort()
Yes
In your Game class you need to create this
public ViewPort getGUIVeiwPort() {
return guiViewPort;
}
and this:
public StartController(Application game)
must be:
public StartController(YourApplication game)
Okay, and in the game class? Are there any commands to initialize the nifty or the ScreenController?
public class MyGame extends SimpleApplication {…
StartController myControll;
public void simpleInitApp() {
myControll=new StartController(this);
}
When I start the game I get a failure:
SCHWERWIEGEND: Uncaught exception thrown in Thread[LWJGL Renderer Thread,5,main]
java.lang.NoSuchMethodError: Method org.lwjgl.openal.AL10.nalEnable(I)V not found
at org.lwjgl.openal.AL10.initNativeStubs(Native Method)
at org.lwjgl.openal.AL.init(AL.java:158)
at org.lwjgl.openal.AL.create(AL.java:140)
at org.lwjgl.openal.AL.create(AL.java:104)
at org.lwjgl.openal.AL.create(AL.java:191)
at com.jme3.audio.lwjgl.LwjglAudioRenderer.initialize(LwjglAudioRenderer.java:97)
at com.jme3.app.Application.initAudio(Application.java:176)
at com.jme3.app.Application.initialize(Application.java:387)
at com.jme3.app.SimpleApplication.initialize(SimpleApplication.java:162)
at com.jme3.system.lwjgl.LwjglAbstractDisplay.initInThread(LwjglAbstractDisplay.java:134)
at com.jme3.system.lwjgl.LwjglAbstractDisplay.run(LwjglAbstractDisplay.java:183)
at java.lang.Thread.run(Thread.java:619)