School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 300,475 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,749 people online right now. Registration is fast and FREE... Join Now!




XNA Divides SpriteSheets automatically?

 

XNA Divides SpriteSheets automatically?, In your png image?

papuccino1

16 Jun, 2009 - 11:09 AM
Post #1

C# Programmer
Group Icon

Joined: 2 Mar, 2008
Posts: 943



Thanked: 33 times
Dream Kudos: 50
My Contributions
If any of you have seen the Platformer starter kit you know how the characters have sprite sheets. How does XNA know how to divide this sheet into 5,6,7 frames?
IPB Image


For instance,
I made this sprite sheet for my project, do I have to do anything special with my image so it works well with XNA?

IPB Image

User is offlineProfile CardPM
+Quote Post


BetaWar

RE: XNA Divides SpriteSheets Automatically?

16 Jun, 2009 - 11:43 AM
Post #2

#include <soul.h>
Group Icon

Joined: 7 Sep, 2006
Posts: 4,729



Thanked: 269 times
Dream Kudos: 1400
My Contributions
I don't know (Haven't used it) but I would say it probably requires you to tell the number of frames per column and row (that is what I have seen in the past).
User is offlineProfile CardPM
+Quote Post

stayscrisp

RE: XNA Divides SpriteSheets Automatically?

16 Jun, 2009 - 12:00 PM
Post #3

Mouth->Insert(Foot);
Group Icon

Joined: 14 Feb, 2008
Posts: 1,380



Thanked: 53 times
Dream Kudos: 300
My Contributions
Hi

XNA will work this out by using parameters you pass in, if it knows the width of each of your frames it can simply increment that amount each time. You will also specify the amount of frames so once the width has reached that amount it will return to the start.

Could you post the code that XNA uses for this and then I can explain it better icon_up.gif






User is online!Profile CardPM
+Quote Post

papuccino1

RE: XNA Divides SpriteSheets Automatically?

16 Jun, 2009 - 12:13 PM
Post #4

C# Programmer
Group Icon

Joined: 2 Mar, 2008
Posts: 943



Thanked: 33 times
Dream Kudos: 50
My Contributions
I don't have any code right now.

I just noticed that when I edited the tomb raider sprite I made him look like he had a hat on backwards.

On-topic: So I have to manually distributed each frame in equal distances because otherwise the character will move very erratically right?
User is offlineProfile CardPM
+Quote Post

stayscrisp

RE: XNA Divides SpriteSheets Automatically?

16 Jun, 2009 - 12:17 PM
Post #5

Mouth->Insert(Foot);
Group Icon

Joined: 14 Feb, 2008
Posts: 1,380



Thanked: 53 times
Dream Kudos: 300
My Contributions

Yeah they need to be perfect, as long as you have done that then your sprite sheet will work just fine.
User is online!Profile CardPM
+Quote Post

papuccino1

RE: XNA Divides SpriteSheets Automatically?

16 Jun, 2009 - 12:28 PM
Post #6

C# Programmer
Group Icon

Joined: 2 Mar, 2008
Posts: 943



Thanked: 33 times
Dream Kudos: 50
My Contributions
Thanks for the clear answer. smile.gif

Do you mind sharing a method you use to accomplish this? What little trick do you have to make this process a bit easier.
User is offlineProfile CardPM
+Quote Post

Core

RE: XNA Divides SpriteSheets Automatically?

16 Jun, 2009 - 12:38 PM
Post #7

Den The Developer
Group Icon

Joined: 8 Dec, 2008
Posts: 2,944



Thanked: 214 times
Dream Kudos: 900
Expert In: C#, VB.NET, .NET Framework

My Contributions
An interesting article on this topic can be found here.
User is offlineProfile CardPM
+Quote Post

stayscrisp

RE: XNA Divides SpriteSheets Automatically?

16 Jun, 2009 - 12:58 PM
Post #8

Mouth->Insert(Foot);
Group Icon

Joined: 14 Feb, 2008
Posts: 1,380



Thanked: 53 times
Dream Kudos: 300
My Contributions

Well you could create a separate animation class, this class would have functions to store the current frame and frame rate, return the current frame and increment the frame as is needed.

