Jump to content
  • Advertisement
Sign in to follow this  
lipsryme

Spritesheet animations over multiple rows

This topic is 2550 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

UPDATE: Alright I think I did it with some help of another guys drawing code.

Hi guys, I've been using this XNA Platformer Kit for a Platformer Project. But my artists now tell me they want more animation frames. Now with a framesize of 256 I can only have up to 16 frames with a single row because of the 4096 pixel limit in XNA. The only way to do this is to modify the sprite drawing so I can do multiple rows but I just can't figure out how to.
Please take a look at this code:


/// <summary>
/// Begins or continues playback of an animation.
/// </summary>
public void PlayAnimation(Animation animation)
{
// If this animation is already running, do not restart it.
if (Animation == animation)
return;

// Start the new animation.
this.animation = animation;
this.frameIndex = 0;
this.time = 0.0f;
}

/// <summary>
/// Advances the time position and draws the current frame of the animation.
/// </summary>
public void Draw(GameTime gameTime, SpriteBatch spriteBatch, Vector2 position, SpriteEffects spriteEffects, float scale = 1.0f)
{
if (Animation == null)
throw new NotSupportedException("No animation is currently playing.");

// Process passing time.
time += (float)gameTime.ElapsedGameTime.TotalSeconds;
while (time > Animation.FrameTime)
{
time -= Animation.FrameTime;

// Advance the frame index; looping or clamping as appropriate.
if (Animation.IsLooping)
{
frameIndex = (frameIndex + 1) % Animation.FrameCount;
}
else
{
frameIndex = Math.Min(frameIndex + 1, Animation.FrameCount - 1);
}
}

// Calculate the source rectangle of the current frame.
Rectangle source = new Rectangle(FrameIndex * Animation.Texture.Height, 0, Animation.Texture.Height, Animation.Texture.Height);

// Draw the current frame.
spriteBatch.Draw(Animation.Texture, position, source, Color.White, 0.0f, Origin, scale, spriteEffects, 0.0f);
}

Share this post


Link to post
Share on other sites
Advertisement
Instead of using the frameindex directly
Rectangle source = new Rectangle(FrameIndex * Animation.Texture.Height, 0, Animation.Texture.Height, Animation.Texture.Height);

Calculate the row and column from the frameIndex:
int column = frameIndex % framesPerRow
int row = frameIndex / framesPerRow

Use that to create the rect:
Rectangle source = new Rectangle(column * Animation.Texture.Height, row * Animation.Texture.Height , Animation.Texture.Height, Animation.Texture.Height);

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!