public AnimatedSprite(int totalFrames, string textureFileName, float fps) { this.totalFrames = totalFrames; this.textureFileName = textureFileName; texture = GameClass.LoadTextureData(textureFileName); this.frameHeight = texture.Height; currentFrame = 0; frameWidth = (int)(texture.Width / totalFrames); this.fps = fps; this.animationType = SpriteAnimationType.Repeating; animationDirection = SpriteAnimationDirection.Forward; }
public void Update(GameTime gameTime) { frameTimer += (float)gameTime.ElapsedGameTime.TotalSeconds; //update frameTimer with current elapsed time. if (frameTimer > (float)(1 / fps)) //if its time to animate to the next frame. { frameTimer = 0.0f; switch (animationType) { case SpriteAnimationType.Repeating: //sprite animates to the end of it's frames, and then starts over. animationDirection = SpriteAnimationDirection.Forward; if (!(currentFrame > totalFrames - 2)) { currentFrame += 1; } else { currentFrame = 0; } break; case SpriteAnimationType.AnimateOnceMaintainFirst: //sprite animates to the end of it's frames once, goes back to it's first frame, and then stops. { animationDirection = SpriteAnimationDirection.Forward; if (!(currentFrame > totalFrames - 2)) { currentFrame += 1; singleAnimationFinished = false; } else { currentFrame = 0; singleAnimationFinished = true; } break; } case SpriteAnimationType.AnimateOnceMaintainLast: //sprite animates to the end of it's frames once, and stops at the last frame. { animationDirection = 0; if ((currentFrame < totalFrames - 1)) { currentFrame += 1; singleAnimationFinished = false; } else { currentFrame = totalFrames - 1; singleAnimationFinished = true; } break; } case SpriteAnimationType.RepeatingReverse: //sprite animates backwards indefinitely. { animationDirection = SpriteAnimationDirection.Backward; if ((currentFrame > 0)) { currentFrame -= 1; } else { currentFrame = totalFrames - 1; } break; } case SpriteAnimationType.AlternatingReverse: //sprite animates forward until the end of it's frames, then animates back to the start, and repeats this cycle. { if (animationDirection == SpriteAnimationDirection.Forward) { if (!(currentFrame > totalFrames - 2)) { currentFrame += 1; } else { currentFrame = totalFrames - 1; animationDirection = SpriteAnimationDirection.Backward; } } else if (animationDirection == SpriteAnimationDirection.Backward) { if ((currentFrame > 0)) { currentFrame -= 1; } else { currentFrame = 0; animationDirection = SpriteAnimationDirection.Forward; } } break; } } } }