rive-app / rive-flutter

Flutter runtime for Rive
https://rive.app
MIT License
1.21k stars 191 forks source link

One shot animations cannot repeat #138

Closed tantzygames closed 3 years ago

tantzygames commented 3 years ago

Following this tutorial: https://help.rive.app/runtimes/playback#repeatedly-playing-a-one-shot-animation

Except the animation never repeats.

It seems when the animation completes _didLoop is set to true which prevents it from ever playing again:

  /// Whether the controller driving this animation should keep requesting
  /// frames be drawn.
  bool get keepGoing => animation.loop != Loop.oneShot || !_didLoop;

It's only able to play again after I set _didLoop to false.

luigi-rosso commented 3 years ago

The important part of that example is where the controller is re-activated when the floating action button is pressed:

onPressed: () => _isPlaying ? null : _controller.isActive = true,

Make sure you're setting _controller.isActive to true or playback will not resume.

If that's still not working, could you post your code? There's also a working example provided in this repo: https://github.com/rive-app/rive-flutter/blob/master/example/lib/play_one_shot_animation.dart

tantzygames commented 3 years ago

Oops sorry, my bad. I extended OneShotAnimation to include speed control using your example for SimpleAnimation which sets it to inactive if !instance.keepGoing:

@override
  void apply(rive.RuntimeArtboard artboard, double elapsedSeconds) {
    if (instance == null || !instance.keepGoing) {
      isActive = false;
      return;
    }
    instance
      ..animation.apply(instance.time, coreContext: artboard, mix: mix)
      ..advance(elapsedSeconds * speed);
  }

Removing the check for keepGoing fixes it