Play all animations in sequence

using TSprite with player.SwitchToAnimation(-1) I played all the animations of a spritesheet in sequence. What is the equivalent with the new system? I think PlayAnimation() explicitly requires to indicate a specific animation, but what if I wanted to view them all in sequence?

Hi Valterb,

I had the same question; look at my posts and code about using "“Working with Scenes and Sprites” that were answered by Michalis.
You can put a sequence in a xml file (I use starling format) and then you can play it.

For instance I have:

PlayAnimation(‘johnwalkleft-’, true);

It will play all numbers in sequence.

I think (but am not sure) that you can also play a sequence of sprite sheets by adding the next filename (Textureatlas) at the end of the subtexture list followed by the next SubTexture names.
(update: not, that does not work)

You can also just give the name of a SubTexture (“johnstandfront”) after the PlayAnimation command and it will automatically find the correct image.
The new sprite system is very nice but I am still learning too and now I am stuck with making them collide. Michalis promised us a demo. I hope it will show everything (collision routines, increasing sprite speed and maybe lightning, shading?) hint hint.

Hi Carring,
thank you for your answer.
Yes, I had read all the previous posts but my case is slightly different as I have different animations in a spritesheet (eg 14 animations for the name “up”, from up_001 to up014, another 14 for down and so on. So I can use PlayAnimation (‘down’, true) … PlayAnimation (‘left’, true) etc.
But I am creating a simple utility that shows the animations available in a spritesheet and I would like the user to be able to choose from a combobox the single animations but also all the animations in sequence.

That is great. But that utility is already avaliable with “View3dScene” tool.

That said I am sure you can call different sequences in a xml file by just calling it’s subtexture name followed by -

SubTexture name=“johnwalkup-0”
SubTexture name=“johnwalkup-1”
SubTexture name=“johnwalkup-2”
SubTexture name=“johnwalkdown-0”
SubTexture name=“johnwalkdown-1”
SubTexture name=“johnwalkdown-2”

Playanimation( ‘johnwalkup-’, true); // play sequence of 0, 1, 2 walkup

Playanimation(‘johnwalkdown-’, true); // play sequence of 0, 1, 2 walkdown

I read your posts in the past and tried your code on making a character walk in 8 directions. We share the same interest in sprites.

Playanimation( ‘johnwalkup-’, true); // play sequence of 0, 1, 2 walkup
Playanimation(‘johnwalkdown-’, true); // play sequence of 0, 1, 2 walkdown

I’m afraid it wouldn’t work, according to the API the last call to PlayAnimation immediately cancels the previous call without waiting for the animation to finish.
There should be a way but I can’t find it.

[edit] For now the only system I have found is to create a second .starling-xml file by renaming all the animations with a single name.
But perhaps there could be a way to read all the animations from the .starling-xml file and create a “container animation” on the fly that contains them all.
Unless there is a simple parameter that does all of this. Let’s wait for Michalis (or whoever has the solution).

I see. Indeed, I would like that option too.
Then we will have to wait for Michalis I guess. He must be busy as he has not answered my questions in the forum for several days.

Sorry for the delay!

In the new system, there’s no “animation that contains all other animations” as it didn’t seem very useful.

However you can implement a feature “play all available animations in a sequence” still, like this:

  1. Get all possible animation names by Scene.AnimationsList. It is just a TStringList with all animation names.

  2. Use the TPlayAnimationParameters.StopNotification mechanism to play one animation, and when it ends play the next one, and when it ends play the next again etc.

See my post Sprite control with the new system - #2 by michalis where I mention this.

See examples/animations/play_animation/code/gamestatemain.pas for an example usage of PlayAnimation with TPlayAnimationParameters.

In your case, it would look like this (warning: untested quick draft code):

  TSomeClass = class // place this in any class you have handy
    AnimList: TStringList;
    NextAnim: Integer;
    procedure RunNextAnimation(const Scene: TCastleSceneCore; const Animation: TTimeSensorNode);
    procedure RunAllAnimations(const Scene: TCastleSceneCore);
procedure TSomeClass.RunAllAnimations(const Scene: TCastleSceneCore);
  AnimList := Scene.AnimationsList;
  NextAnim := 0;
  RunNextAnimation(Scene, nil); // our RunNextAnimation implementation ignores 2nd param, ok to pass nil

procedure TSomeClass.RunNextAnimation(const Scene: TCastleSceneCore; const Animation: TTimeSensorNode);
  NextAnimationName: String;
  Params: TPlayAnimationParameters;
  NextAnimationName := AnimList[NextAnim];
  NextAnim := (NextAnim + 1) mod AnimList.Count;

  // play NextAnimationName, such that RunNextAnimation will be called again when it ends
  Params := TPlayAnimationParameters.Create;
    Params.Name := NextAnimationName;
    Params.StopNotification := @RunNextAnimation;
  finally FreeAndNil(Params) end;

You may want to add here some “cancel” condition to TSomeClass.RunNextAnimation, otherwise it will just go on forever, until TCastleScene instance is finished.

1 Like