I have several animations on my character and want to always transition smoothly from running to walking to idling to punching, etc. at any time without discontinuous jumps (without waiting on a previous transition to finish). I couldn’t find a way to achieve this with AnimationAction
's many methods and properties. Can “vanilla” AnimationAction
s achieve the following, i.e. can my ActionHandler
class be replaced with a few calls to AnimationAction
methods?
class ActionHandler {
constructor(actions) {
this.actions = actions;
this.duration = Infinity;
for (var a of this.actions) {
a.play();
a.enabled = false;
a.weight = 0;
}
}
play(currentAction, duration) {
this.currentAction = currentAction;
this.duration = duration;
}
update(deltaTime) {
// Fade in currentAction
var deltaWeight = deltaTime / this.duration;
if (this.currentAction) {
this.currentAction.enabled = true;
this.currentAction.weight = Math.min(this.currentAction.weight + deltaWeight, 1);
}
// Fade out other actions
for (var a of this.actions) {
if (a !== this.currentAction && a.enabled) {
a.weight -= deltaWeight;
if (a.weight <= 0) {
a.weight = 0;
a.enabled = false;
a.time = 0;
}
}
}
}
}
Usage:
// constructor()
this.actionHandler = new ActionHandler([this.idleAction, this.runAction])
// step()
if (running) {
this.actionHandler.play(this.runAction, 1.0);
}
else {
this.actionHandler.play(this.idleAction, 1.0);
}
// render()
this.actionHandler.update(deltaTime);