Remove the nifty screen

Hi, I’m new at this, I have followed the nifty tutorial and it worked like a charm, still I’m am unable to remove the screen when the user presses a panel so that the game starts.



here is what i did:



_In the xml file I have added to a panel

_In the MySettingScreen.java I have Implemented the sayHello() as so :

public void sayHello(String myarg) {

System.out.println("Nifty says "+myarg);

Main o1= new Main();

o1.RemoveScreen();

}

_In the Main.java I have Implemented RemoveScreen() as so :

public void RemoveScreen(){

nifty.removeScreen(“start”);

}



When I run the project I am getting the “Nifty says hi” when I click the panel but the screen is not being removed instead I am getting :





Nifty says hi

Sep 24, 2011 2:54:49 PM de.lessvoid.nifty.NiftyMethodInvoker callMethod

WARNING: Exception: java.lang.reflect.InvocationTargetException

Sep 24, 2011 2:54:49 PM de.lessvoid.nifty.NiftyMethodInvoker logException

WARNING: sun.reflect.NativeMethodAccessorImpl invoke0 (NativeMethodAccessorImpl.java:-2)

Sep 24, 2011 2:54:49 PM de.lessvoid.nifty.NiftyMethodInvoker logException

WARNING: sun.reflect.NativeMethodAccessorImpl invoke (NativeMethodAccessorImpl.java:39)

Sep 24, 2011 2:54:49 PM de.lessvoid.nifty.NiftyMethodInvoker logException

WARNING: sun.reflect.DelegatingMethodAccessorImpl invoke (DelegatingMethodAccessorImpl.java:25)

Sep 24, 2011 2:54:49 PM de.lessvoid.nifty.NiftyMethodInvoker logException

WARNING: java.lang.reflect.Method invoke (Method.java:597)

Sep 24, 2011 2:54:49 PM de.lessvoid.nifty.NiftyMethodInvoker logException

WARNING: de.lessvoid.nifty.NiftyMethodInvoker callMethod (NiftyMethodInvoker.java:145)

Sep 24, 2011 2:54:49 PM de.lessvoid.nifty.NiftyMethodInvoker logException

WARNING: de.lessvoid.nifty.NiftyMethodInvoker performInvoke (NiftyMethodInvoker.java:91)

Sep 24, 2011 2:54:49 PM de.lessvoid.nifty.NiftyMethodInvoker logException

WARNING: de.lessvoid.nifty.Nifty$DelayedMethodInvoke perform (Nifty.java:1155)

Sep 24, 2011 2:54:49 PM de.lessvoid.nifty.NiftyMethodInvoker logException

WARNING: de.lessvoid.nifty.Nifty invokeMethods (Nifty.java:1133)

Sep 24, 2011 2:54:49 PM de.lessvoid.nifty.NiftyMethodInvoker logException

WARNING: de.lessvoid.nifty.Nifty handleDynamicElements (Nifty.java:291)

Sep 24, 2011 2:54:49 PM de.lessvoid.nifty.NiftyMethodInvoker logException

WARNING: de.lessvoid.nifty.Nifty access$1400 (Nifty.java:72)

Sep 24, 2011 2:54:49 PM de.lessvoid.nifty.NiftyMethodInvoker logException

WARNING: de.lessvoid.nifty.Nifty$NiftyInputConsumerImpl processEvent (Nifty.java:1345)

Sep 24, 2011 2:54:49 PM de.lessvoid.nifty.NiftyMethodInvoker logException

WARNING: de.lessvoid.nifty.Nifty$NiftyInputConsumerImpl processMouseEvent (Nifty.java:1311)

Sep 24, 2011 2:54:49 PM de.lessvoid.nifty.NiftyMethodInvoker logException

WARNING: com.jme3.niftygui.InputSystemJme onMouseButtonEventQueued (InputSystemJme.java:123)

Sep 24, 2011 2:54:49 PM de.lessvoid.nifty.NiftyMethodInvoker logException

WARNING: com.jme3.niftygui.InputSystemJme forwardEvents (InputSystemJme.java:192)

Sep 24, 2011 2:54:49 PM de.lessvoid.nifty.NiftyMethodInvoker logException

WARNING: de.lessvoid.nifty.Nifty update (Nifty.java:227)

Sep 24, 2011 2:54:49 PM de.lessvoid.nifty.NiftyMethodInvoker logException

