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.

When it comes to animation and game loops, it seems the overall consent is that using an empty StoryBoard is currently the best, most efficient way to go.

From my research, the reasons why are 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