Skip to content

Commit

Permalink
test: completed doesn't complete after the animation is reset
Browse files Browse the repository at this point in the history
  • Loading branch information
adil192 committed Aug 2, 2023
1 parent d528cda commit ecae142
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
12 changes: 7 additions & 5 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:flutter/material.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,7 +68,7 @@ class SpriteAnimationTicker {
currentIndex = 0;
_done = false;
_started = false;
_completeCompleter = null;
completeCompleter = null;
}

/// Sets this animation to be on the last frame.
Expand Down Expand Up @@ -128,7 +130,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 ecae142

Please sign in to comment.