Skip to main content

Microsoft Silverlight

Answered Question Game loop for SilverlightRSS Feed

(0)

aodendaal
aodendaal

Member

Member

14 points

4 Posts

Game loop for Silverlight

How do I create something like a gameloop for Silverlight where I can set the number of frames per second and do some calculations each frame to manage objects on the canvas?

Initally I want to create something simple like a ball that bounces off the edge of the screen, so I need to test if the ball is at the edge of the screen and calculate the new direction; but at the end I want to have multiple object (and of course the player) interacting and colliding with each other all the time. 

tomtaylormsft
tomtaylo...

Member

Member

379 points

114 Posts

Microsoft

Re: Game loop for Silverlight

the current way to do this is to set up an empty storyboard of 0 duration, and hook the completed event on the storyboard in your code. You then have a per-frame callback that you can do anything you want with.

Bill Reiss
Bill Reiss

Contributor

Contributor

4840 points

919 Posts

Silverlight MVP

Re: Game loop for Silverlight

Can you provide a simple example of this or point me to one? I've been trying to get this working and can't seem to get it right.


Bill Reiss, Coauthor of Hello! Silverlight 3
My blog (rss feed)

aodendaal
aodendaal

Member

Member

14 points

4 Posts

Answered Question

Re: Game loop for Silverlight

I did some testing this morning and you can use a Storyboard to set up something like a game loop.

The trick is that you must NOT use RepeatBehavior because the Completed event only gets called after all repeats have finished and if you set the RepeatBehavior to "Forever" the Completed event will never fire.  What you have to do is in the event you raise you must call Begin() on the storyboard you're using as a timer.

XAML

          <Storyboard x:Name="animation"

              Storyboard.TargetName="cliff"

              Storyboard.TargetProperty="(Canvas.Left)"

              Completed="BLOCKED SCRIPTloopcode"

              Duration="0:0:0">

          </Storyboard>

JavaScript 

function loopcode(sender, args) {

    //TODO

    sender.findName("animation").begin();

}

Joe Stegman
Joe Stegman

Member

Member

82 points

27 Posts

Re: Game loop for Silverlight

You don’t have to use RepeatBehavior – you can manually re-start the animation in the completed event.  There’s an animation timer sample at the end of this blog post:  http://blogs.msdn.com/jstegman/archive/2007/05/05/mix-createfromxaml-and-timer-sample.aspx.

Joe Stegman

Bill Reiss
Bill Reiss

Contributor

Contributor

4840 points

919 Posts

Silverlight MVP

Re: Game loop for Silverlight

Thanks, I got it working, it turns out that my problem was that I was doing the Begin call in the constructor instead of the Loaded event, and that made it throw an error. It's working great now, thanks for the help.

Bill


Bill Reiss, Coauthor of Hello! Silverlight 3
My blog (rss feed)

aodendaal
aodendaal

Member

Member

14 points

4 Posts

Re: Game loop for Silverlight

I made a working demo and uploaded it to my blog and read/download it here.  It contains TextBlock with a number that increments every frame.

Looking at Joe's example from MIX07, I don't know about 1.0 Beta but in 1.1 Alpha the Storyboard does not need to have a TargetName, TargetProperty or child Animation objects.  You can also extent the duration so that it's a more appropriate timer.

aleiby
aleiby

Member

Member

2 points

1 Posts

Re: Game loop for Silverlight

Good stuff.  Here's a bouncing ball example I whipped together with this knowledge (using Python).

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities