Skip to content

Commit

Permalink
fix: Reset _completeCompleter in ticker (#2636)
Browse files Browse the repository at this point in the history
I was running this code:
```dart
void attack() async {
  print('Attack started');
  current = PlayerState.attack;

  final attackTicker = animationTicker!;
  await attackTicker.completed;
  print('Attack ticker completed');

  attackTicker.reset();
  current = PlayerState.idle;
}
```
and `attackTicker.completed` would get stuck as permanently completed
after the first completion.
  • Loading branch information
adil192 authored Aug 7, 2023
1 parent f3de665 commit a35d3a1
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 a35d3a1

Please sign in to comment.