Nifty Button not working

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]

At a glance, it looks like you’re missing the code necessary to get the GUI to interact with your ScreenController; you can do this from either the Java side or the XML side as shown here:

https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:advanced:nifty_gui_java_interaction

1 Like

Yep, you need to register your screen controller in your xml, like so in your screen tag

[xml]<screen id="start" controller="tutorial.MyStartScreen">[/xml]

in your case i think this would be

[xml]<screen id="start" controller="mygame.NiftyScreen">[/xml]

2 Likes

You haven’t declared a screen controller for your menu screen (in the xml).

Even though you pass an instance of a screen controller to the fromXml method (like you do), you have to declare a controller class in the xml as well. Just add controller=“mygame.NiftyScreen” in the screen tag of your menu screen declaration.

I tested it for you and it works after you add that.



EDIT: What @kbender88 said. :wink:

1 Like

Thanks to all three of you!



A bit embarrassing that I could miss that as I read

https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:advanced:nifty_gui_java_interaction

three to four times before posting.