开发者

Implementing DispatchTimer in Silverlight Mobile

I am working on a dice game for WP7 that involves multiple rolls per turn. During the CPU's turn, I need to slow down the processes so the user can see what's going on. After digging around a little bit, I've found that DispatchTimer can be used in this manner.

Problem is, I have no Idea how this works. Looking at the MSDN documentation only leaves me scratching my head, and most of the examples online have it linked to button presses or mouse clicks.

So what I have is a do while loop that calls my dice rolling function five times and changes the dice image to show what the result was.

now the code for the timer is:

DispatcherTimer dispatcherTimer = new DispatcherTimer();
dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
dispatcherTimer.Interval = new TimeSpan(0,0,1);
dispatcherTimer.Start();

and then:

  private void dispatcherTimer_Tick(object sender, EventArgs e)
{
   int i = 0; 
   do
   {
      cpuTurn();
      i++;
   }
   while(i < 6);
}

But this doesn't work.开发者_如何学运维 So what am I doing wrong? am I using the timer in the wrong way?


Yes, the display won't update until your event handler completes. Call cpuTurn() only once. Move the loop counter outside the method, make it a class member. Call dispatcherTimer.Stop() when it counted up to 6. When it is the CPU's turn again, reset the counter and call Start().

Google "event driven programming" to learn more about the kind of programming that's required when you write GUI code.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