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;
if (_currentFrame >= _frames.Count) { if (_looping) _currentFrame = 0; else { _currentFrame = _frames.Count - 1; IsPlaying = false; } } } } monogame animated sprite
// Example controls var kstate = Keyboard.GetState(); if (kstate.IsKeyDown(Keys.Space) && !_animatedSprite.IsPlaying) _animatedSprite.Play(); if (kstate.IsKeyDown(Keys.P)) _animatedSprite.Pause(); if (kstate.IsKeyDown(Keys.R)) _animatedSprite.Restart(); public AnimatedSprite(Texture2D texture
// 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)); } } _frames = new List<
public bool IsPlaying { get; private set; }
public Game1() { _graphics = new GraphicsDeviceManager(this); Content.RootDirectory = "Content"; IsMouseVisible = true; }
_animatedSprite.Update(gameTime); base.Update(gameTime); }