[SOLVED] Nifty button does not do anything…

@javagame said:
[java]
@Override
public void setEnabled(boolean enabled) {
// Pause and unpause
super.setEnabled(enabled);
if (enabled) {
this.guiViewPort.addProcessor(startMenu);
flyCam.setEnabled(false);
flyCam.setDragToRotate(true);

nifty.getScreen("start").findElementByName("window").setVisible(false);
} else {
}
}
[/java]


this line:
[java]nifty.getScreen("start").findElementByName("window").setVisible(false);[/java]

you don't have an element with id window? and also that if setEnabled(true) is called more than once, than you would add the processor again (not sure if its actually an issue tho)

also make sure that everything is how it should be with [java]nifty.setDebugOptionPanelColors(true);[/java]

I personally use JavaBuilders, but it won’t be different with XML. Here’s what may be the problem (and poo to me if someone already mentioned this)



You have the MyScreenControllerClass which implements ScreenController. This gives you a method called bind(Nifty nifty, Screen screen). In this method, call the following:



[java]

Element button1 = nifty.getScreen(“Whatever your screen ID is”).findElementByName(“Whatever your button ID is”);

//then down below you can do button1.setEnabled(true); or whatever else you want to do.

[/java]



That’s not a 100% guarantee that’s the problem, but trying to bind it could help.



You could also try adding a String resource to your startPressed() method, so it’s like the following:



[java]

public void startPressed(String confirmation) {

System.out.println(confirmation);

}

[/java]



And in the XML:



[xml]

<interact onClick=“startPressed(Button1)”/>

[/xml]



Or button3 for that confirmation string, or however you want to do it so you can see if it’s just a specific button or all of them. Hope that helped!

I do have window, i just updated the XML sorry don’t know why that wasn’t in the code originally, but err, why do i need to add Button1 to to that method call?

This works for me (abbreviated to show the relevant parts):



[java]public class Main extends SimpleApplication implements ScreenController {



private Nifty nifty;

private final Logger log ;



@Override

public void simpleInitApp() {



NiftyJmeDisplay niftyDisplay = new NiftyJmeDisplay(assetManager,

inputManager,

audioRenderer,

guiViewPort);

guiViewPort.addProcessor(niftyDisplay);

nifty = niftyDisplay.getNifty();



flyCam.setEnabled(false);



nifty.fromXml("/UI/start.xml", "start", this);

}



public void runGame() {

}

}[/java]



[xml]<?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="nu.zoom.mj.Main">



<panel id="panel" height="12%" width="35%" align="center" valign="center" childLayout="center" visibleToMouse="false">

<control id="runButton" name="button" label="Start game">

<effect>

<onStartScreen name="move" mode="in" direction="top" length="300" startDelay="0" inherit="true"/>

</effect>

<interact onClick="runGame()"/>

</control>

</panel>[/xml]

@jmaasing How can you tell your runGame() is being called?

@javagame I added the Button1 string to it so you can do the following:



In XML you’d do this:

[xml]

<control id=“submitButton1” type=“button” label=“Connect to a server” height=“30%” width=“70%” visibleToMouse=“true”>

<interact onClick=“startPressed(submitButton1)” />

</control>

<control id=“submitButton2” type=“button” label=“Button 2” height=“30%” width=“70%” visibleToMouse=“true”>

<interact onClick=“startPressed(Button2)” />

</control>

[/xml]



This way you get an output of which buttons worked and which didn’t.

1 Like

@vinexgames oh right i see, makes it easier I guess, but at the minute NO buttons are working

@javagame said:
jmaasing How can you tell your runGame() is being called?


Sorry, I should have written "..." in there. To show that I wrote stuffs there and then stuffs happened :)

ah right. Perhaps it is also worth noting that the window I create cannot be dragged around…

@jmaasing but I used your code, after expanding upon it and what is supposed to happen? the method does not appear to be called. In my project, I have found that when I make a window it cannot be dragged around at all, adding this to the button problem I am now convinced that this is a bug because I really cant find any error with my code, and I’ve been staring at it for over a week now. I also tested it on a different pc with eclipse and the latest nightly (I use stable and JMP at home).



If anyone is willing, I can upload the latest version of my code to be tested…

I suggest you make a test case instead (as minimal as you can make it). If that doesn’t work post it here.

How do I do that, just gut my code to the bare min and upload that?

Do I upload the whole project?

Write a new SimpleApplication. It shouldn’t have to be more than the main class, boil down your problem to as few lines of code as you can.



Try to use existing resources in jme-test-data or make a minimal nifty.xml. Paste the code in code tags here. That’s probably the best way to get people to look at it.



I think most devs. don’t have the time to download some zip-file, unpack it, import et c. so you probably won’t get anyone to look at it then.

1 Like

Ideally condense it down to a single java file, the bare minimum that shows the problem…then put it inside code tags.

1 Like

That will not be possible, as it works when run from just the main class, its only in a separate App State that I get a problem. I have been reading up a bit more on App States, I’ll try out some more thing then come back when I’ve had another shot at debugging it

That’s one of the things with test cases. A lot of the time just making the test case is enough to find the problem.

1 Like
@zarch said:
That's one of the things with test cases. A lot of the time just making the test case is enough to find the problem.


Whoever first came p with the idea for test cases was a genius :)

Oh god, I found out that I wasn’t passing the Input Manager from main to my app state, that took over a week out of my project, thanks for your patience. I spent soo long trying to debug this feels great now to have it finally working.