I have succeed in creating and initializing a state. But, Now I don’t know how to change or/add more state, so one state can monitor a certain condition to check, if other state should be initialized.
A guideline would help.
I am using the following 2 classes,
[java]/*
- To change this template, choose Tools | Templates
- and open the template in the editor.
*/
package RFP.core;
import RFP.states.StartMenuState;
import com.jme3.app.Application;
import com.jme3.bullet.BulletAppState;
/**
*
-
@author irfan
/
public class RFPGame extends Application {
BulletAppState bulletAppState = null;
StartMenuState startMenuState = null;
public RFPGame() {
super();
}
@Override
public void start() {
super.start();
}
@Override
public void initialize() {
super.initialize();
bulletAppState = new BulletAppState();
startMenuState = new StartMenuState(this);
getStateManager().attach(startMenuState);
}
@Override
public void update() {
super.update(); // makes sure to execute AppTasks
if (speed == 0 || paused) {
return;
}
float tpf = timer.getTimePerFrame() * speed;
stateManager.update(tpf);
// simpleUpdate(tpf);
stateManager.render(renderManager);
renderManager.render(tpf, context.isRenderable());
stateManager.postRender();
}
public static void main(String… args){
new RFPGame().start();
}
}
[/java]
State class :
[java]/
- To change this template, choose Tools | Templates
- and open the template in the editor.
*/
package RFP.states;
import RFP.core.RFPGame;
import com.jme3.app.Application;
import com.jme3.app.state.AbstractAppState;
import com.jme3.app.state.AppStateManager;
import com.jme3.input.KeyInput;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.scene.Node;
/**
*
-
@author irfan
*/
public class StartMenuState extends AbstractAppState implements ActionListener{
private RFPGame RFPGameObject;
private Node rootNode = new Node("StartMenu Root Node");
private Node guiNode = new Node("StartMenu GUI Node");
public StartMenuState(RFPGame RFPGameObject) {
this.RFPGameObject = RFPGameObject;
}
public void onAction(String name, boolean isPressed, float tpf) {
if (name.equals("Lefts") && !isPressed) {
System.out.println("StartMenu Start working!");
}
}
@Override
public void stateAttached(AppStateManager stateManager) {
}
@Override
public void stateDetached(AppStateManager stateManager) {
RFPGameObject.getViewPort().detachScene(guiNode);
RFPGameObject.getViewPort().detachScene(rootNode);
}
@Override
public void initialize(AppStateManager stateManager, Application app) {
super.initialize(stateManager, app);
// test method
setupKey();
RFPGameObject.getViewPort().attachScene(guiNode);
RFPGameObject.getViewPort().attachScene(rootNode);
}
@Override
public void update(float tpf) {
super.update(tpf);
// super :: public void update(float tpf){}
rootNode.updateLogicalState(tpf);
guiNode.updateLogicalState(tpf);
rootNode.updateGeometricState();
guiNode.updateGeometricState();
}
private void setupKey(){
RFPGameObject.getInputManager().addMapping("W", new KeyTrigger(KeyInput.KEY_W));
RFPGameObject.getInputManager().addListener(this, "W");
// getInputManager() is inside Application Class
}
}
[/java]