Reputation: 7590
i wan't to know if there is a way to decrease the Run Speed (Update and Draw time between calls).
I need to slow down the frequency of calls because they occurs too fast, and that has the consequence of show what i need to show in very little time, so little that actually the user can't see anything.
The objective is show more slowly the progress of what represent the logic of my application.
If the title isn't clear, i refer to the XNA Game Framework.
Upvotes: 1
Views: 4335
Reputation: 7590
I found the answer in MSDN. The key is set true IsFixedTimeStep and then increase or decrease the variable TargetElapsedTime
First I need to set to true the property IsFixedTimeStep, then I could play with the variable TargetElapsedTime(rate at what Update is call) that for default is 1/60 sec, too fast for what I need to show, so i set it in 1/2 sec, and add two keys to modify this value in runtime.
IsFixedTimeStep = true;
TargetElapsedTime = TimeSpan.FromSeconds(0.5);
And then in the update method I could play with the TargetElapsedTime to speed Up and slow Down the update calls:
if ((Keyboard.GetState().IsKeyDown(Keys.W)))
{
TargetElapsedTime += TimeSpan.FromSeconds(0.1f);
}
if ((Keyboard.GetState().IsKeyDown(Keys.S)))
{
if ((TargetElapsedTime - TimeSpan.FromSeconds(0.1f))>TimeSpan.Zero)
{
TargetElapsedTime -= TimeSpan.FromSeconds(0.1f);
}
}
Upvotes: 1
Reputation: 3161
I agree with some of the other answers here. You don't really want to slow the execution of the game down. There is little point in doing this. Plus design later down the track becomes harder as you are stuck with that constant time.
The issue with using time delta's is that you can never be sure how often your update method will be called. There are any number of factors that might cause this to run too fast or too slow. If your game loop runs slow even for a fraction you could miss an entire tick in your game. For something like a sports game this would be bad.
Instead use a timer. .NET has an amazing event model and a pre built timer class. It gets initialized and you tell it how often you want to loop. Each loop will fire an event you can listen to. The advantages of this are two fold. Firstly you can be assured that your tick will fire close to or when you ask it to. Second it does not run in the main game logic allowing your programs to be structured in a better way. You can even bind more than one event to the timer allowing you to me more modular with your code.
Try this...
//create a new instance of a timer
//and set it to 1000 millisecond interval(1 second)
Timer t = new Timer(1000);
//Add a listener to the elapsed event
t.Elapsed += new ElapsedEventHandler(t_Elapsed);
//start the timer
t.Start();
void t_Elapsed(object sender, ElapsedEventArgs e)
{
// I am called each second
}
Upvotes: 0
Reputation: 76
I would recommend you to have some code inside your Update method that updates your game´s logic every x seconds.
Define:
int millisecondsPerFrame = 1000 //Update every 1 second
int timeSinceLastUpdate = 0 //Accumulate the elapsed time
Now, on your update you can have something like:
timeSinceLastUpdate += gameTime.ElapsedGameTime.TotalMilliseconds;
if(timeSinceLastUpdate >= millisecondsPerFrame)
{
timeSinceLastUpdate = 0;
//YOUR GAMES LOGIC GOES HERE
}
With this approach you dont have to slow down the entire program.
Upvotes: 4