WARNING: com.jme3.niftygui.InputSystemJme endInput (InputSystemJme.java:92)

Sep 24, 2011 2:54:49 PM de.lessvoid.nifty.NiftyMethodInvoker logException

WARNING: com.jme3.input.InputManager processQueue (InputManager.java:773)

Sep 24, 2011 2:54:49 PM de.lessvoid.nifty.NiftyMethodInvoker logException

WARNING: com.jme3.input.InputManager update (InputManager.java:837)

Sep 24, 2011 2:54:49 PM de.lessvoid.nifty.NiftyMethodInvoker logException

WARNING: com.jme3.app.Application update (Application.java:567)

Sep 24, 2011 2:54:49 PM de.lessvoid.nifty.NiftyMethodInvoker logException

WARNING: com.jme3.app.SimpleApplication update (SimpleApplication.java:235)

Sep 24, 2011 2:54:50 PM de.lessvoid.nifty.NiftyMethodInvoker logException

WARNING: com.jme3.system.lwjgl.LwjglAbstractDisplay runLoop (LwjglAbstractDisplay.java:149)

Sep 24, 2011 2:54:50 PM de.lessvoid.nifty.NiftyMethodInvoker logException

WARNING: com.jme3.system.lwjgl.LwjglDisplay runLoop (LwjglDisplay.java:185)

Sep 24, 2011 2:54:50 PM de.lessvoid.nifty.NiftyMethodInvoker logException

WARNING: com.jme3.system.lwjgl.LwjglAbstractDisplay run (LwjglAbstractDisplay.java:223)

Sep 24, 2011 2:54:50 PM de.lessvoid.nifty.NiftyMethodInvoker logException

WARNING: java.lang.Thread run (Thread.java:619)

Sep 24, 2011 2:54:50 PM de.lessvoid.nifty.NiftyMethodInvoker logException

WARNING: Root Cause: java.lang.NullPointerException

Sep 24, 2011 2:54:50 PM de.lessvoid.nifty.NiftyMethodInvoker logException

WARNING: mygame.Main level1 (Main.java:50)

Sep 24, 2011 2:54:50 PM de.lessvoid.nifty.NiftyMethodInvoker logException

WARNING: mygame.MySettingsScreen sayHello (MySettingsScreen.java:36)

Sep 24, 2011 2:54:50 PM de.lessvoid.nifty.NiftyMethodInvoker logException

WARNING: sun.reflect.NativeMethodAccessorImpl invoke0 (NativeMethodAccessorImpl.java:-2)

Sep 24, 2011 2:54:50 PM de.lessvoid.nifty.NiftyMethodInvoker logException

WARNING: sun.reflect.NativeMethodAccessorImpl invoke (NativeMethodAccessorImpl.java:39)

Sep 24, 2011 2:54:50 PM de.lessvoid.nifty.NiftyMethodInvoker logException

WARNING: sun.reflect.DelegatingMethodAccessorImpl invoke (DelegatingMethodAccessorImpl.java:25)

Sep 24, 2011 2:54:50 PM de.lessvoid.nifty.NiftyMethodInvoker logException

WARNING: java.lang.reflect.Method invoke (Method.java:597)

Sep 24, 2011 2:54:50 PM de.lessvoid.nifty.NiftyMethodInvoker logException

WARNING: de.lessvoid.nifty.NiftyMethodInvoker callMethod (NiftyMethodInvoker.java:145)

Sep 24, 2011 2:54:50 PM de.lessvoid.nifty.NiftyMethodInvoker logException

WARNING: de.lessvoid.nifty.NiftyMethodInvoker performInvoke (NiftyMethodInvoker.java:91)

Sep 24, 2011 2:54:50 PM de.lessvoid.nifty.NiftyMethodInvoker logException

WARNING: de.lessvoid.nifty.Nifty$DelayedMethodInvoke perform (Nifty.java:1155)

Sep 24, 2011 2:54:50 PM de.lessvoid.nifty.NiftyMethodInvoker logException

WARNING: de.lessvoid.nifty.Nifty invokeMethods (Nifty.java:1133)

Sep 24, 2011 2:54:50 PM de.lessvoid.nifty.NiftyMethodInvoker logException

WARNING: de.lessvoid.nifty.Nifty handleDynamicElements (Nifty.java:291)

Sep 24, 2011 2:54:50 PM de.lessvoid.nifty.NiftyMethodInvoker logException

