11 Replies - 1868 Views - Last Post: 16 June 2009 - 06:55 PM Rate Topic: -----

#1 papuccino1  Icon User is offline

  • His name was Robert Paulson.
  • member icon

Reputation: 63
  • View blog
  • Posts: 1,121
  • Joined: 02-March 08

XNA Divides SpriteSheets automatically?

Posted 16 June 2009 - 12:09 PM

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?
Posted 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?

Posted Image

Is This A Good Question/Topic? 0
  • +

Replies To: XNA Divides SpriteSheets automatically?

#2 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1138
  • View blog
  • Posts: 7,105
  • Joined: 07-September 06

Re: XNA Divides SpriteSheets automatically?

Posted 16 June 2009 - 12:43 PM

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).
Was This Post Helpful? 0
  • +
  • -

#3 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 998
  • View blog
  • Posts: 4,173
  • Joined: 14-February 08

Re: XNA Divides SpriteSheets automatically?

Posted 16 June 2009 - 01:00 PM

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 :^:
Was This Post Helpful? 0
  • +
  • -

#4 papuccino1  Icon User is offline

  • His name was Robert Paulson.
  • member icon

Reputation: 63
  • View blog
  • Posts: 1,121
  • Joined: 02-March 08

Re: XNA Divides SpriteSheets automatically?

Posted 16 June 2009 - 01:13 PM

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?
Was This Post Helpful? 0
  • +
  • -

#5 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 998
  • View blog
  • Posts: 4,173
  • Joined: 14-February 08

Re: XNA Divides SpriteSheets automatically?

Posted 16 June 2009 - 01:17 PM

Yeah they need to be perfect, as long as you have done that then your sprite sheet will work just fine.
Was This Post Helpful? 0
  • +
  • -

#6 papuccino1  Icon User is offline

  • His name was Robert Paulson.
  • member icon

Reputation: 63
  • View blog
  • Posts: 1,121
  • Joined: 02-March 08

Re: XNA Divides SpriteSheets automatically?

Posted 16 June 2009 - 01:28 PM

Thanks for the clear answer. :)

Do you mind sharing a method you use to accomplish this? What little trick do you have to make this process a bit easier.
Was This Post Helpful? 0
  • +
  • -

#7 Core  Icon User is offline

  • using System.Linq;
  • member icon

Reputation: 774
  • View blog
  • Posts: 5,097
  • Joined: 08-December 08

Re: XNA Divides SpriteSheets automatically?

Posted 16 June 2009 - 01:38 PM

An interesting article on this topic can be found here.
Was This Post Helpful? 0
  • +
  • -

#8 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 998
  • View blog
  • Posts: 4,173
  • Joined: 14-February 08

Re: XNA Divides SpriteSheets automatically?

Posted 16 June 2009 - 01:58 PM

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 :^:
Was This Post Helpful? 0
  • +
  • -

#9 papuccino1  Icon User is offline

  • His name was Robert Paulson.
  • member icon

Reputation: 63
  • View blog
  • Posts: 1,121
  • Joined: 02-March 08

Re: XNA Divides SpriteSheets automatically?

Posted 16 June 2009 - 03: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!

Posted Image
Was This Post Helpful? 0
  • +
  • -

#10 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 945
  • View blog
  • Posts: 6,342
  • Joined: 18-October 08

Re: XNA Divides SpriteSheets automatically?

Posted 16 June 2009 - 06:15 PM

View Postpapuccino1, on 16 Jun, 2009 - 04:54 PM, said:

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!

Posted Image


Hey there,

I can help you with this. :)

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:

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.
Was This Post Helpful? 0
  • +
  • -

#11 papuccino1  Icon User is offline

  • His name was Robert Paulson.
  • member icon

Reputation: 63
  • View blog
  • Posts: 1,121
  • Joined: 02-March 08

Re: XNA Divides SpriteSheets automatically?

Posted 16 June 2009 - 06: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. :P
Was This Post Helpful? 0
  • +
  • -

#12 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 945
  • View blog
  • Posts: 6,342
  • Joined: 18-October 08

Re: XNA Divides SpriteSheets automatically?

Posted 16 June 2009 - 06:55 PM

View Postpapuccino1, on 16 Jun, 2009 - 07:41 PM, said:

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. :P


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.)

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


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1