Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Hi i'm working on my First 2D Game with XNA and I have a little problem. To give a running effect to my Sprite, I scroll through a SpriteSheet with this code(running right):

if (AnimationDelay == 6)
    if (CurrentFrameR.X < SheetSizeR.X)
        CurrentFrameR.X = 1;

    if (CurrentFrameR.Y >= SheetSizeR.Y)
        CurrentFrameR.X = 0;
        CurrentFrameR.Y = 0;

    AnimationDelay = 0;
    AnimationDelay += 1;

xPosition += xDeplacement;

And these are the objects used :

Point FrameSizeR = new Point(29, 33);
Point SheetSizeR = new Point(5, 1);
Point CurrentFrameR = new Point(0, 0);
int AnimationDelay = 0;

I have the same Code with different SpriteSheet when the sprite is running Left. Everything is working fine I'd say 90% of the time but the other 10% the sprite animation stays on one Frame of the SpriteSheet, on both directions(left and right) and it stays stuck until I close the program. The thing is I can't quite figure out why since it never happens at the same moment..Sometimes after 10,15,30 seconds and sometimes even on boot! Any idea why? Thanks in advance and let me know if you need any other parts of the code

share|improve this question
add comment

2 Answers

up vote 6 down vote accepted

I bet it's because AnimationDelay manages to make it past 6 somehow. I'd change the if statement to:

if (AnimationDelay >= 6)
share|improve this answer
Wow.. well seen! Completely solve the thing +1 to you sir and thanks a lot !:) –  PhaDaPhunk Dec 1 '11 at 17:10
@PhaDaPhunk - If that was actually the problem, you probably want to figure out why it was going over six, since that indicates there's something incorrect or unexpected happening in your code. –  Richard Marskell - Drackir Dec 2 '11 at 19:57
add comment

John's question will do, however you might still want to look at doing it 'correctly' in the first place.

// It's important we make the current frame a float; because something like 0.1f
// might be added to it at really high framerates.
// This will refer to each frame as if the tileset was actually a single lines of frames.
// (Even though they are not)
private float CurrentFrame;

********** In your update/draw method. **********

// Animate your sprite at 15 FPS.
CurrentFrame += gameTime.ElapsedGameTime.TotalSeconds * 15;

// Constrain CurrentFrame to the number of frames available.
// (This replaces your if; frame = 0; etc.)
CurrentFrame %= SheetSizeR.X * SheetSizeR.Y;

// Find out the X and Y position of the current cell we want, from
// CurrentFrame. You must use integral division here - as it has a
// few properties that makes mapping a 1D index to a 2D array more
// straight-forward.
CurrentFrameR.X = (int)CurrentFrame % (int)SheetSizeR.X;
CurrentFrameR.Y = (int)CurrentFrame / (int)SheetSizeR.Y;
share|improve this answer
(I'd just like to point out that CurrentFrame should be renamed to currentFrame in case people follow Microsoft's coding guidelines. Which would be nice, since you're working with XNA, and others are going to read your code..) (Private variables should start with a lower case) –  Zolomon Dec 2 '11 at 9:49
@Zolomon that's really pedantic - I was keeping stuff consistent with his question; for all I know his stuff is public. Also, Microsoft uses _currentFrame, m_CurrentFrame and m_currentFrame. –  Jonathan Dickinson Dec 2 '11 at 10:13
I realized that after a few minutes, I meant no disrespect! I suppose I should've commented on his question first. :) –  Zolomon Dec 2 '11 at 14:02
@Zolomon I never take on people about how they write code; unless I have to work with them on that code. If it's a personal project you can name your variables after your dead pets for all I care :). –  Jonathan Dickinson Dec 2 '11 at 14:09
add comment

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.