WARNING: de.lessvoid.nifty.Nifty access$1400 (Nifty.java:72)

Sep 24, 2011 2:54:50 PM de.lessvoid.nifty.NiftyMethodInvoker logException

WARNING: de.lessvoid.nifty.Nifty$NiftyInputConsumerImpl processEvent (Nifty.java:1345)

Sep 24, 2011 2:54:50 PM de.lessvoid.nifty.NiftyMethodInvoker logException

WARNING: de.lessvoid.nifty.Nifty$NiftyInputConsumerImpl processMouseEvent (Nifty.java:1311)

Sep 24, 2011 2:54:50 PM de.lessvoid.nifty.NiftyMethodInvoker logException

WARNING: com.jme3.niftygui.InputSystemJme onMouseButtonEventQueued (InputSystemJme.java:123)

Sep 24, 2011 2:54:50 PM de.lessvoid.nifty.NiftyMethodInvoker logException

WARNING: com.jme3.niftygui.InputSystemJme forwardEvents (InputSystemJme.java:192)

Sep 24, 2011 2:54:50 PM de.lessvoid.nifty.NiftyMethodInvoker logException

WARNING: de.lessvoid.nifty.Nifty update (Nifty.java:227)

Sep 24, 2011 2:54:50 PM de.lessvoid.nifty.NiftyMethodInvoker logException

WARNING: com.jme3.niftygui.InputSystemJme endInput (InputSystemJme.java:92)

Sep 24, 2011 2:54:50 PM de.lessvoid.nifty.NiftyMethodInvoker logException

WARNING: com.jme3.input.InputManager processQueue (InputManager.java:773)

Sep 24, 2011 2:54:50 PM de.lessvoid.nifty.NiftyMethodInvoker logException

WARNING: com.jme3.input.InputManager update (InputManager.java:837)

Sep 24, 2011 2:54:50 PM de.lessvoid.nifty.NiftyMethodInvoker logException

WARNING: com.jme3.app.Application update (Application.java:567)

Sep 24, 2011 2:54:50 PM de.lessvoid.nifty.NiftyMethodInvoker logException

WARNING: com.jme3.app.SimpleApplication update (SimpleApplication.java:235)

Sep 24, 2011 2:54:50 PM de.lessvoid.nifty.NiftyMethodInvoker logException

WARNING: com.jme3.system.lwjgl.LwjglAbstractDisplay runLoop (LwjglAbstractDisplay.java:149)

Sep 24, 2011 2:54:50 PM de.lessvoid.nifty.NiftyMethodInvoker logException

WARNING: com.jme3.system.lwjgl.LwjglDisplay runLoop (LwjglDisplay.java:185)

Sep 24, 2011 2:54:50 PM de.lessvoid.nifty.NiftyMethodInvoker logException

WARNING: com.jme3.system.lwjgl.LwjglAbstractDisplay run (LwjglAbstractDisplay.java:223)

Sep 24, 2011 2:54:50 PM de.lessvoid.nifty.NiftyMethodInvoker logException

WARNING: java.lang.Thread run (Thread.java:619)

_In the xml file I have added to a panel : *

Just make a screen with no content and go to that. Or switch to a screen that represents your in-game UI.

1 Like

how do i switch ?? sry for the question but i really dont know how

nifty.gotoScreen()?

Thanks a lot Mr. normen really appreciate it , but it didn’t work, I must be doing something wrong, can you please send me a finished example of a simple menu, I need one with only new game and exit buttons. I know it’s a lot to ask but my project is due on tuesday and all i got left is that damn menu…thanks again for putting up with me .

This is the UI of monkey zone:

http://code.google.com/p/monkeyzone/source/browse/trunk/assets/Interface/ClientUI.xml

I think it would’ve been easier to see the original screen and the methods being called to “close” that screen. This is something pretty elemental to do and shouldn’t give any trouble. It’s probably something obvious. :wink:

Usually “nifty.exit();” should work to close the screen. But what is your goal when “removing” the screen exactly?

you add the screen in main.java in your simpleInitApp(), my goal is to eliminate the screen added so i can start my game (or in this case see the box i’ve created)



this should make it clearer:

@Override

