Skip to content

Commit 231f6a5

Browse files
authored
convert HelloAnimation to the new animation system (#1487)
* convert HelloAnimation to the new animation system * HelloAnimation: more correct action * HelloAnimation: simplify using Action.setLength() * HelloAnimation: callback needn't be public
1 parent 38fe0d1 commit 231f6a5

File tree

1 file changed

+47
-41
lines changed

1 file changed

+47
-41
lines changed

jme3-examples/src/main/java/jme3test/helloworld/HelloAnimation.java

Lines changed: 47 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,12 @@
3232

3333
package jme3test.helloworld;
3434

35-
import com.jme3.animation.*;
35+
import com.jme3.anim.AnimComposer;
36+
import com.jme3.anim.tween.Tween;
37+
import com.jme3.anim.tween.Tweens;
38+
import com.jme3.anim.tween.action.Action;
39+
import com.jme3.anim.tween.action.BlendSpace;
40+
import com.jme3.anim.tween.action.LinearBlendSpace;
3641
import com.jme3.app.SimpleApplication;
3742
import com.jme3.input.KeyInput;
3843
import com.jme3.input.controls.ActionListener;
@@ -42,14 +47,14 @@
4247
import com.jme3.math.Vector3f;
4348
import com.jme3.scene.Node;
4449

45-
/** Sample 7 - how to load an OgreXML model and play an animation,
46-
* using channels, a controller, and an AnimEventListener. */
47-
public class HelloAnimation extends SimpleApplication
48-
implements AnimEventListener {
50+
/**
51+
* Sample 7 - Load an OgreXML model and play some of its animations.
52+
*/
53+
public class HelloAnimation extends SimpleApplication {
4954

55+
private Action advance;
56+
private AnimComposer control;
5057
private Node player;
51-
private AnimChannel channel;
52-
private AnimControl control;
5358

5459
public static void main(String[] args) {
5560
HelloAnimation app = new HelloAnimation();
@@ -67,52 +72,53 @@ public void simpleInitApp() {
6772
rootNode.addLight(dl);
6873

6974
/** Load a model that contains animation */
70-
player = (Node) assetManager.loadModel("Models/Oto/OtoOldAnim.j3o");
75+
player = (Node) assetManager.loadModel("Models/Oto/Oto.mesh.xml");
7176
player.setLocalScale(0.5f);
7277
rootNode.attachChild(player);
7378

74-
/** Create a controller and channels. */
75-
control = player.getControl(AnimControl.class);
76-
control.addListener(this);
77-
channel = control.createChannel();
78-
channel.setAnim("stand");
79-
}
79+
/* Use the model's AnimComposer to play its "stand" animation clip. */
80+
control = player.getControl(AnimComposer.class);
81+
control.setCurrentAction("stand");
8082

81-
/** Use this listener to trigger something after an animation is done. */
82-
@Override
83-
public void onAnimCycleDone(AnimControl control, AnimChannel channel, String animName) {
84-
if (animName.equals("Walk")) {
85-
/** After "walk", reset to "stand". */
86-
channel.setAnim("stand", 0.50f);
87-
channel.setLoopMode(LoopMode.DontLoop);
88-
channel.setSpeed(1f);
89-
}
83+
/* Compose an animation action named "halt"
84+
that transitions from "Walk" to "stand" in half a second. */
85+
BlendSpace quickBlend = new LinearBlendSpace(0f, 0.5f);
86+
Action halt = control.actionBlended("halt", quickBlend, "stand", "Walk");
87+
halt.setLength(0.5);
88+
89+
/* Compose an animation action named "advance"
90+
that walks for one cycle, then halts, then invokes onAdvanceDone(). */
91+
Action walk = control.action("Walk");
92+
Tween doneTween = Tweens.callMethod(this, "onAdvanceDone");
93+
advance = control.actionSequence("advance", walk, halt, doneTween);
9094
}
9195

92-
/** Use this listener to trigger something between two animations. */
93-
@Override
94-
public void onAnimChange(AnimControl control, AnimChannel channel, String animName) {
95-
// unused
96+
/**
97+
* Callback to indicate that the "advance" animation action has completed.
98+
*/
99+
void onAdvanceDone() {
100+
/**
101+
* Play the "stand" animation action.
102+
*/
103+
control.setCurrentAction("stand");
96104
}
97105

98-
/** Custom Keybindings: Mapping a named action to a key input. */
106+
/**
107+
* Map the spacebar to the "Walk" input action, and add a listener to initiate
108+
* the "advance" animation action each time it's pressed.
109+
*/
99110
private void initKeys() {
100111
inputManager.addMapping("Walk", new KeyTrigger(KeyInput.KEY_SPACE));
101-
inputManager.addListener(actionListener, "Walk");
102-
}
103112

104-
/** Definining the named action that can be triggered by key inputs. */
105-
final private ActionListener actionListener = new ActionListener() {
106-
@Override
107-
public void onAction(String name, boolean keyPressed, float tpf) {
108-
if (name.equals("Walk") && !keyPressed) {
109-
if (!channel.getAnimationName().equals("Walk")) {
110-
/** Play the "walk" animation! */
111-
channel.setAnim("Walk", 0.50f);
112-
channel.setLoopMode(LoopMode.Loop);
113+
ActionListener handler = new ActionListener() {
114+
@Override
115+
public void onAction(String name, boolean keyPressed, float tpf) {
116+
if (keyPressed && control.getCurrentAction() != advance) {
117+
control.setCurrentAction("advance");
113118
}
114119
}
115-
}
116-
};
120+
};
121+
inputManager.addListener(handler, "Walk");
122+
}
117123

118124
}

0 commit comments

Comments
 (0)