Rest method (make the computer chill)
I'm trying to put together a method that makes the computer rest or delay itself from going too fast or from looping through the game again. Here's the code I put together, but it doesn't seem to be working.
void
Rest(int
milliseconds){
for
(int
i = 0; i < milliseconds; )i += ElapsedRealTime.Milliseconds;
}
Has anyone put together something similar that works?
[516 byte] By [
Leipage] at [2007-12-25]
In reality your method doesn't make the computer rest at all - it just makes it sit in a tight loop.
The correct way to do what you want is to use Thread.Sleep()
To be honest if you use the Game class it will automatically do this for you and only run every 1/60s (or based on your refresh rate). So there is not much reason to do this.
Nice, that seems to work.
I have a for loop where it adds points and plays a sound for every missile that is left over at the end of the level, but it is doing everything way too fast. This just slows things down to a reasonable level
Its probably better to handle that by just checking the time in the Update method. If you add pauses yourself you will affect the framerate of the game plus if there is any other animation you want to do you wont be able to.
Look in the spacewar code for how I did the number counting at the ends of the rounds.
Maybe this simple class will be useful to you:
/// <summary>
/// A simple timer that 'ticks' at a specified frequency
/// </summary>
class Ticker
{
/// <summary>Frequency of the ticker</summary>
private uint _frequency;
/// <summary>ParticleSystem time of the last tick</summary>
private uint _lastTick;
/// <summary>
/// Default constructor
/// </summary>
/// <param name="frequency">frequency of the ticker</param>
public Ticker(uint frequency)
{
Frequency = frequency;
//Mark the last tick as the current system time to avoid an
//immediate tick
_lastTick = (uint)Environment.TickCount;
}
/// <summary>
/// Gets or sets the frequency of a ticker
/// </summary>
public uint Frequency
{
get { return _frequency; }
set { _frequency = value; }
}
/// <summary>
/// Check to see if the ticker has ticked since the last call to this method
/// </summary>
/// <returns>True if the ticker has 'ticked'</returns>
public bool HasTicked()
{
uint time = (uint)Environment.TickCount;
if (time > (_lastTick + _frequency))
{ //The ticker has ticked!
_lastTick = time;
return true;
}
else
{ //The ticker hasn't ticked yet
return false;
}
}
}