# Does my class reinvent AnimationAction's methods?

**URL:** <https://discourse.threejs.org/t/does-my-class-reinvent-animationactions-methods/6380>\
**Category:** Questions\
**Tags:** animation\
**Created:** [March 2, 2019, 7:05pm UTC](https://discourse.threejs.org/t/does-my-class-reinvent-animationactions-methods/6380 "2019-03-02T19:05:34Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vortico](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/vortico/32/4382_2.png) [@Vortico](https://discourse.threejs.org/u/Vortico)\
**Post date:** [March 2, 2019, 7:05pm UTC](https://discourse.threejs.org/t/does-my-class-reinvent-animationactions-methods/6380/1 "2019-03-02T19:05:34Z")

</div>

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?

```auto
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:

```auto
// 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);

```

---

<div class="post-metadata">

**Author:** ![Mugen87](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/mugen87/32/5645_2.png) [@Mugen87](https://discourse.threejs.org/u/Mugen87)\
**Post date:** [March 2, 2019, 7:42pm UTC](https://discourse.threejs.org/t/does-my-class-reinvent-animationactions-methods/6380/2 "2019-03-02T19:42:43Z")

</div>

Have you tried using methods like [AnimationAction.fadeIn](https://threejs.org/docs/index.html#api/en/animation/AnimationAction.fadeIn)() or [AnimationAction.fadeOut](https://threejs.org/docs/index.html#api/en/animation/AnimationAction.fadeOut)? There are also the so called [cossFade\*](https://threejs.org/docs/index.html#api/en/animation/AnimationAction.crossFadeTo) methods which provide a nice API for smoothly switch from one action to an other. The following example uses these methods.

[https://threejs.org/examples/webgl\_animation\_skinning\_blending.html](https://threejs.org/examples/webgl_animation_skinning_blending.html)

---

<div class="post-metadata">

**Author:** ![Vortico](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/vortico/32/4382_2.png) [@Vortico](https://discourse.threejs.org/u/Vortico)\
**Post date:** [March 2, 2019, 7:56pm UTC](https://discourse.threejs.org/t/does-my-class-reinvent-animationactions-methods/6380/3 "2019-03-02T19:56:29Z")

</div>

Is appears that `fadeIn` and `fadeOut` assume that `animationAction.weight` is 0 or 1, respectively. They ignore the current weight, which might be somewhere between 0 and 1.  
The crossFade methods call the fade methods, so they inherit the same problem.  
Am I thinking about this incorrectly? Is this the normal way of creating animations?

---

<div class="post-metadata">

**Author:** ![Mugen87](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/mugen87/32/5645_2.png) [@Mugen87](https://discourse.threejs.org/u/Mugen87)\
**Post date:** [March 2, 2019, 8:22pm UTC](https://discourse.threejs.org/t/does-my-class-reinvent-animationactions-methods/6380/4 "2019-03-02T20:22:54Z")

</div>

Well, there are many scenarios where the current active animation always has a `weight` of `1`. The mentioned API is intended for such use cases.

If that is not the case in your app, you can still implement a custom transition like shown above. I’m also doing something similar in my current project^^

---

<div class="post-metadata">

**Author:** ![Vortico](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/vortico/32/4382_2.png) [@Vortico](https://discourse.threejs.org/u/Vortico)\
**Post date:** [March 2, 2019, 8:28pm UTC](https://discourse.threejs.org/t/does-my-class-reinvent-animationactions-methods/6380/5 "2019-03-02T20:28:15Z")

</div>

Ah, good to hear I’m not alone.  
Perhaps `AnimationAction` is for animations that have similar start and end poses, so that you can queue up a number of actions and wait until they’re finished to move on to the next cutscene or whatever. But if that’s the case, I still don’t see things like `onFinish` and an actual queue, so maybe I’m wrong.
