diff --git a/packages/flame/lib/src/sprite_animation_ticker.dart b/packages/flame/lib/src/sprite_animation_ticker.dart index a6f3c2754f3..289cb7d5214 100644 --- a/packages/flame/lib/src/sprite_animation_ticker.dart +++ b/packages/flame/lib/src/sprite_animation_ticker.dart @@ -47,6 +47,16 @@ class SpriteAnimationTicker { /// still image). bool get isSingleFrame => spriteAnimation.frames.length == 1; + bool _paused = false; + + /// Returns current value of paused flag. + bool get isPaused => _paused; + + /// Sets the given value of paused flag. + set paused(bool value) { + _paused = value; + } + /// A future that will complete when the animation completes. /// /// An animation is considered to be completed if it reaches its [isLastFrame] @@ -68,6 +78,7 @@ class SpriteAnimationTicker { currentIndex = 0; _done = false; _started = false; + _paused = false; // Reset completeCompleter if it's already completed if (completeCompleter?.isCompleted ?? false) { @@ -111,9 +122,12 @@ class SpriteAnimationTicker { /// calls to [onStart]. bool _started = false; - /// Updates this animation, ticking the lifeTime by an amount [dt] - /// (in seconds). + /// Updates this animation, if not paused, ticking the lifeTime by an amount + /// [dt] (in seconds). void update(double dt) { + if (_paused) { + return; + } clock += dt; elapsed += dt; if (_done) { diff --git a/packages/flame/test/sprite_animation_ticker_test.dart b/packages/flame/test/sprite_animation_ticker_test.dart index ab1c1fe8a12..e12c798547a 100644 --- a/packages/flame/test/sprite_animation_ticker_test.dart +++ b/packages/flame/test/sprite_animation_ticker_test.dart @@ -162,5 +162,26 @@ void main() { animationTicker.completed; expect(animationTicker.completeCompleter!.isCompleted, false); }); + + test('paused pauses ticket', () async { + final sprite = MockSprite(); + final animationTicker = SpriteAnimation.spriteList( + [sprite, sprite], + stepTime: 1, + loop: false, + ).createTicker(); + + expect(animationTicker.isPaused, false); + expect(animationTicker.currentIndex, 0); + animationTicker.update(1); + expect(animationTicker.currentIndex, 1); + animationTicker.paused = true; + expect(animationTicker.isPaused, true); + animationTicker.update(1); + expect(animationTicker.currentIndex, 1); + animationTicker.reset(); + expect(animationTicker.currentIndex, 0); + expect(animationTicker.isPaused, false); + }); }); }