Silverlight Games 101

Write games in Silverlight 2 using C# by Bill Reiss
Our upcoming Silverlight book for beginners (includes a great chapter on game development in Silverlight!) Hello! Silverlight 2 with Dave Campbell, available online now!



Pages

    Recent posts

    Navigation

    Archive

    Blogroll

      Tampa Divorce Lawyer

      North of Tampa in Lutz, Florida. A Tampa Divorce Lawyer focusing on family, divorce, and real estate law.

      Disclaimer

      The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

      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.

      Posted: Sep 26 2008, 13:10 by Bill Reiss | Comments (22) RSS comment feed |
      • Currently 0/5 Stars.
      • 1
      • 2
      • 3
      • 4
      • 5
      Filed under:

      Comments

      Comments are closed