Unable to set the animation to specific frame / duration
Description
With the current version of Rive (^0.8.4) i'm not able to able to control an animation anymore. In the a previous version, the following code would have set the animation to specific frame/point in time.
riveController = SimpleAnimation('Gesamt');
riveController.apply(riveController.instance!.animation.context, time);
Source .riv/.rev file
Expected behavior
The animation should be set to the specific frame/point in time.
Device & Versions (please complete the following information)
- Device: [iOS Simulator]
- OS: [iOS 15.4]
Flutter 3.0.0 • channel stable • https://github.com/flutter/flutter.git
Framework • revision ee4e09cce0 (3 weeks ago) • 2022-05-09 16:45:18 -0700
Engine • revision d1b9a6938a
Tools • Dart 2.17.0 • DevTools 2.12.2
Here was my repo:
- Modify our simple_animation.dart (in our 'examples' folder from our SDK). I copied your file ('header.riv') into our examples/assets folder.
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Simple Animation'),
),
body: const Center(
// child: RiveAnimation.network(
// 'https://cdn.rive.app/animations/vehicles.riv',
child: RiveAnimation.asset(
'assets/header.riv',
animations: ['Gesamt'],
fit: BoxFit.cover,
),
),
);
}
Then I modified lib/src/controllers/simple_controller.dart to set the time, instead of calling advance().
_instance!
..animation.apply(_instance!.time, coreContext: artboard, mix: mix)
..time = 1.859;
// ..advance(elapsedSeconds);
Then I ran the example app included in our SDK, and chose 'Simple Animation'. This is what it drew.
Which appears to be correct for that time value.
Hi all, there is a similar question here: https://github.com/rive-app/rive-flutter/issues/165
And similar for testing, here: https://github.com/rive-app/rive-flutter/issues/162
There may have been an issue with apply but it should be working fine now. Note however, that apply does not set the animation to a particular frame/duration. Instead it advances the animation by that duration. For example if you call:
// initial time is 0
controller.apply(controller.instance!.animation.context, 1); // advances 1 second, now at 1
controller.apply(controller.instance!.animation.context, 2); // advances 2 seconds, now at 3
Each call will add time to the animation.
If you want to be sure you're setting the time to a particular duration you should first call reset, which will set the time back to 0.
controller.reset(); // time now at 0
controller.apply(controller.instance!.animation.context, 1); // advances 1 second, now at 1
You could alternatively create your own AnimationController by extending RiveAnimationController with custom logic that advances the animation as needed - as suggested by @mikerreed
Closing this for now, but feel free to reopen with suggestions/issues