How do you handle levels?

Jan 17, 2012 at 4:44 AM

I've got my levels as Usercontrols (1 per level) which i add or remove from my main game page as required.

This is generally pretty good, but it means i can't add any dynamic objects in the level control as it violates the hierarchy and i get the "Element is already a child of another element" error.

Just wondering if there's a better way to go about this or how other people are handling it?

Jan 17, 2012 at 12:42 PM

In previous games I used separate usercontrols as levels, but I found there was a significant overhead in WP7 for instantiating new controls.

So in newer apps I try to pre-create the objects up front and then just position them according to positions I set via a level designer. It is much faster on WP7!

Feb 2, 2012 at 10:33 PM

Hi Andy,


Could you elaborate a bit more on how you do this? Currently I’m designing levels in blend, the levels themselves are user controls which I add to my game page. If there's anything more complicated that needs to be done as a user control, I have the level user control set the position but add it at the game page level to avoid nesting problems with canvases.


You mentioned a level editor, is this something you’ve written or are you using Glee2D (or something like that)? 

Feb 6, 2012 at 1:35 PM

If you are using Blend as your Level Editor (I've done that for Boss Launch 2 and some other little games), then what you can do is create an instance of your user control in code and then iterate through the usercontrol's main canvas Children, something like...

foreach (UIElement elem in LayoutRoot.Children)
    // look in your PhysicsSprite cache for a matching sprite and position accordingly

... so you are basically just creating an instance of the user control so that you can find what sprites are positioned where, and instead using the sprites already created/ cached before from previous levels.

Hope this helps.


Jun 19, 2012 at 1:22 PM

Instead of using user controls, could pages be used? As in, each level would be a different page and to load the level, I just navigate to that page. Is there any disadvantage in doing it this way?

Jun 19, 2012 at 1:30 PM

On a WP7 device, you will notice some lag when instantiating/creating a lot of physics objects. So it is best to do this once up-front, and the re-use the physics objects that you created when possible (a cache of objects).

But if you absolutely want to go the page route, just make sure you clean things up (remove event handlers and clean up resources on pages).