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