AppStates

[EDIT]: ah never mind, added state.update(tpf); before ok = true; and it works. Got all working, what I asked.



Why I cant create AppState in simpleUpdate() -method? It throws an exception:

java.lang.IllegalStateException: Scene graph is not properly updated for rendering.

Make sure scene graph state was not changed after

rootNode.updateGeometricState() call.



I can create them in simpleInitApp() but I dont want create ALL AppStates at the beginning.

I should be able to create AppState inside an AppState too?



Well, there is a showcase:



package mygame;

import com.jme3.app.SimpleApplication;

import com.jme3.app.state.AbstractAppState;

import com.jme3.scene.Node;

import com.jme3.scene.Spatial;



public class Main extends SimpleApplication

{

public static void main(String[] args)

{

Main app = new Main();

app.setShowSettings(false);

app.start();

}



@Override

public void simpleInitApp() { }



boolean ok = false;



@Override

public void simpleUpdate(float tpf)

{

if (ok == false) // attach appstate only once

{

TestState state = new TestState(this);

viewPort.attachScene(state.GetRootNode());

stateManager.attach(state);

ok = true;

}

}

}



class TestState extends AbstractAppState

{

static int Count = 0;

Node rootNode = new Node(“AppStateRootNode” + Count++);

Spatial scene;



public TestState(SimpleApplication app)

{

scene = app.getAssetManager().loadModel(“Models/Menu/menu.scene”);

rootNode.attachChild(scene);

initialized = true;

}



public Node GetRootNode()

{

return rootNode;

}



@Override

public void update(float tpf)

{

rootNode.updateLogicalState(tpf);

rootNode.updateGeometricState();

}

}

Try this:

[java]

state.GetRootNode().updateLogicalState();

state.GetRootNode().updateGeometricState();

viewPort.attachScene(state.GetRootNode());

[/java]

Yes, just noticed that I needed those updates before simpleUpdate() ends.

You should do the attaching to viewPort in the initialize() method of your AppState, then this isn’t necessary.