Nifty Misunderstood

Hello guys, I’ve started to try to using Nitfy on my game project since 3 days, and after read the tutorials on the site, I have something I’ve think misunderstood.

I want to use some ScreenController on a AbstractAppState, and to not make a mess on my project, I want to try to write it into an anonymous function / class (I think it’s the name).
So to try the system, I take the sample from the next tutorial : https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:advanced:nifty_gui_java_layout

And I just want, in the easiest way, to write on the output “yo!” when i clic on the Nifty button.
So i’ve simply modify the code like that :

[java]
nifty.addScreen(“CruiserMenu”, new ScreenBuilder(“CruiserMenuScreen”){{
controller(new DefaultScreenController(){ // ANON CLASS
public void yo(){
Magic.show(“yo!”);
}
}); // Screen properties

        // 
        layer(new LayerBuilder("Layer_ID") {{
            childLayoutVertical(); // layer properties, add more...

            // 
            panel(new PanelBuilder("Panel_ID") {{
               childLayoutCenter(); // panel properties, add more...               
               alignRight();
                // GUI elements
               /*control(new ButtonBuilder("lol","lol"){{
               }});*/
                control(new ButtonBuilder("Button_ID", "Hello Nifty"){{
                    alignCenter();
                    valignCenter();
                    height("5%"); 
                    width("15%");
                    interactOnClick("yo()"); // And i call the method on the controller.
                }});

                //.. add more GUI elements here              

            }});
            // 
          }});
        // 
      }}.build(nifty));
    nifty.gotoScreen("CruiserMenu");

[/java]

So, the compilation happens, the button is here, but when i click on it, nothing happen (in fact, he detect the clic, because i can see it in “INFO” on the output, but there is no- “Yo!” in the output).

I think i have misunderstood the Nitfy GUI workflow, and I need some explanation.

Thanks for your future participations.

Hmm, I’m not sure anonymous classes are a good idea for scene controllers. Try using a full class.

@zarch said: Hmm, I'm not sure anonymous classes are a good idea for scene controllers. Try using a full class.

I just noticied some warnings and exceptions when i clicking on the button :

[java]Jan 14, 2013 11:32:49 AM de.lessvoid.nifty.NiftyMethodInvoker callMethod
WARNING: Exception: java.lang.IllegalAccessException: Class de.lessvoid.nifty.NiftyMethodInvoker can not access a member of class net.nakou.stardust.game.statesmanager.YourCruiserMenuState$1$1 with modifiers “public”
Jan 14, 2013 11:32:49 AM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: sun.reflect.Reflection ensureMemberAccess (Reflection.java:95)
Jan 14, 2013 11:32:49 AM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: java.lang.reflect.AccessibleObject slowCheckMemberAccess (AccessibleObject.java:261)
Jan 14, 2013 11:32:49 AM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: java.lang.reflect.AccessibleObject checkAccess (AccessibleObject.java:253)
Jan 14, 2013 11:32:49 AM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: java.lang.reflect.Method invoke (Method.java:594)
Jan 14, 2013 11:32:49 AM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: de.lessvoid.nifty.NiftyMethodInvoker callMethod (NiftyMethodInvoker.java:145)
Jan 14, 2013 11:32:49 AM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: de.lessvoid.nifty.NiftyMethodInvoker performInvoke (NiftyMethodInvoker.java:104)
Jan 14, 2013 11:32:49 AM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: de.lessvoid.nifty.Nifty$DelayedMethodInvoke perform (Nifty.java:1176)
Jan 14, 2013 11:32:49 AM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: de.lessvoid.nifty.Nifty invokeMethods (Nifty.java:1154)
Jan 14, 2013 11:32:49 AM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: de.lessvoid.nifty.Nifty handleDynamicElements (Nifty.java:312)
Jan 14, 2013 11:32:49 AM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: de.lessvoid.nifty.Nifty access$1500 (Nifty.java:73)
Jan 14, 2013 11:32:49 AM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: de.lessvoid.nifty.Nifty$NiftyInputConsumerImpl processEvent (Nifty.java:1371)
Jan 14, 2013 11:32:49 AM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: de.lessvoid.nifty.Nifty$NiftyInputConsumerImpl processMouseEvent (Nifty.java:1329)
Jan 14, 2013 11:32:49 AM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: com.jme3.niftygui.InputSystemJme handleMouseEvent (InputSystemJme.java:124)
Jan 14, 2013 11:32:49 AM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: com.jme3.niftygui.InputSystemJme onMouseButtonEventQueued (InputSystemJme.java:196)
Jan 14, 2013 11:32:49 AM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: com.jme3.niftygui.InputSystemJme forwardEvents (InputSystemJme.java:260)
Jan 14, 2013 11:32:49 AM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: de.lessvoid.nifty.Nifty update (Nifty.java:248)
Jan 14, 2013 11:32:49 AM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: com.jme3.niftygui.InputSystemJme endInput (InputSystemJme.java:113)
Jan 14, 2013 11:32:49 AM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: com.jme3.input.InputManager processQueue (InputManager.java:821)
Jan 14, 2013 11:32:49 AM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: com.jme3.input.InputManager update (InputManager.java:885)
Jan 14, 2013 11:32:49 AM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: com.jme3.app.Application update (Application.java:606)
Jan 14, 2013 11:32:49 AM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: com.jme3.app.SimpleApplication update (SimpleApplication.java:230)
Jan 14, 2013 11:32:49 AM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: com.jme3.system.lwjgl.LwjglAbstractDisplay runLoop (LwjglAbstractDisplay.java:151)
Jan 14, 2013 11:32:49 AM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: com.jme3.system.lwjgl.LwjglDisplay runLoop (LwjglDisplay.java:185)
Jan 14, 2013 11:32:49 AM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: com.jme3.system.lwjgl.LwjglAbstractDisplay run (LwjglAbstractDisplay.java:228)
Jan 14, 2013 11:32:50 AM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: java.lang.Thread run (Thread.java:722)[/java]

I think you’re right, I think there is some issues with anonymous class in this case, to acces on methods. That’s very bad, I found my solution was aesthetically beautiful.

I’m goin to create the Controller in the AppState class (like we do with ActionListener) and see if it run correctly or not.
Thanks for your answer.

Okay, the solution with the creation of the Controller on the bottom of AppState class doesn’t work. I’m a moron. Obviously, Java consider it’s a anonymous class too.
I imagine there is a process who take the method of the class and put in a collection of string to reference it.
This workflow propably doesn’t work in a anonymous class, and it’s bad, because it force me to create a specific class file everytime.

Or i’m totally wrong and there is a solution I’ve not found.

If any Nifty jME guy is there, and thinking i’m more moron than i’m allready think, feel free to tell me the solution :D.

Have a great day guys, and thanks for your help.

You should be able to create an INNER class, just not an ANONYMOUS inner class.