Sign in to follow this  

XNA Sprite Frame Inconsistency

This topic is 1915 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

I have an explosion sprite, with 16 frames.


Everytime the player collides with an asteroid, the gamestate is set to loseScreen, and when the loseScreen is the gamestate, the lose() function is continually called until the gamestate changes.

Inside the lose() function, this code draws the animated spritesheet, which runs through the 16 frame animation. It should always start from frame 0, but it doesn't always. Sometimes it starts at frame 10, and only runs for 6 frames before finishing. That's the issue. The code inside the lose() function to draw the explosion animation is as follows:


[CODE]expFrameID = 0;
expFrameID += (int)(time * expFPS) % expFrameCount;
Rectangle expRect = new Rectangle(expFrameID * expFrameWidth, 0, 63, 63);
spriteBatch.Draw(explosion, shipLocation, expRect, Color.White, 0f, new Vector2(32, 32), 1.0f, pickupSpriteEffects, 0.1f);
if (expFrameID == 15)
expIsDrawn = true;[/CODE]


The time variable is as follows, it gets the total elapsed game time.

[CODE]time = (float)gameTime.TotalGameTime.TotalSeconds;[/CODE]

I've tried changing TotalGameTime to ElapsedGameTime, nothing changed.
It's the following line from the lose() function that I believe is the issue, perhaps:

[CODE]expFrameID += (int)(time * expFPS) % expFrameCount;[/CODE]

It works, and it animates. However it doesn't always start at frame 0. it starts from random frames between 0 and 16, but it still runs correctly at 16 frames per second.

So, how do I get it to always start from frame 0?


Would I solve my issue by making a new time variable that starts a timer from 0 only when the lose() function is called? How do I do that?

Share this post


Link to post
Share on other sites
Well, you are using the total elapsed amount of game time in seconds to determine which frame is going to be drawn. I would use a timer to wait before going to the next frame, it would go something like this

[CODE]
// members
expFrameID = 0;
timer = 0;
UpdateAnimation()
{
// ElapsedGametime gets how long has it been since the last update
timer += (int) GameTimer.ElapsedGameTime.TotalMilisseconds;
if(timer >= 60) // Because you want 16 frames persecond at least, correct? So 60ms I guess.
{
timer = 0;
if(expFrameID == 15)
expFrameID = 0;
else
expFrameID++;
}

Rectangle expRect = new Rectangle(expFrameID * expFrameWidth, 0, 63, 63);
if (expFrameID == 15)
{
expIsDrawn = true;
}
}
Draw()
{
spriteBatch.Draw(explosion, shipLocation, expRect, Color.White, 0f, new Vector2(32, 32), 1.0f, pickupSpriteEffects, 0.1f);
}
[/CODE] Edited by Arthur Souza

Share this post


Link to post
Share on other sites

This topic is 1915 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this