Ecrofirt

Let me start off by saying that I ALREADY KNOW how to animate sprites.

I think the way I've been doing my animation, though, isn't really recommended as it can cause hiccups.

Here's what I've been doing:

I've got a sprite class.
This class has a DateTime member called startTime. It also has a TimeSpan member called timePassed.
In my constructor I have the following bit of code at the very end:
public Sprite()
{
...
DateTime startTime = DateTime.Now;
}

which sets the initial point of reference for the sprite.

I've also got an UpdateFrame function that basically checks the current time, sees if enough milliseconds have passed, and either exits or changes the frame of the sprite:

//this will check to see if it's time to change the animation frame.
private void UpdateFrame()
{

timePassed = DateTime.Now - startTime;



int totalMilliseconds = timePassed.Milliseconds;

if (totalMilliseconds >= 250)
{

frame++;
frame = frame % 4; //keep it within the 4 frame boundry of this sprite

startTime = DateTime.Now;

}

}

Now I know this works. It works well.

The thing is, I think it might work better for me if I use the current gameTime and such. Doing it this way seems like it's more along the lines of what's expected with XNA.

Can someone perhaps tell me if they way I'm doing it is fine, or perhaps show me a quick example using GameTime instead

I was thinking I could use the GameTime object to return the total time since start up, and use that as my reference check point. Dunno if this is preferred or not, though.

Thanks!


Re: XNA Game Studio Express Sprite animation

dczraptor

use:

float timePassed = 0;

public void UpdateFrame(GameTime gameTime)
{

timePassed += gameTime.ElapsedGameTime.TotalMilliseconds;

if(timePassed >= 250)

{

frame++;

frame = frame % 4;

timePassed = 0;
}
}






Re: XNA Game Studio Express Sprite animation

Ecrofirt

Wow, that's easier than I was making it out to be.

Thanks a bundle.