Animating Sprites
Found this
http://www.c-unit.com/tutorials/mdirectx/?t=46
but that's in one texture, how can I do it if I have a lot of bitmaps?
Found this
http://www.c-unit.com/tutorials/mdirectx/?t=46
but that's in one texture, how can I do it if I have a lot of bitmaps?
You could load them into an array of textures, something like:
Texture2D
[] myFrames = newTexture2D[15];Then inside your update method, increment a "current frame" int based on the elapsed time, and use it to fetch the correct frame from the array.
I really can't say whether that's the best way of doing it, or if you'd be better off stitching them together and just reading from disk once, but it should work. I'd of course recommend wrapping it up inside a class, so your main class isn't littered with currentFrame counters for each sprite you have :D
Danny Tuppeny wrote:
You could load them into an array of textures, something like:
Texture2D[] myFrames = newTexture2D[15];
for (int i = 0; i < 15; i++)
myFrames = Texture2D.FromFile(graphicsService.GraphicsDevice, string.Format("myfile{0}.jpg", i));Then inside your update method, increment a "current frame" int based on the elapsed time, and use it to fetch the correct frame from the array.
I really can't say whether that's the best way of doing it, or if you'd be better off stitching them together and just reading from disk once, but it should work. I'd of course recommend wrapping it up inside a class, so your main class isn't littered with currentFrame counters for each sprite you have :D
Yeah, that's what I was thinking of doing but I didn't know if it was more effecient to have it as one texture. For me, programming it I think it's easier in an array anyways.
Typically it will always be better practice to load "sheets" of sprites as single textures rather than loading dozens of individual textures for single frames of animation. A much more effective way to deal with sprites is to be smart about using the source rectangle in calls to SpriteBatch.Draw(). Looping though sprites, I highly reccomend simply changing the parameters of the source rectangle to correspond to the target frame of animation.
I don't know of any, but it's probably not more than a fewlines of code in C# using the Bitmap class. Create a new botmap at the full size (new Bitmap((frame width * number frames, frame height)), and loop through them (Bitmap.FromFile(?)) copying them into the new one, then save it out.
Incidentally, I just noticed the Spacewar gameuses an array:
sun =
newTexture2D[numFrames];sun[0] =
BubbleMoleGame.TextureCache[BubbleMoleGame.Settings.MediaPath + @"\textures\suntest1.tga"];sun[1] =
BubbleMoleGame.TextureCache[BubbleMoleGame.Settings.MediaPath + @"\textures\suntest2.tga"];sun[2] =
BubbleMoleGame.TextureCache[BubbleMoleGame.Settings.MediaPath + @"\textures\suntest3.tga"];sun[3] =
BubbleMoleGame.TextureCache[BubbleMoleGame.Settings.MediaPath + @"\textures\suntest4.tga"];sun[4] =
BubbleMoleGame.TextureCache[BubbleMoleGame.Settings.MediaPath + @"\textures\suntest5.tga"];I wonder if there's a reason they didn't use a single file like has been suggested is better, or if it was just easier/quicker for the demo.
Rick Hoskinson wrote:
Typically it will always be better practice to load "sheets" of sprites as single textures rather than loading dozens of individual textures for single frames of animation. A much more effective way to deal with sprites is to be smart about using the source rectangle in calls to SpriteBatch.Draw(). Looping though sprites, I highly reccomend simply changing the parameters of the source rectangle to correspond to the target frame of animation.
This method worked beautifully for me just using a frame counter and drawing the source rectangle accordingly.
As long as your image is done well, it will look super smooth.
Thanks for the advice! Im sure I will need a bunch more as I try to learn C#.
Ponchous wrote:
Can you give an example of how to do that? I am stumped. I have a sheet of sprites. But i don't know how to do it.
I have a tutorial for using a sprite sheet to do some simple animations. You should be able to work through the tutorial and get it figured out how to use a single sheet for doing animations in XNA.
http://www.xnadevelopment.com/tutorials/thewizard/theWizard.shtml
Hope that helps!
You could also keep your frames on a single sheet as a resource and then pull them into a texture array at run time. That would mean that at draw time, you wouldn't have to calculate a new rectangle position each time.
All it would need is a bit of planning when laying out your sprites on the sheet to keep the frames within a consistent sized box, but that's certainly not an issue for most artists.
I think it all boils down to the overhead for using certain techniques. In Java on mobile phones, clipping rectangles can be a hit on performance but on a PC, that shouldn't be an issue. You could pre-build an array of positions, widths, heights and origins if you wanted to cut down on calculations but I honestly can't see the performance being a concern. I think with this level of hardware, flexibility is the key, so whichever is easiest for you to use.
There's a batch image-stitching program called ImageKlebor over at http://www.download.com/ImageKlebor/3000-2186_4-10510675.html?tag=lst-0-1
It's the only one I've found that stitches PNGs as well as other formats.
Hope that helps! :)
I have created a tool to help create Sprite Sheets/Sprite Strips. Check out this post for more information and to download the tool:
http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=787022&SiteID=1
Regards:
John
A supplementary approach I’ve come across when using a Sprite batch image is to create a separate AnimatedTexture class to handle animations from these tile sets.
A good example of this can be found in the http://www.xnaresources.com/blog.asp?action=viewarticle&articleid=35 "Kill All Bunnies" source, it's good as a starting position.
Remember though, if your using the texture more than once (or a couple of times), you only want to load the texture once and re-user it everywhere else, save memory space, bit of a basic thing to point out but an important one.
Darkside