3 Replies - 3616 Views - Last Post: 16 September 2012 - 01:22 AM

#1 Sasstraliss  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 15-September 12

XNA Sprite Frame Inconsistency

Posted 15 September 2012 - 10:32 PM

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:

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;




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

time = (float)gameTime.TotalGameTime.TotalSeconds;



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:

expFrameID += (int)(time * expFPS) % expFrameCount;



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?

Is This A Good Question/Topic? 0
  • +

Replies To: XNA Sprite Frame Inconsistency

#2 LiberLogic969  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 34
  • View blog
  • Posts: 85
  • Joined: 03-September 12

Re: XNA Sprite Frame Inconsistency

Posted 16 September 2012 - 12:06 AM

Hello,

Yes, I think making a timer for the lose method will solve this. Try changing the time variable to something like this :


time += (float)gameTime.ElapsedGameTime.TotalSeconds;




Then either when you leave the lose method or after your animation is complete do this :


time -= time;




Hope it helps! If any other problems pop up let us know.
Was This Post Helpful? 0
  • +
  • -

#3 Sasstraliss  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 15-September 12

Re: XNA Sprite Frame Inconsistency

Posted 16 September 2012 - 12:50 AM

View PostLiberLogic969, on 16 September 2012 - 12:06 AM, said:

Hello,

Yes, I think making a timer for the lose method will solve this. Try changing the time variable to something like this :


time += (float)gameTime.ElapsedGameTime.TotalSeconds;




Then either when you leave the lose method or after your animation is complete do this :


time -= time;




Hope it helps! If any other problems pop up let us know.



Didn't work. Still doesn't always start from frame 0.


            elapTime += (float)gameTime.ElapsedGameTime.TotalSeconds;

            if (!expIsDrawn)
            {
                expFrameID = 0;
                expFrameID += (int)(elapTime * 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;
                    elapTime -= elapTime;
                }
            }

Was This Post Helpful? 0
  • +
  • -

#4 Sasstraliss  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 15-September 12

Re: XNA Sprite Frame Inconsistency

Posted 16 September 2012 - 01:22 AM

The issue is, everytime lose() is called, elapTime is set to the total gameTime.

I need elapTime to reset to 0 everytime the animation finishes, and elapTime -= elapTime; just doesn't work.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1