Main menu for a game

Hey,
I was wondering how to make a main menu for a game. I don’t know what tool to use, so it would be nice if someone could help me with that. Also, I was asking myself if the class with the menu is suppose to have the main method in it for a simple Application. In addition to all this, if ,example, I place a button to start the game, should it trigger the app.start() instruction.

Thanks and don’t be tough with me, I’m new with all this programming stuff:)

http://hub.jmonkeyengine.org/wiki/doku.php/jme3:advanced:nifty_gui

Also see the sample code in JME3 Tests:

JmeTests/src/jme3test/TestChooser.java
JmeTests/src/jme3test/games/CubeField.java

Three different ways to do what you’re asking. It’s really up to you, but you might want to start here:

http://hub.jmonkeyengine.org/wiki/doku.php/jme3

2 Likes

What @charlesg said, and in addition you can read this doc about nifty http://freefr.dl.sourceforge.net/project/nifty-gui/nifty-gui/nifty-gui-the-manual-v1.0.pdf

In addition to all this, if ,example, I place a button to start the game, should it trigger the app.start() instruction.
No, the menu is integrated to the game, you should look into AppState (in JME doc) and make your menu switch between different Appstates

Thanks a lot!:slight_smile:

Heyho guys!

I followed the tutorial till the last steps, but when I run the game, the GUI won’t show, like it never existed. What couldb e the problem?

Thanks in advance!

6 Likes

Oh, right, my bad:D

Here it is the xml file:
[java]
<?xml version=“1.0” encoding=“UTF-8”?>
<nifty xmlns=“http://nifty-gui.sourceforge.net/nifty-1.3.xsd” xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance” xsi:schemaLocation=“http://nifty-gui.sourceforge.net/nifty-1.3.xsd http://nifty-gui.sourceforge.net/nifty-1.3.xsd”>
<useStyles filename=“nifty-default-styles.xml” />
<useControls filename=“nifty-default-controls.xml” />
<screen id=“start” controller=“mygame.MyStartScreen”>
<layer id=“background” childLayout=“center”>
<image filename=“Interface/Images/Tropic_Night.jpg”></image>
</layer>
<layer id=“foreground” backgroundColor="#0000" childLayout=“vertical”>
<panel id=“panel_top” height=“25%” width=“75%” align=“center” childLayout=“center”>
<text text=“The Provinces Of Dintena” font=“Interface/Fonts/Default.fnt” width=“100%” height=“100%” />
</panel>
<panel id=“panel_mid” height=“50%” width=“75%” align=“center” childLayout=“center”>
<text text="${CALL.getPlayerName()}'s Cool Game" font=“Interface/Fonts/Default.fnt” width=“100%” height=“100%” />
</panel>
<panel id=“panel_bottom” height=“25%” width=“75%” align=“center” childLayout=“horizontal”>
<panel id=“panel_bottom_left” height=“50%” width=“50%” valign=“center” childLayout=“center”>
<control name=“button” label=“Start” id=“StartButton” align=“center” valign=“center” visibleToMouse=“true”>
<interact onClick=“startGame(hud)”/>
</control>
</panel>
<panel id=“panel_bottom_right” height=“50%” width=“50%” valign=“center” childLayout=“center” >
<control name=“button” label=“Quit” id=“QuitButton” align=“center” valign=“center” visibleToMouse=“true”>
<interact onClick=“quitGame()”/>
</control>
</panel>
</panel>
</layer>
</screen>
<screen id=“hud”>
<layer id=“background” childLayout=“center”>

    &lt;/layer&gt;
    &lt;layer id="foreground" backgroundColor="#0000" childLayout="horizontal"&gt;
        &lt;panel id="panel_left" width="80%" height="100%" childLayout="vertical"&gt;
            
        &lt;/panel&gt;
        &lt;panel id="panel_right" width="20%" height="100%" childLayout="vertical"&gt;  
            &lt;panel id="panel_top_right1" width="100%" height="15%" childLayout="center" backgroundColor="#00f8"&gt;
                &lt;control name="label" color="#000" text="123" width="100%" height="100%" /&gt;
            &lt;/panel&gt;
            &lt;panel id="panel_top_right2" width="100%" height="15%" childLayout="center"&gt;
                
            &lt;/panel&gt;
            &lt;panel id="panel_bot_right" width="100%" height="70%" valign="center"&gt;
                
            &lt;/panel&gt;
        &lt;/panel&gt;
    &lt;/layer&gt;
&lt;/screen&gt;

</nifty>
[/java]

This is the Main class:
[java]
package mygame;

import com.jme3.app.SimpleApplication;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.renderer.RenderManager;
import com.jme3.scene.Geometry;
import com.jme3.scene.shape.Box;

/**

  • test

  • @author normenhansen
    */
    public class Main extends SimpleApplication {

    public static void main(String[] args) {
    Main app = new Main();
    app.start();

    }

    @Override
    public void simpleInitApp() {
    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);
    

    }

    @Override
    public void simpleUpdate(float tpf) {
    //TODO: add update code
    }

    @Override
    public void simpleRender(RenderManager rm) {
    //TODO: add render code
    }
    }
    [/java]

And last but not least the MyStartScreen class:
[java]
package mygame;

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;

