开发者

Pausing between operations

开发者 https://www.devze.com 2023-03-04 15:03 出处:网络
I am trying to build a simple memory game. Clicking on a card that has not been \"flipped\" simply \"flips\" the card over (revealing the underside of the image).

I am trying to build a simple memory game. Clicking on a card that has not been "flipped" simply "flips" the card over (revealing the underside of the image).

When a card is already showing and a second card is flipped over, I would like to pause for one second. Then, if the card that was flipped over matches the first card, I remove it from the board, if it does not match, I would like to flip both cards back to their hidden stage.

I have the "flipping" coded, I just want to know how I can pause it for one second after the second card is flipped.

I've tried:

System.Threading.Thread.Sleep(1000)

and

Dispatcher.BeginInvoke(() => System.Threading.Thread.Sleep(1000));
开发者_Python百科

But it doesn't work like I want. This is my first WP7 and Silverlight project, so not sure what I'm doing wrong.

Any advise would be greatly appreaciated!


use the DispatcherTimer class:

var timer = new DispatcherTimer();
timer.Interval = new TimeSpan(0,0,0,1);
timer.Tick += SomeTickMethod;
timer.Start();

private void SomeTickMethod(obejct sender, EventArgs e) {
    FlipBackCards();
    //remember to stop it :)
    ((DispatcherTimer)sender).Stop();
}

then in your Tick method you flip over the card. You could make the timer a class member in which case (based on some of your own logic) you could Stop() the timer from firing at any time.

Hope that helps

(note I quickly just typed up this code, may not be 100%, should be close)

0

精彩评论

暂无评论...
验证码 换一张
取 消