diff --git a/packages/timing/traits/animated.trait.js b/packages/timing/traits/animated.trait.js index e9d2bbd..de09f45 100644 --- a/packages/timing/traits/animated.trait.js +++ b/packages/timing/traits/animated.trait.js @@ -34,6 +34,7 @@ class AnimatedBase extends Trait { this.animations = {}; this.animationViews = undefined; this.animationsPromise = undefined; + this._currentAnimation = this.entity.currentAnimation; } destroy() { @@ -115,7 +116,7 @@ class AnimatedBase extends Trait { const animationView = this.animationViews[key]; animationView.position = this.offsetFor(key); // Ensure animation is made visible upfront. - const isCurrentAnimation = key === this.entity.currentAnimation; + const isCurrentAnimation = key === this._currentAnimation; if (isCurrentAnimation) { this.showAnimation(key); } @@ -157,7 +158,7 @@ class AnimatedBase extends Trait { return { visibleBoundingBoxes: () => { - const key = this.entity.currentAnimation; + const key = this._currentAnimation; const animation = this.animations[key]; if (!animation) { return [0, 0, 0, 0]; @@ -179,7 +180,8 @@ class AnimatedBase extends Trait { listeners() { return { - currentAnimationChanged: (oldKey) => { + currentAnimationChanged: (oldKey, currentAnimation) => { + this._currentAnimation = currentAnimation; // Reset old animation. const oldAnimation = this.animations[oldKey]; if (oldAnimation) { @@ -193,7 +195,7 @@ class AnimatedBase extends Trait { } // Swap the animation. this.hideAnimation(oldKey); - this.showAnimation(this.entity.currentAnimation); + this.showAnimation(this._currentAnimation); }, directionChanged: () => { @@ -231,14 +233,18 @@ class AnimatedBase extends Trait { return; } // Only tick current animation. - const currentAnimation = this.entity.currentAnimation; + const currentAnimation = this._currentAnimation; const animation = this.animations[currentAnimation]; if (!animation) { return; } - const jitter = Math.random() * this.jitterFor(currentAnimation); - const halfJitter = jitter / 2; - animation.tick(elapsed + (jitter - halfJitter)); + const jitterAmount = this.jitterFor(currentAnimation); + if (jitterAmount > 0) { + const jitter = Math.random() * jitterAmount; + const halfJitter = jitter / 2; + elapsed += jitter - halfJitter; + } + animation.tick(elapsed); } }