A much cleaner Game Loop in Silverlight 2 RC0
Silverlight 2 RC0 is now available. See the details here:
http://weblogs.asp.net/scottgu/archive/2008/09/25/silverlight-2-release-candidate-now-available.aspx
They've made it much easier to do a game loop now with a new event that has been added. This event fires before the rendering of each frame. So now you don't need an empty Storyboard or a DispatcherTimer. Let's see how we can animate a rectangle, here's the page.xaml:
<UserControl x:Class="SilverlightApplication34.Page"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="400" Height="300">
<Canvas x:Name="LayoutRoot" Background="White">
<Rectangle x:Name="rect" Fill="Red" Width="50" Height="50"/>
</Canvas>
</UserControl>
Then for the game loop, all you need to do is something like the following:
public partial class Page : UserControl
{ double x = 0;
public Page()
{ InitializeComponent();
CompositionTarget.Rendering += new EventHandler(CompositionTarget_Rendering);
}
void CompositionTarget_Rendering(object sender, EventArgs e)
{ x+=.3;
rect.SetValue(Canvas.LeftProperty, x);
}
}
You can then add things like elapsed time between renders, etc. I'll follow up with a post on converting the game loop logic covered before to use this method.