Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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)
            return;

        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;
        }
    }

}
share|improve this question
    
What do you mean by "cycle from one animation back to the original"? What's "original" ? –  XiaoChuan Yu Apr 25 '12 at 18:06
    
I thought about having one always be Idle and then playing only one extra then it goes back to idle? –  Setheron Apr 25 '12 at 18:06
    
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 –  Setheron Apr 25 '12 at 18:07
    
I could use Event/Delegate? –  Setheron Apr 25 '12 at 18:07
1  
Have you looked at the Platformer Starter Kit? They have a pretty clean way of doing it msdn.microsoft.com/en-us/library/… –  Joe Apr 25 '12 at 18:30

1 Answer 1

up vote 2 down vote accepted

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.

share|improve this answer
    
Oh animation stack.. what a good idea! –  Setheron Apr 25 '12 at 18:22
    
I just did it using an IdleAnimation and a CurrentAnimation now... I wonder if I should revamp... –  Setheron Apr 25 '12 at 18:22
    
I wonder how it work for the case of more than 1 idle anim. –  Setheron Apr 25 '12 at 18:23
    
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. –  OriginalDaemon Apr 25 '12 at 18:27
1  
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. –  OriginalDaemon Apr 26 '12 at 8:58

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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