Search code examples
flutterflame

Flutter Flame - SpriteSheetAnimation: How to change the row but keep frame/index


I have a sprite Sheet animation where one row shows all frames of an animation. I have several rows showing the animation in different rotations. I now want to switch the rotation / change the row when the element "rotates".

Therefore I currently call

animation = spriteDeath.createAnimation(row: row, stepTime: 0.03);
animation!.currentIndex = index;

But this way, the animation just breaks thus no animation is being executed at all. I now tried to execute

animation!.reset();

But this also doesn't change anything. How can I fix this?

What seems weird is that if during debugging I insert and remove the reset function makes the animation work all of a sudden but I cannot get it working probably from a "fresh start"


Solution

  • Additionally to

    animation!.currentIndex = index;
    

    One also needs to update the clock

    animation!.clock = clock;
    

    As I have just discovered in the flutter code itself. This means the best way to just change the rotation (as far as I now right now) is to copy the index and clock attribute from the current animation, recreate the animation for a different row and assign the clock/index again afterwards.