public void simpleInitApp() {

//ADDING A BOX (THIS IS SUPPOSE TO BE MY GAME)

Box b = new Box(Vector3f.ZERO, 1, 1, 1);

Geometry geom = new Geometry(“Box”, b);



Material mat = new Material(assetManager, “Common/MatDefs/Misc/Unshaded.j3md”);

mat.setColor(“Color”, ColorRGBA.Blue);

geom.setMaterial(mat);



rootNode.attachChild(geom);




//ADDING THE MENU SCREEN

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/newXMLDocument.xml”, “start”, new MySettingsScreen());

// attach the Nifty display to the gui view port as a processor

guiViewPort.addProcessor(niftyDisplay);

// disable the fly cam

flyCam.setDragToRotate(true);





}

Please next time use the “java” button to put java. That makes code 1000x easier to read.



From what I can deduce from the the code above, your Nifty screen is some kind of “Settings” screen. I imagine for options and stuff to set the new game with.



You probably also have a button to apply those settings and “start” the game. Bound to that button in the XML, there’s probably a method linked to the button, something like[xml] <interact onClick=“startGame()” …>[/xml]



So, with the method above, you set your stuff, then at the end you turn nifty off by using what I posted above" nifty.exit();



Now. The HUGE problem you have is that there’s a screen made each and EVERY frame! You don’t want that. There are many ways to tackle that, the easiest and simplest would be to use a flag.



[java]

// in simpleInit

if (!guiLoaded) {

loadNiftyScreen();

guiLoaded = true;

}

[/java]

1 Like

what If I am trying to remove a specific panel or layer within the screen itself?

say I have a menu of buttons and I would like to remove a ButtonBuilder from it. is that doable?

    screen.removeLayerElement("id");  &gt;&gt; did not remove it

you can try [java]nifty.removeElement()[/java]

or

[java]element.markForRemoval();
element.getParent().layoutElements();[/java]

[java]
screen.findElementByName(“id”).markForRemoval();
[/java]

That code is used to remove elements from nifty. I haven’t tried removing a layer, however, and I’m not sure what effect that would have.

Edit: Wezrule beat me to it! :slight_smile:

[java] screen.findElementByName(“fileDialog”).markForRemoval();[/java]

this will remove it as after putting it the element with the id fileDialog won’t be found. However, I still see the button element attached on the screen although it’s a child of the fileDialog panel.

This is the code that I have for fileDialog element

[java]
LayerBuilder subMenuLayer = new LayerBuilder(“subMenuLayer”) {{
Element panel = new PanelBuilder(“fileDialog”) {{

            ButtonBuilder test1 =  new ButtonBuilder("exit_button", "Exit") {
                {    
                    width("10%");
                    x("25px");
                    y("25px");
                    visibleToMouse(true);
                    this.focusable(false);
                }};

                test1.build(nifty, screen, dialogLayer);

        }}.build(nifty, screen, screen.findElementByName("background"));
    }};[/java]

that works thanks

@homsi said: [java] screen.findElementByName("fileDialog").markForRemoval();[/java]

this will remove it as after putting it the element with the id fileDialog won’t be found. However, I still see the button element attached on the screen although it’s a child of the fileDialog panel.

This is the code that I have for fileDialog element

[java]
LayerBuilder subMenuLayer = new LayerBuilder(“subMenuLayer”) {{
Element panel = new PanelBuilder(“fileDialog”) {{

            ButtonBuilder test1 =  new ButtonBuilder("exit_button", "Exit") {
                {    
                    width("10%");
                    x("25px");
                    y("25px");
                    visibleToMouse(true);
                    this.focusable(false);
                }};

                test1.build(nifty, screen, dialogLayer);

        }}.build(nifty, screen, screen.findElementByName("background"));
    }};[/java]</blockquote>

I’m not sure you figured it out or not, but you are parenting your button incorrectly. The syntax is something like…

[java]
LayerBuilder subMenuLayer = new LayerBuilder(“subMenuLayer”) {{
control(new PanelBuilder(“fileDialog”) {{
control(new ButtonBuilder(“exit_button”, “Exit”) {
{
width(“10%”);
x(“25px”);
y(“25px”);
visibleToMouse(true);
this.focusable(false);
}});

        }});
    }};
    subMenuLayer.build(nifty, screen, screen.findElementByName("background"));

[/java]

1 Like

God, I love you. I have been trying to use AppStateManager to detach nifty menus from my app for the past hour. I had no idea you had to actually exit nifty in order to detach the menu from the screen. This worked great for me.