Your player could then call this function in its own movement code, passing in the values needed.

Another helpful way of doing things so that you don't have to keep changing the width and height values for your sprites is to write a function that works that out for you, this is basically just dividing the entire width or height of the sprite sheet by the amount of frames.

Good luck icon_up.gif
User is online!Profile CardPM
+Quote Post

papuccino1

RE: XNA Divides SpriteSheets Automatically?

16 Jun, 2009 - 02:54 PM
Post #9

C# Programmer
Group Icon

Joined: 2 Mar, 2008
Posts: 943



Thanked: 33 times
Dream Kudos: 50
My Contributions
StaysCrisp, would this work for XNA animation?

I positioned each frame at equal distance, and they are the same height and width.

I need some help!

IPB Image
User is offlineProfile CardPM
+Quote Post

SixOfEleven

RE: XNA Divides SpriteSheets Automatically?

16 Jun, 2009 - 05:15 PM
Post #10

Code Guru
Group Icon

Joined: 18 Oct, 2008
Posts: 2,910



Thanked: 165 times
Dream Kudos: 725
Expert In: C, C#, XNA, Game Programming, Programming Concepts

My Contributions
QUOTE(papuccino1 @ 16 Jun, 2009 - 04:54 PM) *

StaysCrisp, would this work for XNA animation?

I positioned each frame at equal distance, and they are the same height and width.

I need some help!

IPB Image


Hey there,

I can help you with this. smile.gif

What you would like to do is create an array of Rectangles(optionally you could also do it with a list but it would be a little harder to code but not impossible). Each Rectangle would hold one frame of the animation. Say your sprite was 64 pixels wide, 100 pixels high. Taking your images you have 6 sprites in the sprite sheet. You can set up the array of rectangles like this:

csharp

Rectangle[] spriteSheet = new Rectangle[6];

for (int i = 0; i < 6; i++)
{
spriteSheet[i] = new Rectancle(
i * WidthofSprite,
0,
WidthofSprite,
HeightofSprite);
}


That would give you the frame for each animation step. Now, I can't help you work on animating the frames at this moment as I'm not at home, but if you can wait until tomorrow, I can help you start with animating them.
User is offlineProfile CardPM
+Quote Post

papuccino1

RE: XNA Divides SpriteSheets Automatically?

16 Jun, 2009 - 05:41 PM
Post #11

C# Programmer
Group Icon

Joined: 2 Mar, 2008
Posts: 943



Thanked: 33 times
Dream Kudos: 50
My Contributions
Thanks for the help Jamie. As always you rock my socks, however I'm going to fall back a bit with this. I'm going to take a step back and rethink my approach to this.

I have a couple of ideas that have to marinate. tongue.gif
User is offlineProfile CardPM
+Quote Post

SixOfEleven

RE: XNA Divides SpriteSheets Automatically?

16 Jun, 2009 - 05:55 PM
Post #12

Code Guru
Group Icon

Joined: 18 Oct, 2008
Posts: 2,910



Thanked: 165 times
Dream Kudos: 725
Expert In: C, C#, XNA, Game Programming, Programming Concepts

My Contributions
QUOTE(papuccino1 @ 16 Jun, 2009 - 07:41 PM) *

Thanks for the help Jamie. As always you rock my socks, however I'm going to fall back a bit with this. I'm going to take a step back and rethink my approach to this.

I have a couple of ideas that have to marinate. tongue.gif


I will be covering animation in my blog here some day. If you don't want to use a sprite sheet, there is an alternative way to do this. You could, in your LoadContent method, load the sprites individually into a List or array. (It would be a bit of pain but it would work. You would just have an index for each item in the list, when you get the last item set it back to zero. The timing for frames is not that hard with XNA.)

csharp

List<Texture2D> LeftFacingSpriteFrames = new List<Texture2D>(); // Class level variable

// Inside the LoadContent method:

Texture2D tempTexture;

tempTexture = Content.Load<Texture2D>("leftFrame0");
LeftFacingSpriteFrames.Add(tempTexture);

tempTexture = Content.Load<Texture2D>("leftFrame1");
LeftFacingSpriteFrames.Add(tempTexture);

// Repeat for all other left facing frames


User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/8/09 03:22AM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month