AppStates

Hi Forum,

I’m attempting to write a simple game similar to ‘temple run’ and so far I’ve created a character in blender with ‘walk’, ‘jump’ and ‘slide’ animations which I then loaded onto jme3 and assigned triggers to each of the animations which work perfectly fine. However I need to be able to pause and unpause the game whenever I want and I’ve already looked into AppStates but problem is, I don’t really understand how to use it in my code which is as follows :
package game;

import com.jme3.animation.AnimChannel;
import com.jme3.animation.AnimControl;
import com.jme3.animation.AnimEventListener;
import com.jme3.animation.LoopMode;
import com.jme3.app.Application;
import com.jme3.app.SimpleApplication;
import com.jme3.app.state.AbstractAppState;
import com.jme3.app.state.AppStateManager;
import com.jme3.cinematic.MotionPath;
import com.jme3.input.KeyInput;
import com.jme3.input.controls.AnalogListener;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.light.DirectionalLight;
import com.jme3.math.ColorRGBA;
import com.jme3.math.FastMath;
import com.jme3.math.Vector3f;
import com.jme3.scene.CameraNode;
import com.jme3.scene.Node;
import com.jme3.scene.control.CameraControl.ControlDirection;
import com.jme3.cinematic.events.MotionTrack;

/** Sample 7 - how to load an OgreXML model and play an animation,

  • using channels, a controller, and an AnimEventListener. */
    public class MyGame extends SimpleApplication
    implements AnimEventListener {
    private AnimChannel channel;
    private AnimControl control;
    Node player;
    CameraNode camNode;
    public static void main(String[] args) {
    MyGame app = new MyGame();
    app.start();
    }

@Override
public void simpleInitApp() {
viewPort.setBackgroundColor(ColorRGBA.White);
initKeys();
DirectionalLight dl = new DirectionalLight();
dl.setDirection(new Vector3f(200f, 200f, 200f).normalizeLocal());
rootNode.addLight(dl);
player = (Node) assetManager.loadModel(“Models/N0name/Cube.mesh.j3o”);
player.rotate(-90FastMath.DEG_TO_RAD, 0f, 0f);
player.setLocalScale(0.1f);
rootNode.attachChild(player);
control = player.getControl(AnimControl.class);
control.addListener(this);
channel = control.createChannel();
// Disable the default flyby cam
flyCam.setEnabled(false);
//create the camera Node
camNode = new CameraNode(“Camera Node”, cam);
//This mode means that camera copies the movements of the target:
camNode.setControlDir(ControlDirection.SpatialToCamera);
//Attach the camNode to the target:
player.attachChild(camNode);
//Move camNode, e.g. behind and above the target:
camNode.setLocalTranslation(new Vector3f(0, 20, 27));
//Rotate the camNode to look at the target:
camNode.lookAt(player.getLocalTranslation(), Vector3f.UNIT_Y);
camNode.rotate(75
FastMath.DEG_TO_RAD,0,0);
motionpaths();

}

public void onAnimCycleDone(AnimControl control, AnimChannel channel, String animName) {
if (animName.equals(“Jump”)){
channel.setAnim(“Walk”,0.4f);
channel.setLoopMode(LoopMode.Loop);
channel.setSpeed(2f);
}else if (animName.equals(“Slide”)){
channel.setAnim(“Walk”,0.4f);
channel.setLoopMode(LoopMode.Loop);
channel.setSpeed(2f);
}
}

public void onAnimChange(AnimControl control, AnimChannel channel, String animName) {

}
private void motionpaths(){
MotionPath mdlbtm = new MotionPath();
mdlbtm.addWayPoint(new Vector3f(0,0,30));
mdlbtm.addWayPoint(new Vector3f(0,0,0));
mdlbtm.enableDebugShape(assetManager, rootNode);
MotionTrack mdlbtmmtntrck = new MotionTrack(player, mdlbtm);
mdlbtmmtntrck.setDirectionType(MotionTrack.Direction.Path);
//motionTrack.setEnabled(true);
MotionPath mdltop = new MotionPath();
mdltop.addWayPoint(new Vector3f(0,2,30));
mdltop.addWayPoint(new Vector3f(0,2,0));
mdltop.enableDebugShape(assetManager, rootNode);
MotionTrack mdltopmtntrck = new MotionTrack(player, mdltop);
mdltopmtntrck.setDirectionType(MotionTrack.Direction.Path);
//motionTrack.setEnabled(true);
MotionPath toplft = new MotionPath();
toplft.addWayPoint(new Vector3f(-1,2,30));
toplft.addWayPoint(new Vector3f(-1,2,0));
toplft.enableDebugShape(assetManager, rootNode);
MotionTrack toplftmtntrck = new MotionTrack(player, toplft);
toplftmtntrck.setDirectionType(MotionTrack.Direction.Path);
//motionTrack.setEnabled(true);
MotionPath btmleft = new MotionPath();
btmleft.addWayPoint(new Vector3f(-1,0,30));
btmleft.addWayPoint(new Vector3f(-1,0,0));
btmleft.enableDebugShape(assetManager, rootNode);
MotionTrack btmleftmtntrck = new MotionTrack(player, btmleft);
btmleftmtntrck.setDirectionType(MotionTrack.Direction.Path);
//motionTrack.setEnabled(true);
MotionPath toprght = new MotionPath();
toprght.addWayPoint(new Vector3f(1,2,30));
toprght.addWayPoint(new Vector3f(1,2,0));
toprght.enableDebugShape(assetManager, rootNode);
MotionTrack toprghtmtntrck = new MotionTrack(player, toprght);
toprghtmtntrck.setDirectionType(MotionTrack.Direction.Path);
//motionTrack.setEnabled(true);
MotionPath btmrght = new MotionPath();
btmrght.addWayPoint(new Vector3f(1,0,30));
btmrght.addWayPoint(new Vector3f(1,0,0));
btmrght.enableDebugShape(assetManager, rootNode);
MotionTrack btmrghttmtntrck = new MotionTrack(player, btmrght);
btmrghttmtntrck.setDirectionType(MotionTrack.Direction.Path);
//motionTrack.setEnabled(true);
}

/** Custom Keybinding: Map named actions to inputs. */
private void initKeys() {
inputManager.addMapping(“Slide”, new KeyTrigger(KeyInput.KEY_SPACE));
inputManager.addMapping(“Walk”, new KeyTrigger(KeyInput.KEY_W));
inputManager.addMapping(“Jump”, new KeyTrigger(KeyInput.KEY_J));
inputManager.addListener(actionListener, “Slide”,“Walk”,“Jump”);
}
private AnalogListener actionListener = new AnalogListener() {
public void onAnalog(String name, float value, float tpf) {
if (name.equals(“Slide”)) {
channel.setAnim(“Slide”, 1f);
channel.setLoopMode(LoopMode.DontLoop);
}else if (name.equals(“Jump”)) {
channel.setAnim(“Jump”,1f);
channel.setLoopMode(LoopMode.DontLoop);
}else {
channel.setAnim(“Walk”,1f);
channel.setLoopMode(LoopMode.Loop);
channel.setSpeed(2);
}
}
};
}
I’m really sorry if the answer is obvious but I honestly do fail to see how to use it in my code and if anyone could please help me out, I’d greatly appreciate it :slight_smile:

Thanks in Advance
Dan :slight_smile:

To pause you just stop the update loop running (with a boolean or something) or disable appstates/controls and disable any animations/particles. For appstates in general you implement AbstractAppstate use the methods, create it and attach to the statemanager, check the tutorials/wiki docs for this.

I’ve gone through the wikis but how and where do I do what?? I’ve read through it many times and I’m still very confused, If you could please break it down so that an absolute newb such as myself could understand it, It’d be GREAT :slight_smile:

Thanks in advance,
Dan

Normally, you just have to call your applicationStates.setEnable(false).
Their update method will not be called anymore until you enable it again.