Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Reset _completeCompleter in ticker #2636

Merged
merged 5 commits into from
Aug 7, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions packages/flame/lib/src/sprite_animation_ticker.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dart:async';

import 'package:flame/components.dart';
import 'package:meta/meta.dart';

/// A helper class to make the [spriteAnimation] tick.
class SpriteAnimationTicker {
Expand Down Expand Up @@ -30,7 +31,8 @@ class SpriteAnimationTicker {
/// Registered method to be triggered when the animation complete.
void Function()? onComplete;

Completer<void>? _completeCompleter;
@visibleForTesting
Completer<void>? completeCompleter;

/// The current frame that should be displayed.
SpriteAnimationFrame get currentFrame => spriteAnimation.frames[currentIndex];
Expand All @@ -54,9 +56,9 @@ class SpriteAnimationTicker {
return Future.value();
}

_completeCompleter ??= Completer<void>();
completeCompleter ??= Completer<void>();

return _completeCompleter!.future;
return completeCompleter!.future;
}

/// Resets the animation, like it would just have been created.
Expand All @@ -66,6 +68,11 @@ class SpriteAnimationTicker {
currentIndex = 0;
_done = false;
_started = false;

// Reset completeCompleter if it's already completed
if (completeCompleter?.isCompleted ?? false) {
completeCompleter = null;
}
}

/// Sets this animation to be on the last frame.
Expand Down Expand Up @@ -127,7 +134,7 @@ class SpriteAnimationTicker {
} else {
_done = true;
onComplete?.call();
_completeCompleter?.complete();
completeCompleter?.complete();
return;
}
} else {
Expand Down
17 changes: 17 additions & 0 deletions packages/flame/test/sprite_animation_ticker_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
}