/**
*

  • @author Rudolf Hergat
    */
    public class MyStartScreen extends AbstractAppState implements ScreenController {

    private Nifty nifty;
    private Screen screen;
    private SimpleApplication app;

    public MyStartScreen(String data) {
    }

    public void bind(Nifty nifty, Screen screen) {
    this.nifty = nifty;
    this.screen = screen;
    }

    public void onStartScreen() {
    //Unused
    }

    public void onEndScreen() {
    //Unused
    }

    @Override
    public void initialize(AppStateManager stateManager, Application app) {
    super.initialize(stateManager, app);
    this.app = (SimpleApplication) app;
    }

    @Override
    public void update(float tpf) {
    }

    public void startGame(String nextScreen) {
    nifty.gotoScreen(nextScreen); // switch to another screen
    // start the game and do some more stuff…
    }

    public void quitGame() {
    app.stop();
    }

    public String getPlayerName(){
    return System.getProperty(“user.name”);
    }
    }
    [/java]

I did everything as the tutorial said, but it didn’t wokr at me. Actually I’m not so suprised, cuz in the tutorial I didn’t see anything about “turning on” the GUI.
PS.: I slept 5 hours in the last 3 days, so I’m pretty sure I did something wrong.

You haven’t initialized nifty.

This is missing in your simpleInitApp-Method:

NiftyJmeDisplay display = new NiftyJmeDisplay(assetManager, inputManager, audioRenderer, viewPort); //create jme-nifty-processor
guiViewPort.addProcessor(display); //add it to your gui-viewport so that the processor will start working
Nifty nifty = display.getNifty();
nifty.gotoScreen(“yourWishedScreenId”); //load the screen of choice

1 Like

I copied the code into the SimpleInitApp method, but it didn’t change nothing:/

show us the new code you have tried, mainly simpleInitApp (assuming you haven’t changed anything else). Also what nifty warnings/errors are you getting?

1 Like

There are no errors or exceptions at all negihter at the Design view of the xml and negihter in the log when I run it. I think the problem coudl be that I didn’t “connect” the GUI to the game at some palce where I should have.

Here is the Main class after I added the code ceiphren suggested:
[java]
package mygame;

import com.jme3.app.SimpleApplication;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.niftygui.NiftyJmeDisplay;
import com.jme3.renderer.RenderManager;
import com.jme3.scene.Geometry;
import com.jme3.scene.shape.Box;
import de.lessvoid.nifty.Nifty;

/**

  • test

  • @author normenhansen
    */
    public class Main extends SimpleApplication {

    public static void main(String[] args) {
    Main app = new Main();
    app.start();

    }

    @Override
    public void simpleInitApp() {

     NiftyJmeDisplay display = new NiftyJmeDisplay(assetManager, inputManager, audioRenderer, viewPort); //create jme-nifty-processor
     guiViewPort.addProcessor(display); //add it to your gui-viewport so that the processor will start working
     Nifty nifty = display.getNifty();
     nifty.gotoScreen("start"); //load the screen of choice
     
     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);
    

    }

    @Override
    public void simpleUpdate(float tpf) {
    //TODO: add update code
    }

    @Override
    public void simpleRender(RenderManager rm) {
    //TODO: add render code
    }
    }
    [/java]

Change that to:

[java]MyStartScreen startScreen = new MyStartScreen ();
NiftyJmeDisplay display = new NiftyJmeDisplay(assetManager, inputManager, audioRenderer, viewPort); //create jme-nifty-processor
guiViewPort.addProcessor(display); //add it to your gui-viewport so that the processor will start working
Nifty nifty = display.getNifty();
nifty.fromXML(“Interface/xmlNameGoes.xml”, “start”, startScreen);[/java]

1 Like

It’s working! Thank you guys!:smiley:

I think what I’m doing in this topic is pushing the limits of harresment, but I’ve got another problem:D
As the tutorial said, I can call functions from the XML file, as it was writen in the example:
[java]
<text text="${CALL.getPlayerName()}'s Cool Game" font=“Interface/Fonts/Default.fnt” width=“100%” height=“100%” />
[/java]

It’s working properly on the start screen, but only show the original “${CALL.getPlayerName()}'s Cool Game” text on the hud screen, instead of the return value.
I was checking around on the net and the forum, but couldn’t find anything about it.

Never mind, I fixed it. I forgot to add the control to the hud screen. What a noob move:D But I have antoher problem. I wrote a function, and it doesn’t seem to work, but I don’t get why.
The function is in the MyStartScreen class:
[java]
public String getPlayerLuck(){
return Integer.toString(app.player.luck);
}
[/java]
app is the Main class, player is a field in Main, Player type, and luck is a field in Player class, integer type.

Did you put a println in it to see if it is even being called? Or set a breakpoint in the debugger? etc.?

1 Like

Yeah I did. If I call the function from the Main class then it’s working, so the fucntion itself is okay. Something must be in the xml.

Is this from your hud screen? because you haven’t given it a controller in the xml file.

[java]<screen id=”hud”>[/java]

(nevermind)

@wezrule yeah I found the problem and fixed it, and now the getPlayerName() works, but the getPlayerLuck() won’t.