Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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!

share|improve this question
add comment

2 Answers 2

up vote 9 down vote accepted

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.

share|improve this answer
    
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
add comment

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;
    ...
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.