Page 1 of 1

XNA Sprite Sheets

Posted: Sat May 22, 2010 3:18 pm
by epicasian
Can someone point me in the right direction for a simple tutorial?

Thanks,
EpicAsian

Re: XNA Sprite Sheets

Posted: Sat May 22, 2010 3:37 pm
by davidthefat
http://creators.xna.com/en-US/sample/spritesheet
There are tons of tutorials on this site, which is owned and operated by microsoft, its their main XNA site

Re: XNA Sprite Sheets

Posted: Sat May 22, 2010 4:04 pm
by epicasian
I've looked at that, I guess I was to aggravated to actually sit down and look at all the code.

Thanks,
EpicAsian

Edit:
So, in XNA, The sprite sheets are just a lot of different, single image files?

I'm used to loading in one file with many sprites on it (SDL).

~EpicAsian

Re: XNA Sprite Sheets

Posted: Mon May 24, 2010 8:15 am
by mattheweston
You basically have one image with many sprites on it(Spritesheet). From there you have to locate within the spritesheet the individual sprite you want to draw.

First you add the spritesheet image to your project.
You then load the spritesheet into a 2D texture.
Then you can setup rectangles for the individual sprites you need.
Finally you use the rectangles that you created in your calls to the draw function.


This is at least what I've learned so far in my study of XNA.

Re: XNA Sprite Sheets

Posted: Tue May 25, 2010 10:49 am
by MrDeathNote
mattheweston wrote:You basically have one image with many sprites on it(Spritesheet). From there you have to locate within the spritesheet the individual sprite you want to draw.

First you add the spritesheet image to your project.
You then load the spritesheet into a 2D texture.
Then you can setup rectangles for the individual sprites you need.
Finally you use the rectangles that you created in your calls to the draw function.


This is at least what I've learned so far in my study of XNA.
^This is the basic idea of it. I'll try go into a bit more detail:

Code: Select all

                spritebatch.Draw(texture,
                position,
                new Rectangle(currentFrame.X * frameSize.X,
                    currentFrame.Y * frameSize.Y,
                    frameSize.X, frameSize.Y),
                Color.White, 0, Vector2.Zero,
                1f, SpriteEffects.None, 0);
the section you should be concerned with is the third parameter.

Code: Select all

                new Rectangle(currentFrame.X * frameSize.X,
                    currentFrame.Y * frameSize.Y,
                    frameSize.X, frameSize.Y),
This finds the current frame in the tilesheet. You need two Vector2's currentFrame(to hold the position of the frame on the tilesheet eg 0,0 is the first frame starting at the top left corner) and frameSize(which holds the size of each frame eg if the sheet was full of 32x32pixel tiles then frameSize would hold 32, 32).