Skip to content

Commit

Permalink
Merge branch 'main' into bugfix-2633
Browse files Browse the repository at this point in the history
  • Loading branch information
spydon authored Aug 7, 2023
2 parents 61ed09c + a35d3a1 commit 08a1512
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
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);
});
});
}

0 comments on commit 08a1512

Please sign in to comment.