Skip to main content
GameDev.net gamedev.net
🔒 Locked

[XNA] Sprite animation handling and structuration

Started by kontrolkl Mar 26, 2011 at 11:02 PM 1 replies 2.7k views
Original Post
kontrolkl
kontrolkl
Hello everyone,

I'm currently searching for a better way to manage my 2D game animation sprite sheet than what is currently shown in each XNA tutorial. The challenge here is that each sprite sheet correspond to a different "game entity" (Enemy, map tile, etc) and I each can behave differently and have different informations associated to them. So I need somekind of metadata to be attached to each of my sprite sheet. (For exemple, line one is running left, line 2 is running right, line 3 is jumping) and the size the sprites on the sprite sheets etc. So basicly at the moment I may have a sprite sheet in my content, and an associated xml file for each of these files. The problem about this technique is that I don't know at compile time that I'm missing an xml file and thus may crash my game, and the bigger the game gets, the harder it is to find such problem. Also it gets heavier every sprite sheet to "manually" manage the metadata of each file.

So as you can see I'm searching for a better solution, how bigger game handle their sprite sheet content. This will get important specially when I will have to create a level editor that should make use of the sprites that I will have in my content and I absolutly need to avoid using both as two separate list so It doens't crash at runtime because a there is a missing sprite sheet.

I'm also looking about advanced tutorials on effecient content management related to my problems if you have any.
And I'm also looking about level loading/management tutorials if that exist

Thank
applejacks
applejacks
I'm fairly new to XNA and C# but here's what I've been thinking:

A class that will be the animation:

public class Animation {
public string spriteSheet;
public int width;
public int height;
public int numFrames;
public int spriteX;
public int spriteY;

public Animation(string ss, int w, int h, int x, int y, int n)
{
spriteSheet = ss;
width = w;
height = h;
spriteX = x;
spriteY = y;
numFrames = n;
}
}


And a static class to hold all of the animations:

public static class AnimationManager {

public static Dictionary<string, Animation> animations = new Dictionary<string, Animation>();

public static AnimationManager() { }

public static void AddAnimation(string sequenceName, string spriteContent, int frameWidth, int frameHeight,
int frames) {
animations.Add(sequenceName, new Animation(spriteContent, frameWidth, frameHeight, frames));
}

public static Animation GetAnimationByName(string sequenceName)
{
return animations[sequenceName];
}
}


Then you can just save animations, and when you need it call it with GetAnimationByName. Such as GetAnimationByName("walk_right") or "attack". And when you add one, you can add what sprite sheet it's on ("Content/Path/To/PNG"), the width and height of the animation, number of frames, and what x and y location it is on the sprite sheet.

At least that's what came off of the top of my head. If I'm not right, oh well I tried
kontrolkl
kontrolkl
Hi applejacks,

Thank for taking time to help me. However I don't think it is what I'm exactly searching for.
I need a way to tie metadata to my content, so they behave as a single unit and doesn't allow for runtime exception because either one of them is missing.

Searching a bit further on the internet I found this :

http://create.msdn.com/en-US/education/catalog/sample/sprite_sheet

They use a processor to create sprite sheet from multiple files, but I could change that to hold metadata on a sprite sheet and name my animations in the xml file.
I'm still not 100% convinced on that method, if anyone has a better idea, or would like to comment on this one, I'd be very happy

Thank.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.