Batch your draws: Use one SpriteSheet instance and draw all frames in a single SpriteBatch.Begin/End block.
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");
return _regions.TryGetValue(name, out var rect) ? rect : Rectangle.Empty; monogame sprite sheet
sprite_sheet.json :
if (_clips.TryGetValue(clipName, out var clip)) if (_currentClip != clip) _currentClip = clip; _currentFrame = 0; _elapsedTime = 0; Batch your draws: Use one SpriteSheet instance and
Here's how to do it right. Create a dedicated class to manage your sheet:
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) Create a dedicated class to manage your sheet:
"texture": "characters/hero_sheet", "frameWidth": 32, "frameHeight": 32, "regions": "idle": "x": 0, "y": 0, "width": 32, "height": 32 , "walk1": "x": 32, "y": 0, "width": 32, "height": 32 , "walk2": "x": 64, "y": 0, "width": 32, "height": 32 , "jump": "x": 96, "y": 0, "width": 32, "height": 32