# Easiest Way To Implement “Slow Motion” and variable game speed in XNA?

I have an XNA 4.0 game that I want to be able to switch into slow motion and back again to full speed every now and then.

So if you kill an enemy the game switches into slow motion as they explode and then goes back to normal.

What is the easiest way to do this in XNA 4.0 without having to alter all my existing code that relies on GameTime?

I have some code that relies on the TotalGameTime, which will be wrong unless I get XNA to slow down. Is there anyway to avoid refactoring that code?

Thanks!

-

For a fast answer, it is pretty simple, you should have something such as:

``````void update(float elapsedTime){
// update code
animation2.update(elapsedTime);
}
``````

All you have to do is to create a float timeScale and multiply it beforehand by the elapsedTime;

``````void update(float elapsedTime){
elapsedTime *= timeScale;
// update code
animation2.update(elapsedTime);
}
``````

Normally, timeScale will be 1.0f, that means a regular time flow; Otherwise just make it smaller than 1 for a slower motion, and more than one for faster motion. You can even animate timeScale towards the desired speed. Only a couple of lines change, and it should do what you want.

-
Thanks! The trick with XNA is that it has a time step like elapsedTime but it also has TotalGameTime which is the total time the game has been running. So I can mess with the time step as you have shown but the TotalGameTime will be wrong. I guess I'll probably have to just refactor to remove any dependencies on it. – TerryB Mar 19 '12 at 2:02
Na, make your own totalGameTime and make it grow by the calculated value (totalGameTime += elapsedTime*timeScale). In the end, it is really just semantics, you may consider "TOTAL" the real time passed, as well as consider "TOTAL" the real in-game time passed.. – Grimshaw Mar 19 '12 at 2:11
Yeah, you're right. I'll just have to stop using XNA's GameTime class everywhere and wrap it in my own to do this. – TerryB Mar 19 '12 at 2:13

It's exactly as DevilWithin said but you can actually retrieve the elapsed time using XNA:

``````public override void Update (GameTime gameTime)
{
float time = gameTime.ElapsedGameTime.Milliseconds * scaledTime;
...
}
``````
-