From d528cda4bd1f18f27a8758e1c68a7e2bca164f7f Mon Sep 17 00:00:00 2001 From: Adil Hanney Date: Wed, 2 Aug 2023 03:35:31 +0100 Subject: [PATCH 1/4] fix: reset _completeCompleter in ticker --- packages/flame/lib/src/sprite_animation_ticker.dart | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/flame/lib/src/sprite_animation_ticker.dart b/packages/flame/lib/src/sprite_animation_ticker.dart index ab9099f5994..7b22ce35ec5 100644 --- a/packages/flame/lib/src/sprite_animation_ticker.dart +++ b/packages/flame/lib/src/sprite_animation_ticker.dart @@ -66,6 +66,7 @@ class SpriteAnimationTicker { currentIndex = 0; _done = false; _started = false; + _completeCompleter = null; } /// Sets this animation to be on the last frame. From ecae142827b261db51a40d4684b3a6ef5f5b24eb Mon Sep 17 00:00:00 2001 From: Adil Hanney Date: Wed, 2 Aug 2023 04:13:23 +0100 Subject: [PATCH 2/4] test: completed doesn't complete after the animation is reset --- .../flame/lib/src/sprite_animation_ticker.dart | 12 +++++++----- .../test/sprite_animation_ticker_test.dart | 17 +++++++++++++++++ 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/packages/flame/lib/src/sprite_animation_ticker.dart b/packages/flame/lib/src/sprite_animation_ticker.dart index 7b22ce35ec5..4bc3723f2c0 100644 --- a/packages/flame/lib/src/sprite_animation_ticker.dart +++ b/packages/flame/lib/src/sprite_animation_ticker.dart @@ -1,6 +1,7 @@ import 'dart:async'; import 'package:flame/components.dart'; +import 'package:flutter/material.dart'; /// A helper class to make the [spriteAnimation] tick. class SpriteAnimationTicker { @@ -30,7 +31,8 @@ class SpriteAnimationTicker { /// Registered method to be triggered when the animation complete. void Function()? onComplete; - Completer? _completeCompleter; + @visibleForTesting + Completer? completeCompleter; /// The current frame that should be displayed. SpriteAnimationFrame get currentFrame => spriteAnimation.frames[currentIndex]; @@ -54,9 +56,9 @@ class SpriteAnimationTicker { return Future.value(); } - _completeCompleter ??= Completer(); + completeCompleter ??= Completer(); - return _completeCompleter!.future; + return completeCompleter!.future; } /// Resets the animation, like it would just have been created. @@ -66,7 +68,7 @@ class SpriteAnimationTicker { currentIndex = 0; _done = false; _started = false; - _completeCompleter = null; + completeCompleter = null; } /// Sets this animation to be on the last frame. @@ -128,7 +130,7 @@ class SpriteAnimationTicker { } else { _done = true; onComplete?.call(); - _completeCompleter?.complete(); + completeCompleter?.complete(); return; } } else { diff --git a/packages/flame/test/sprite_animation_ticker_test.dart b/packages/flame/test/sprite_animation_ticker_test.dart index 567de61ed2c..ab1c1fe8a12 100644 --- a/packages/flame/test/sprite_animation_ticker_test.dart +++ b/packages/flame/test/sprite_animation_ticker_test.dart @@ -145,5 +145,22 @@ void main() { expectLater(animationTicker.completed, doesNotComplete); }, ); + + test("completed doesn't complete after the animation is reset", () async { + final sprite = MockSprite(); + final animationTicker = SpriteAnimation.spriteList( + [sprite], + stepTime: 1, + loop: false, + ).createTicker(); + + animationTicker.completed; + animationTicker.update(1); + expect(animationTicker.completeCompleter!.isCompleted, true); + + animationTicker.reset(); + animationTicker.completed; + expect(animationTicker.completeCompleter!.isCompleted, false); + }); }); } From 1855f02dc860db28c49f49b90053fdff4d7ae9f3 Mon Sep 17 00:00:00 2001 From: Adil Hanney Date: Mon, 7 Aug 2023 13:17:24 +0100 Subject: [PATCH 3/4] chore: fix import Co-authored-by: Lukas Klingsbo --- packages/flame/lib/src/sprite_animation_ticker.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/flame/lib/src/sprite_animation_ticker.dart b/packages/flame/lib/src/sprite_animation_ticker.dart index 4bc3723f2c0..088cb9d0684 100644 --- a/packages/flame/lib/src/sprite_animation_ticker.dart +++ b/packages/flame/lib/src/sprite_animation_ticker.dart @@ -1,7 +1,7 @@ import 'dart:async'; import 'package:flame/components.dart'; -import 'package:flutter/material.dart'; +import 'package:meta/meta.dart'; /// A helper class to make the [spriteAnimation] tick. class SpriteAnimationTicker { From 5f8f13e3bb7d32f49dc90e1819a5e545d993ce45 Mon Sep 17 00:00:00 2001 From: Adil Hanney Date: Mon, 7 Aug 2023 13:27:45 +0100 Subject: [PATCH 4/4] fix: only reset completeCompleter if it's already completed --- packages/flame/lib/src/sprite_animation_ticker.dart | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/packages/flame/lib/src/sprite_animation_ticker.dart b/packages/flame/lib/src/sprite_animation_ticker.dart index 088cb9d0684..a6f3c2754f3 100644 --- a/packages/flame/lib/src/sprite_animation_ticker.dart +++ b/packages/flame/lib/src/sprite_animation_ticker.dart @@ -68,7 +68,11 @@ class SpriteAnimationTicker { currentIndex = 0; _done = false; _started = false; - completeCompleter = null; + + // Reset completeCompleter if it's already completed + if (completeCompleter?.isCompleted ?? false) { + completeCompleter = null; + } } /// Sets this animation to be on the last frame.