Implementing a game loop in Silverlight 1.1 Alpha
As Danny Day of the Game.Dev community will tell you, good game development is all about cheats and tricks; fooling the player into believing they’re part of a living environment and rendering marvels before their eyes when you’re actually taking every shortcut you can to save on CPU cycles.
Well, I’ve discovered a trick I would like to share with you and it’s how to implement a game loop in Silverlight so you can do move objects along deterministic paths, test for collisions, make decisions and, in effect, make real-time games.
Because Silverlight is event driven we have to create and use a Storyboard object with a duration of 0:0:0 (one frame) and hook into the Completed event of the Storyboard to perform our looping code. An important note here is that the Storyboard MUST NOT have a RepeatBehaviour property because the Completed event only fires at the end of the activity cycle (which includes all the repeats) and if you set RepeatBehaviour to “Forever” then the Completed event will never fire. This means that in the function that is called by the Completed event we must restart ( .Begin() ) the Storyboard for the next frame.
XAML
<Storyboard x:Name=“animation“
Completed=“javascript:loopcode“
Duration=“0:0:0“>
</Storyboard>
JavaScript
function loopcode(sender, args)
{
// TODO
sender.findName(“animation”).begin();
}
When you instantiate your Silverlight object in your webpage you can set the MaxFrameRate which means faster machines won’t execute faster than normal machines, although obviously you’ll have a performance hit on slower machines. You should be able to setup successful timers for actions although I’d encourage you to investigate using another Storyboard object like the one used for the main loop. By default the MaxFrameRate is set to 24 and the max rate you can have is 64 “Any value that is set to greater than 64 is considered to be set to 64.”
Of course I have a demo you can download! You can get the source here and in it I’ve setup a Storyboard as a loop and a counter which increments every frame. I then have a TextBlock which displays the counter (and an image of the SA Developer .NET lemming and the URL to the community I help run; but they’re just there for show).
Now it’s time to make a game with Silverlight, and my first attempt will be to minic the first tutorial from the GameMaker website “Catch the Clown”. A simple game with a clown that bounces around the room and the player must click on the clown to score points. Each time the clown is clicked it reappears in a different local and coninues to bounce around but this time a litle faster, so it’s harder to catch
Wish me luck!



Hi,
I implemented a game loop similar to the storyboard engine in your examples (except using C#, so if anything it should be faster). For a light load, it works great, but when I try to do something more practical with it, it chokes badly.
I am having two problems:
1. If a single frame takes too long to update (seems like about > 50ms), the whole game sputters and goes through clumps of unresponsiveness, sometimes for nearly a minute at a time! Interestingly, adding a GC.Collect() call in the update loop smooths some of it out, and if I set an interval in the storyboard that is greater than the time it takes to update a frame, performance becomes uniform again (albeit bad).
2. Problem 1 isn’t so bad, since it really shouldn’t take more than 50 ms to render a frame, right? Well I have a collection of Image controls representing the game board and sprites in my game… using transparency encoded .PNGs. If I leave them all stationary, everything is fine. I can even animate a few of them (~10 per frame) without performance impact. But once I start trying to move too many of them around (15-20 per frame) the performance drops sharply. I have tried setting their position both with the Left/Top properties, and with a TranslateTransform, but the result is the same — bad.. really bad.
Any idea why this could be?
Have you had any success animating 20+ image-based sprites?