Page view counter

Silverlight Tips of the Day - Blog by Mike Snow

Game Programming with Silverlight

Silverlight Tip of the Day #16 - StoryBoard versus DispatcherTimer for Animation and Game Loops.

In Tip of the Day #5 I discussed how to use the DispatcherTimer for your main game loop. However, a better approach may be to use the StoryBoard timer as discussed here or the CompositionTarget.Rendering event which was recently added to Silverlight 2. Check out Tip of the Day #50 for more info on using the CompositionTarget.Rendering event for your main game loop.

From my research, the reasons the StoryboardTimer is better than the DispatcherTimer is as follows:

  1. The StoryBoard is handled on a separate thread that is not affected by the UI thread which the DispatcherTimer is on.
  2. The DispatcherTimer is a lower resolution timer than the timer behind the Storyboard class, which causes loss in fidelity.
  3. The Storyboard execution is more stable across the different supported OS’s and web browsers.

Given that, let’s take a look at how it can be done. In the example below, we create a StoryBoard timer and increment and display a count variable that represents the number of times the MainGameLoop was called. I have set my duration between calls to be zero milliseconds but you will want to change this to whatever best fits your animation story.

Page.xaml.cs:

namespace SilverlightApplication8
{
    public partial class Page : UserControl
    {
        Storyboard _gameLoop = new Storyboard();
        int count = 0;
 
        public Page()
        {
            InitializeComponent();
            _gameLoop.Duration = TimeSpan.FromMilliseconds(0);
            _gameLoop.Completed += new EventHandler(MainGameLoop);
            _gameLoop.Begin();
        }
 
        void MainGameLoop(object sender, EventArgs e)
        {
            // Add any game logic/animation here.
            // Example:
            myTextbox.Text = count.ToString();
            count++;
 
            // Continue storyboard timer
            _gameLoop.Begin();
        }
    }
}
Page.xaml:
<UserControl x:Class="SilverlightApplication8.Page"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Width="400" Height="300">
    <Grid x:Name="LayoutRoot" Background="White">
        <TextBlock x:Name="myTextbox">Display Counter</TextBlock>
    </Grid>
</UserControl>

Thank you,
--Mike Snow

 Subscribe in a reader

Comments

Silverlight news for July 10, 2008 said:

Pingback from  Silverlight news for July 10, 2008

# July 10, 2008 5:10 AM

Community Blogs said:

Rajeev Goel with Silverlight Nuggets, Michael Scherotter on SL and IE8, Adam Kinney reports on Extreme

# July 11, 2008 1:35 AM

Mike Snows Silverlight Blog said:

Main Game Loop NOTE : Please see Tip of the Day #16 – I have found that using the StoryBoard control

# July 16, 2008 2:19 PM

Visual Web Developer Team Blog said:

6 new Silverlight tutorials are completed! Tip of the Day #15 - Communicating between JavaScript &amp;

# July 17, 2008 5:19 PM

Pietro Brambati Blog said:

Updated 18/7/2008 Percorso formativo in italiano Aggiornamento del percorso formativo su Silverlight

# July 18, 2008 8:21 AM

Pietro Brambati Blog said:

Updated 18/7/2008 Percorso formativo in italiano Aggiornamento del percorso formativo su Silverlight

# July 18, 2008 8:27 AM

?????????????????? “?????????????????? ??????????”?? ???????????????? ???????????? ???????? said:

Pingback from  ?????????????????? &#8220;?????????????????? ??????????&#8221;?? ???????????????? ???????????? ????????

# July 18, 2008 6:22 PM

Mike Snows Silverlight Blog said:

Each Silverlight element exposes a property called RenderTransform that is used to set the transform

# August 14, 2008 3:03 PM

BabbaBlog said:

Silverlight tip #1: Game Loops

# August 15, 2008 4:37 AM

Mike Snows Silverlight Blog said:

For this tutorial I will be demonstrating how to create a fast, optimized Sprite animation class. In

# August 20, 2008 7:45 PM

Silverlight Tips of the Day - Blog by Mike Snow said:

In the recent release of Silverlight 2 RC0 there is a new event that fires once before the rendering

# September 29, 2008 8:00 PM

Introduction to game programming in Silverlight : Die, AJAX! said:

Pingback from  Introduction to game programming in Silverlight : Die, AJAX!

# October 29, 2008 12:51 AM

Silverlight Tips of the Day said:

Main Game Loop The main game loop is the heart of your game. In this function you will execute the majority

# November 18, 2008 1:58 PM

Silverlight Tips of the Day - Blog by Mike Snow said:

The purpose of this post is to create an outline summary all the blogs from my Silverlight tips of the

# January 2, 2009 5:56 PM

o UAU nosso de cada dia said:

essa lista eu copiei desse blog bárbaro (acompanhe por RSS você também): uma lista de dicas super úteis

# January 3, 2009 6:24 AM

jin_u as blog » Blog Archive » ??????????????? ??? ?????? said:

Pingback from  jin_u as blog  &raquo; Blog Archive   &raquo; ??????????????? ??? ??????

# January 14, 2009 7:54 PM

Recent Faves Tagged With "storyboard" : MyNetFaves said:

Pingback from  Recent Faves Tagged With "storyboard" : MyNetFaves

# February 15, 2009 8:20 AM

Coding, training dog, playing Aikido, harmonica and.... said:

Trong Blog viết về SilverLight của mình, MikeSnow đã so sánh các phương pháp tạo Animation: 1- Sử dụng

# March 16, 2009 12:42 PM

Kevin Yang said:

A storyboard is running in a seperate thread?? is that really true?

according to what i 've learn, there should be one ui thread, and if storyboard can access the ui elements directly, it should be located in the same ui thread.

correct me if i'm wrong.

# November 9, 2009 7:31 AM