Monogame Animated Sprite Hot! -
// Split texture into frames (assumes frames in a single row) int columns = texture.Width / frameWidth; for (int i = 0; i < columns; i++) { _frames.Add(new Rectangle(i * frameWidth, 0, frameWidth, frameHeight)); } }
public void Update(GameTime gameTime) { if (!IsPlaying) return; monogame animated sprite
protected override void Update(GameTime gameTime) { if (Keyboard.GetState().IsKeyDown(Keys.Escape)) Exit(); // Split texture into frames (assumes frames in
public bool IsPlaying { get; private set; } for (int i = 0
public AnimatedSprite(Texture2D texture, int frameWidth, int frameHeight, double framesPerSecond, bool looping = true) { _texture = texture; _frames = new List<Rectangle>(); _currentFrame = 0; _timePerFrame = 1.0 / framesPerSecond; _elapsedTime = 0; _looping = looping; IsPlaying = true;
public void Draw(SpriteBatch spriteBatch, Vector2 position, SpriteEffects effects = SpriteEffects.None) { spriteBatch.Draw(_texture, position, _frames[_currentFrame], Color.White, 0f, Vector2.Zero, 1f, effects, 0f); }