Skip to Main Content
Learning XNA 3.0
book

Learning XNA 3.0

by Aaron Reed
November 2008
Beginner content levelBeginner
510 pages
16h 24m
English
O'Reilly Media, Inc.
Content preview from Learning XNA 3.0

Adjusting the Animation Speed

While adjusting the framerate of the game itself does affect the three rings animation speed, it's not the ideal way to do so. Why is that? When you change the framerate for the project, it will affect the animation speed of all images, as well as things like the speed of moving objects and so on. If you wanted one image to animate at 60 fps and another to animate at 30 fps, you wouldn't be able to accomplish that by adjusting the overall game's framerate.

Remove the line you added in the previous section that set the TargetElapsedTime member of the Game1 class, and let's try a different route.

When adjusting a sprite's animation speed, you typically want to do so for that sprite alone. This can be done by building in a way to move to the next frame in the sprite sheet only when a specified time has elapsed. To do this, add two class-level variables, which you'll use to track the time between animation frames:

int timeSinceLastFrame = 0;
int millisecondsPerFrame = 50;

The timeSinceLastFrame variable will be used to track how much time has passed since the animation frame was changed. The millisecondsPerFrame variable will be used to specify how much time you want to wait before moving the current frame index.

The actual cycling of animation frames happens in your Update method. So, the next step is to check the elapsed time between animation frames and run the code that moves the current frame only if the desired elapsed time has been reached. Modify the ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Learning XNA 4.0

Learning XNA 4.0

Aaron Reed
Beginning C# 7 Programming with Visual Studio 2017

Beginning C# 7 Programming with Visual Studio 2017

Benjamin Perkins, Jacob Vibe Hammer, Jon D. Reid

Publisher Resources

ISBN: 9780596154905Supplemental ContentErrata Page