playWith method

Future<Event> playWith(
  1. Juggler juggler, {
  2. int? gotoFrame,
  3. int? stopFrame,
})

Play the animation with the juggler.

If the optional gotoFrame argument is specified, the animation will start at the given frame number, otherwise the animation will start at the current frame.

If the optional stopFrame argument is specified, the animation will stop at the given frame number, otherwise the animation will continue to run depending on the loop configuration.

The returned future is completed on the first occurrence of:

  • the animation does not loop and the last frame was reached.
  • the optionally specified argument stopFrame was reached.
  • the stop method was called.

Implementation

Future<Event> playWith(Juggler juggler, {int? gotoFrame, int? stopFrame}) {
  _play = true;
  _frameTime = null;
  _currentFrame = gotoFrame ?? currentFrame;

  final completed = onComplete.first;
  var currentTime = juggler.elapsedTime;
  final subscription = juggler.onElapsedTimeChange.listen((elapsedTime) {
    advanceTime(elapsedTime - currentTime);
    currentTime = elapsedTime;
    if (currentFrame == stopFrame) stop();
  });

  completed.then((_) => subscription.cancel());
  return completed;
}