# Sprite animation with "overlapping" animations?

I'm trying to implement my own sprite animation system, and so far I've got it working quite nicely. I store each animation as a "take" which basically denotes the row of the animation in the sprite sheet, the starting frame and the number of frame. These takes are created during start-up and played by calling play(takeID) on the animation. The player is split into four sprites: head, torso, arms and legs, and different animations can be played on each of these separately from the other parts.

Now, this works fine for situations where I only need a single animation (walking, idling, running, swinging a sword etc.), but I need a more complex animation system for situations where the player performs multiple things at the same time, for example walking while swinging a sword.

I've tried to break down the requirements of the animation system by using the example above, and I've found these cases:

1. Player swings the sword
1.1. Play swinging animation
1.2. When done, play idle animation

2. Player starts walking
2.1. Play walking animation until player stops
2.2. Play idle animation when player stops

3. Player walks and swings the sword
3.1. Play walking animation
3.2. Replace the animation of the hands by a swinging animation
3.3. When swinging animation is done, revert back to the "walking hands" animation

4. Player walks, initiates a swing and stops walking during the swing
4.1. Play walking animation
4.2. Replace the animation of the hands by a swinging animation
4.3. (Player stops walking during the swing)
4.4. Stop walking animation and play idle animation for
all other body parts except the hands
4.5. When swinging animation is done, play idle animation for hands


I've tried fiddling around with "overriding" animations where the animation with the higher priority (in this case, the "swinging" animation) would be shown instead of the lower priority animation (the walking animation) while updating still both simultaneously to maintain the correct rhythm of the original animation. However, I have trouble getting it to work properly.

The "reversion to idle animation" is a rather challenging task: if I have just the single walking animation, I can stop it and then play the idle animation (or rather, the "idle frame"), but when the sword swinging is added, it becomes more difficult to know when to swap to the idle animation.

So, to conclude with my question: has anyone faced a similar problem? How should I tackle this issue?