I’m new at jme3 and at first I wanted to make a kart game like Mario Kart so I started with a simple GUI to learn about AppStates and so on.
I followed the Instructions from the jme3 Nifty Gui
But if i click the button the startGame() method of the screencontroller isn’t triggered.
Here’s the XML code:
[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=“titleScreen” controller=“kartGame.TitleScreenState”>
<layer id=“background” childLayout=“center”>
<image filename=“Interface/start-background.png”></image>
</layer>
<layer id=“foreground” backgroundColor="#0000" childLayout=“vertical” visibleToMouse=“true”>
<panel id=“panel_top” height=“25%” width=“75%” align=“center” childLayout=“center”>
<text text=“My Cool Kart Game” font=“Interface/Fonts/Default.fnt” width=“100%” height=“100%” />
</panel>
<panel id=“panel_mid” height=“25%” width=“75%” align=“center” childLayout=“center”>
<text text=“This Game is still under Developing and this Screen is just for testing. Development Stadium is still far away from pre-alpha so: Use on your on own risk!”
font=“Interface/Fonts/Default.fnt” width=“100%” height=“100%” wrap=“true” />
</panel>
<panel id=“panel_bottom_1” height=“15%” width=“75%” valign=“center” childLayout=“center” visibleToMouse=“true”>
<control name=“button” label=“Start” id=“StartButton” align=“center” valign=“center”
visibleToMouse=“true” >
<interact onClick=“startGame()”/>
</control>
</panel>
<panel id=“panel_bottom_2” height=“15%” width=“75%” valign=“center” childLayout=“center” visibleToMouse=“true”>
<control name=“button” label=“Quit” id=“QuitButton” align=“center” valign=“center”
visibleToMouse=“true” >
<interact onClick=“quitGame()”/>
</control>
</panel>
</layer>
</screen>
</nifty>[/xml]
And here’s the TitleScreenStateClass wich implements the ScreenController.
[java]public class TitleScreenState extends AbstractAppState implements ScreenController {
private final ColorRGBA backgroundColor = ColorRGBA.Gray;
private Nifty nifty;
private Screen screen;
private KartGame game;
private NiftyJmeDisplay niftyDisplay;
public TitleScreenState(KartGame game){
this.game = game;
}
@Override
public void initialize(AppStateManager stateManager, Application app) {
super.initialize(stateManager, app);
niftyDisplay = new NiftyJmeDisplay(game.getAssetManager(),
game.getInputManager(),
game.getAudioRenderer(),
game.getGuiViewPort());
nifty = niftyDisplay.getNifty();
nifty.fromXml(“Interface/TitleScreen.xml”, “titleScreen”);
game.getGuiViewPort().addProcessor(niftyDisplay);
}
@Override
public void update(float tpf) {
/** any main loop action happens here */
}
@Override
public void stateAttached(AppStateManager stateManager) {
game.getFlyByCamera().setEnabled(false);
}
@Override
public void stateDetached(AppStateManager stateManager) {
game.getGuiNode().detachAllChildren();
game.getGuiViewPort().removeProcessor(niftyDisplay);
game.getFlyByCamera().setEnabled(true);
}
public void bind(Nifty nifty, Screen screen) {
this.nifty = nifty;
this.screen = screen;
}
public void onStartScreen() {
}
public void onEndScreen() {
}
public void startGame(){
//game.startTrackRace();
throw new RuntimeException(“Works”);
// I put an exception here that i defenitely note if the method is called or not
}
}[/java]
How do you instantiate TitleScreenState?
My thread might have some interesting information for you - read through it.
http://hub.jmonkeyengine.org/groups/gui/forum/topic/interact-onclick-not-working/
That’s an easy one to fix…
Since you don’t provide an instance of the ScreenController in your fromXML() call, Nifty tries to instantiate it via the default constructor. Since you don’t have a default constructor in your ScreenController, this will not be possibe, so the ScreenController will not be instantiated. As a result, there is no way for nifty to call your startGame() method on the ScreenController.
Adding a default controller should fix this. Or, if you want to put in the the current instance, you could pass it as a parameter to the fromXML() method.