Monogame Sprite Sheet — Verified
public void Draw(SpriteBatch spriteBatch, string regionName, Vector2 position, Color color)
if (_currentClip == null) return; _elapsedTime += gameTime.ElapsedGameTime.TotalSeconds; if (_elapsedTime >= _currentClip.FrameDuration) _elapsedTime -= _currentClip.FrameDuration; _currentFrame++; if (_currentFrame >= _currentClip.RegionNames.Length) if (_currentClip.Loop) _currentFrame = 0; else _currentFrame = _currentClip.RegionNames.Length - 1;
var region = GetRegion(regionName); if (region != Rectangle.Empty) spriteBatch.Draw(_texture, position, region, color, rotation, origin, scale, SpriteEffects.None, 0f); monogame sprite sheet
public Rectangle GetRegion(string name)
var texture = content.Load<Texture2D>("player_sheet"); // 128x128 (4 frames of 32x32) _sheet = new SpriteSheet(texture, 32, 32); // Define animations _sheet.AddRegion("idle1", new Rectangle(0, 0, 32, 32)); _sheet.AddRegion("idle2", new Rectangle(32, 0, 32, 32)); _sheet.AddRegion("walk1", new Rectangle(64, 0, 32, 32)); _sheet.AddRegion("walk2", new Rectangle(96, 0, 32, 32)); _animator = new AnimatedSprite(_sheet); _animator.AddClip("idle", new[] "idle1", "idle2" , 4f); _animator.AddClip("walk", new[] "walk1", "walk2" , 8f); _animator.Play("idle"); public void Draw(SpriteBatch spriteBatch
_regions[name] = region;
string json = File.ReadAllText(jsonPath); var data = JsonSerializer.Deserialize<SpriteSheetData>(json); Texture2D texture = content.Load<Texture2D>(data.Texture); var sheet = new SpriteSheet(texture, data.FrameWidth, data.FrameHeight); foreach (var region in data.Regions) _elapsedTime += gameTime.ElapsedGameTime.TotalSeconds
public static SpriteSheet LoadSpriteSheet(string jsonPath, ContentManager content)