protected override void LoadContent() { _spriteBatch = new SpriteBatch(GraphicsDevice); // Load a spritesheet: each frame is 64x64, 8 frames total (8 columns, 1 row) _spritesheet = Content.Load<Texture2D>("player_run"); _animatedSprite = new AnimatedSprite(_spritesheet, 64, 64, 12.0, true); }
// 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();
protected override void Update(GameTime gameTime) { if (Keyboard.GetState().IsKeyDown(Keys.Escape)) Exit();
_elapsedTime += gameTime.ElapsedGameTime.TotalSeconds;
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 (_elapsedTime >= _timePerFrame) { _currentFrame++; _elapsedTime -= _timePerFrame;
_animatedSprite.Update(gameTime); base.Update(gameTime); }
protected override void LoadContent() { _spriteBatch = new SpriteBatch(GraphicsDevice); // Load a spritesheet: each frame is 64x64, 8 frames total (8 columns, 1 row) _spritesheet = Content.Load<Texture2D>("player_run"); _animatedSprite = new AnimatedSprite(_spritesheet, 64, 64, 12.0, true); }
// 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();
protected override void Update(GameTime gameTime) { if (Keyboard.GetState().IsKeyDown(Keys.Escape)) Exit();
_elapsedTime += gameTime.ElapsedGameTime.TotalSeconds;
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 (_elapsedTime >= _timePerFrame) { _currentFrame++; _elapsedTime -= _timePerFrame;
_animatedSprite.Update(gameTime); base.Update(gameTime); }