I am trying to use the TextureAtlas loader.
The “AtlasData.json” looks like this:
{"frames": [
{
"filename": "Char/walk/down/1.png",
"frame": {"x":142,"y":0,"w":15,"h":14},
"rotated": true,
"trimmed": true,
"spriteSourceSize": {"x":1,"y":1,"w":15,"h":14},
"sourceSize": {"w":16,"h":16},
"pivot": {"x":0.5,"y":0.5}
},
{
"filename": "Char/walk/down/2.png",
"frame": {"x":84,"y":0,"w":15,"h":15},
"rotated": false,
"trimmed": true,
"spriteSourceSize": {"x":1,"y":1,"w":15,"h":15},
"sourceSize": {"w":17,"h":16},
"pivot": {"x":0.5,"y":0.5}
},
etc.
As you can see, there are rotated frames, because this is how it was packed.
I use the SpriteSheetAnimation class to do the animation.
var atlas = game.Content.Load<TextureAtlas>("AtlasData");
TextureRegion2D[] walkDown = {
atlas.GetRegion("Char/walk/down/1"),
atlas.GetRegion("Char/walk/down/2"),
atlas.GetRegion("Char/walk/down/3"),
atlas.GetRegion("Char/walk/down/4")
};
spriteSheetAnimation = new SpriteSheetAnimation("walkDown", walkDown);
sprite = new Sprite(spriteSheetAnimation.CurrentFrame) { Position = new Vector2(200, 200) };
I also contemplated building the animation with the SpriteSheetAnimationFactory class, and using AnimatedSprite, but it requires me to use the SpriteSheetAnimationData class, and it’s not clear how I can integrate that with the atlas, as it asks for frame indices, but I would like to refer to my frames with strings (it is basically the only reason for me to use a texture packer). Can I do that?
Then I update the SpriteSheetAnimation and Draw it:
// in Update method
spriteSheetAnimation.Update((float)gameTime.ElapsedGameTime.TotalSeconds);
// in Draw method
sprite.TextureRegion = spriteSheetAnimation.CurrentFrame;
spriteBatch.Draw(sprite);
I get this animation:
As you can see, the rotated sprite is problematic. Since the image is packed tightly there are also a few pixels from the next sprite (and the bottom is cut out), because the width and height parameters are getting mixed up.