I'm making a blackjack game where a card needs to be shown a second after the last card. I've googled it and saw Thread.Sleep - but people said timers would be better for that. How can I do this with timers? Thanks!
Asked
Active
Viewed 1.3k times
7
-
2You shouldn't use either (*especially* don't use `Thread.Sleep`). Accumulate the `gameTime.ElapsedGameTime` when `Game.Update` is called. – Andrew Russell Aug 23 '11 at 05:52
-
1@Andrew Russel +1 Thread.Sleep should be avoided in 99.9% of cases where you might be tempted to use it! – MattDavey Aug 23 '11 at 08:05
-
Can you give an example? – Tom Aug 23 '11 at 08:43
-
@Matt - why? One of the reasons for implementing functionality in seperate threads is that such actions can be simply taken 'in-line' - if a protocol needs a two second delay, sleep(2000), is a one-liner that does not adversely affect any other thread or require any timer-driven state-machine. Sleep() can, of course be misused, like almost anything. – Martin James Aug 23 '11 at 08:45
-
@Martin James there is much discussion on the subject.. http://stackoverflow.com/questions/1096794/is-sleep-evil – MattDavey Aug 23 '11 at 13:32
2 Answers
11
float WaitTimeToShowCard = 0;
public void Update(GameTime gametime)
{
if (HasToShowCard ())
{
WaitTimeToShowCard = 1;
}
if (WaitTimeToShowCard >0)
{
WaitTimeToShowCard -= (float) gametime.Elapsed.TotalSeconds;
if (WaitTimeToShowCard <=0)
{
WaitTimeToShowCard = 0;
ShowCard();
}
}
}
or
public class Timer
{
public Action Trigger;
public float Interval;
float Elapsed;
Timer() {}
public void Update(float Seconds)
{
Elapsed+= Seconds;
if (Elapsed>= Interval)
{
Trigger.Invoke();
Destroy();
}
}
public void Destroy()
{
TimerManager.Remove(this);
}
public static void Create(float Interval, Action Trigger)
{
Timer Timer = new Timer() { Interval = Interval, Trigger = Trigger }
TimerManager.Add(this);
}
}
public class TimerManager : GameComponent
{
List<Timer> ToRemove = new List<Timer>();
List<Timer> Timers = new List<Timer>();
public static TimerManager Instance;
public static void Add(Timer Timer) { Instance.Timers.Add( Timer ); }
public static void Remove(Timer Timer) { Instance.ToRemove.Add(Timer); }
public void Update(GameTime gametime)
{
foreach (Timer timer in ToRemove) Timers.Remove(timer);
ToRemove.Clear();
foreach (Timer timer in Timers) timer.Update( (float) gametime.Elapsed.Totalseconds);
}
}
public class Game
{
public void Initialize() { Components.Add(new TimerManager(this);}
public Update()
{
if (HasToShowCard(out card))
{
Timer.Create(1, () => card.Show());
}
}
}

Blau
- 5,742
- 1
- 18
- 27
0
using System.Timers;
.
.
.
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Timer t = new Timer(1000);
//The number is the interval in miliseconds
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
t.Elapsed += new ElapsedEventHandler(t_Elapsed);
t.Enabled = true;
}
void t_Elapsed(object sender, ElapsedEventArgs e)
{
//code
}
.
.
.
}
I copied some of the bits of code to show the positioning of the added code... Also, after typing "t.Elapsed +=" press TAB twice, to create the event handler. If you want to stop the timer, set the "Enabled" property to false. ('t' is a bad variable name)

NBcode
- 149
- 12
-
Is System.Timers.Timer available in the compact framework? Might be worth letting the OP know he's sacrificing Xbox and phone support by using it.. – MattDavey Aug 23 '11 at 08:04
-
Hmm.. I don't know if it is.. but I can't comment on his original question to notify him. (If you see this, and want Xbox or phone support, use the suggestion by Andrew Russel – NBcode Aug 23 '11 at 08:45
-
Eventhough the OP asked for timers, it really isnt the best solution in game development. – TJHeuvel Aug 23 '11 at 09:07