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

feat: Add pause and isPaused to SpriteAnimationTicker #2660

Merged
merged 3 commits into from
Aug 20, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
18 changes: 16 additions & 2 deletions packages/flame/lib/src/sprite_animation_ticker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,16 @@ class SpriteAnimationTicker {
/// still image).
bool get isSingleFrame => spriteAnimation.frames.length == 1;

bool _paused = false;

/// Returns current value of paused flag.
bool get isPaused => _paused;

/// Sets the given value of paused flag.
set paused(bool value) {
_paused = value;
}

/// A future that will complete when the animation completes.
///
/// An animation is considered to be completed if it reaches its [isLastFrame]
Expand All @@ -68,6 +78,7 @@ class SpriteAnimationTicker {
currentIndex = 0;
_done = false;
_started = false;
_paused = false;

// Reset completeCompleter if it's already completed
if (completeCompleter?.isCompleted ?? false) {
Expand Down Expand Up @@ -111,9 +122,12 @@ class SpriteAnimationTicker {
/// calls to [onStart].
bool _started = false;

/// Updates this animation, ticking the lifeTime by an amount [dt]
/// (in seconds).
/// Updates this animation, if not paused, ticking the lifeTime by an amount
/// [dt] (in seconds).
void update(double dt) {
if (_paused) {
return;
}
clock += dt;
elapsed += dt;
if (_done) {
Expand Down
21 changes: 21 additions & 0 deletions packages/flame/test/sprite_animation_ticker_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -162,5 +162,26 @@ void main() {
animationTicker.completed;
expect(animationTicker.completeCompleter!.isCompleted, false);
});

test('paused pauses ticket', () async {
final sprite = MockSprite();
final animationTicker = SpriteAnimation.spriteList(
[sprite, sprite],
stepTime: 1,
loop: false,
).createTicker();

expect(animationTicker.isPaused, false);
expect(animationTicker.currentIndex, 0);
animationTicker.update(1);
expect(animationTicker.currentIndex, 1);
animationTicker.paused = true;
munsterlander marked this conversation as resolved.
Show resolved Hide resolved
expect(animationTicker.isPaused, true);
animationTicker.update(1);
expect(animationTicker.currentIndex, 1);
animationTicker.reset();
expect(animationTicker.currentIndex, 0);
expect(animationTicker.isPaused, false);
});
});
}
Loading