# Playing part of an animation xml file

**URL:** <https://forum.castle-engine.io/t/playing-part-of-an-animation-xml-file/432>\
**Category:** Uncategorized\
**Created:** [October 8, 2021, 5:40pm UTC](https://forum.castle-engine.io/t/playing-part-of-an-animation-xml-file/432 "2021-10-08T17:40:05Z")\
**Posts on this page:** 1\
**Showing post:** 23

<div class="post-metadata">

**Author:** ![Carring](https://forum.castle-engine.io/user_avatar/forum.castle-engine.io/carring/32/1950_2.png) [@Carring](https://forum.castle-engine.io/u/Carring)\
**Post date:** [November 11, 2021, 7:20am UTC](https://forum.castle-engine.io/t/playing-part-of-an-animation-xml-file/432/23 "2021-11-11T07:20:41Z")

</div>

> [@michalis](#):
>
> you have to compare 2 distances: one is going forward through the animation list (“index of last animation - index of first animation”, and if it’s negative add the “count of animations”), the other distance is going backward (which should be equal “count of animations - distance of going forward”). Choose the option with the shortest distance.

Yes! This is what I was looking for but I could not get it working because I could not figure out how to calculate the backward playing distance.  
Thanks!

Now I have this and it works perfectly 🙂

```auto
procedure TStateMain.SetAnimationRange;
var A, B, AnimCountForward, AnimCountBackward: Integer;
List: array[0..20] of string;
begin
  AnimList := Scene1.AnimationsList;
  For A := 0 to AnimList.Count-1 do
  begin
    List[A] := AnimList[A];
    if List[A] = FirstAnimationName then FirstAnim := A; // find current animationame and remember its index
  end;

  For B := 0 to AnimList.Count-1 do
  begin
    List[B] := AnimList[B];
    if List[B] = LastAnimationName then LastAnim := B; // find destination animation and remember its index
  end;

  AnimCountForward := LastAnim - FirstAnim; // count the forward number of animations between them
  If AnimCountForward < 0 then AnimCountForward := (AnimCountForward + AnimList.Count);

  AnimCountBackward := AnimList.Count - AnimCountForward; // count the backward number of animations between them
  if AnimCountBackward < AnimCountForward then PlayForward := False else PlayForward := True;

```

> [@michalis](#):
>
> Looks like you want to hack `RunAllAnimations` into doing something contrary to its name (which would be “run all animations in the scene”).

Yes, I should have named it like RunAnimationRange.

> [@michalis](#):
>
> I would rather advise to go back to the working `RunAnimationList` that you had. Build new concepts on top of the existing concepts that you have working reliably.

Ok, maybe I will try this for the above, but since it works at last now I dont’ dare to change again 🙂  
I think I will use RunAnimationList for playing certain animations in the sprite sheet and ‘RunAnimationRange’ only for turning the sprite.

---

_[View the full topic](https://forum.castle-engine.io/t/playing-part-of-an-animation-xml-file/432)._
