Right, first off, this is my first post. Hi guys!
Ive been reading up and playing with jME3 for the past 3 days. Have been through almost all of the beginner tutorials, and figured its time to dip my foot into the GUI pond.
Ive read the documentation here on it, and have read other peoples posts and tried their code. My problem is that when I close my nifty instance, my assetManager becomes null. I first tried to do this with a more complex world, but now ive slimmed it down and simplified it to a nifty gui and a rotating Geometry box. I can create the gui then the box, but cannot create gui, close gui, create box.
Code:
Main:
[java]public class Main extends SimpleApplication {
private Geometry box_geo;
private Material box_mat;
private NiftyJmeDisplay niftyDisplay;
public static void main(String[] args) {
Main app = new Main();
app.setPauseOnLostFocus(false);
app.start();
}
public int gameState = 0;
public Nifty nifty;
@Override
public void simpleInitApp() {
if (gameState == 0) {
loadStartScreen();
addNewBox();
}else{
nifty.exit();
guiViewPort.removeProcessor(niftyDisplay);
//addNewBox();
}
}
public void loadStartScreen() {
niftyDisplay = new NiftyJmeDisplay(assetManager, inputManager, audioRenderer, guiViewPort);
nifty = niftyDisplay.getNifty();
nifty.fromXml(“assets/Interface/startscreen.xml”, “start”);
// attach the nifty display to the gui view port as a processor
guiViewPort.addProcessor(niftyDisplay);
// disable the fly cam
flyCam.setEnabled(false);
}
private void addNewBox(){
if(assetManager == null){
System.out.println(“NULL AM”);
}
box_mat = new Material(assetManager,“Common/MatDefs/Misc/Unshaded.j3md”);
Texture boxTexture = assetManager.loadTexture(“Interface/Logo/Monkey.jpg”);
box_mat.setTexture(“ColorMap”, boxTexture);
Box box = new Box(Vector3f.ZERO,1f,1f,1f);
box_geo = new Geometry(“Box”,box);
box_geo.setMaterial(box_mat);
box_geo.setLocalTranslation(0,0,0);
rootNode.attachChild(box_geo);
}
@Override
public void simpleUpdate(float tpf){
if(box_geo!=null){
box_geo.rotate(0,2*tpf,0);
}
}
}[/java]
MainScreen:
[java]public class MainGameScreen extends Main implements ScreenController {
public void bind(Nifty nifty, Screen screen) {
this.nifty=nifty;
}
public void onStartScreen() { }
public void onEndScreen() { }
public void startGame(){
System.out.println(“DO START”);
gameState=1;
simpleInitApp();
}
public void
quit() {
nifty.exit();
gameState = 1;
System.exit(0);
}
}[/java]
The code has no problem running how it is. It starts with GUI, with rotating cube behind it. Swap the commenting of the addNewBox() in simpleInnitApp and it breaks. I tried it without the call the guiViewPort with the same results. With it, it turns out the guiViewPort is null as well. I feel like ive missed something really small somewhere, and cant for the life of me figure out what it is or where it should go.
Any help appreciated.
EDIT: Even after changing nifty to go to another screen, eveything is still null. Where am I going wrong to kill the assetManager and guiViewPort and stuff?
EDIT2: Ok, I did a bit more reading, and remember the third parameter for creating NiftyDisplay. I was using a different instance of a simple application because the controller class in the nifty xml was instantiated fresh so was a different instance. Fixed by either making the class implement SurfaceController and pass in ‘this’, or instantiating the controller class, passing in ‘this’, and then passing that into the NiftyDisplay.