I'm writing an AnimatedSprite class that has an Animation object (which itself contains a sprite sheet).

The AnimatedSprite plays certain animations during key input. My problem is that how do I cycle from one animation back to the original ? Is there a good method to do this?

Here is my Animation class. The AnimatedSprite just calls PlayAnimation

public class Animation
    public SpriteSheet SpriteSheet { get; set; }
    public bool IsActive { get; set; }
    public float FramesPerSecond { get; set; }
    public bool IsLooping { get; set; }
    public int FrameCount { get; set; }

    private int CurrentFrame { get; set; }
    private float ElapsedTime { get; set; }
    private float TimePerFrame { get { return 1.0f / FramesPerSecond; } }

    public Texture2D Texture { get { return SpriteSheet.Texture; } }
    public Rectangle Rectangle { get { return SpriteSheet.SourceRectangle(CurrentFrame);} }

    public Animation(SpriteSheet sheet)
        SpriteSheet = sheet;
        FramesPerSecond = 20f;
        IsActive = true;
        IsLooping = false;
        FrameCount = SpriteSheet.NumberOfSprites;
        CurrentFrame = 0;
        ElapsedTime = 0;

    public Animation(SpriteSheet sheet, int fps) : this(sheet)
        FramesPerSecond = fps;

    public virtual void Update(GameTime gameTime)
        if (!IsActive)

        ElapsedTime += (float)gameTime.ElapsedGameTime.TotalSeconds;

        if (ElapsedTime > TimePerFrame)
            CurrentFrame += 1;//move frame ahead one
            if (CurrentFrame >= FrameCount)
                CurrentFrame = 0; //reset the animation;
                if (!IsLooping)
                    IsActive = false;

            ElapsedTime -= TimePerFrame;

  • \$\begingroup\$ What do you mean by "cycle from one animation back to the original"? What's "original" ? \$\endgroup\$ – XiaoChuan Yu Apr 25 '12 at 18:06
  • \$\begingroup\$ I thought about having one always be Idle and then playing only one extra then it goes back to idle? \$\endgroup\$ – Setheron Apr 25 '12 at 18:06
  • \$\begingroup\$ so consider: I'm running and then want to jump. I want to continue back to running once the animation is done without having a running button pressed \$\endgroup\$ – Setheron Apr 25 '12 at 18:07
  • \$\begingroup\$ I could use Event/Delegate? \$\endgroup\$ – Setheron Apr 25 '12 at 18:07
  • 1
    \$\begingroup\$ Have you looked at the Platformer Starter Kit? They have a pretty clean way of doing it msdn.microsoft.com/en-us/library/… \$\endgroup\$ – user159 Apr 25 '12 at 18:30

One method is to have a stack of animations in your animated sprite class. When the user says jump, put the jump animation on the top of the stack. When the end condition for the animation has been met, remove it from the stack. If you are always using the animation on the top of the stack, then removing the top one will automatically continue the next one down.

Delegates could be used to attach functions to the Animation object, which will return a boolean signaling whether the animation should stop or not. This way the Animation object can call this delegate method during it's update and it can return this value as the return value for the update. So your AnimatedSprite object just updates the top animation and, if the udpate function returns true, it removes that object from the stack.

For example, a function could be attached which returns true if the player is on the ground and false if their not. Attaching this to a jump animation would cause it to stop, and therefore be removed from the stack if the player hits the ground.

  • \$\begingroup\$ Oh animation stack.. what a good idea! \$\endgroup\$ – Setheron Apr 25 '12 at 18:22
  • \$\begingroup\$ I just did it using an IdleAnimation and a CurrentAnimation now... I wonder if I should revamp... \$\endgroup\$ – Setheron Apr 25 '12 at 18:22
  • \$\begingroup\$ I wonder how it work for the case of more than 1 idle anim. \$\endgroup\$ – Setheron Apr 25 '12 at 18:23
  • \$\begingroup\$ You just have one idle at the bottom and stick the others on top. Make their end condition a time and every so often, stick a new one on there. Alternately, concatenate those idle animations into one animation object, which itself switches the idle animation being played. It could do this randomly. \$\endgroup\$ – OriginalDaemon Apr 25 '12 at 18:27
  • 1
    \$\begingroup\$ That's really another question as there is a wealth of possible answers. I, for example, don't allow movement in the animation if possible and just move things in code where appropriate, but you could store a series of position modifiers with each animation and use them to augment you sprites position. \$\endgroup\$ – OriginalDaemon Apr 26 '12 at 8:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.