tags:

views:

134

answers:

2

So i have been googling/binging trying to find an answer on this but drawing up blanks. If I want to have a level type of game with different assets for each level do I do something like

public class Level:DrawableGameComponent
{
}



//somewhere inside functions or w.e
  var Level = new Level(this,"Level1");
  Components.Add(Level));
//Player wins Level
  Components.Remove(Level));
   Level = new Level(this,"Level2");
  Components.Add(Level);

Is this how it is done, I looked at the Platformer Kit that is in XNA 3.1 and for the Level class provided they did not implement the DrawableGameComponent and decided to mimic the same functionality.

+1  A: 

That is a way and it's a fine way to use DrawableGameCompenents. But it's not the only way or the best way. There's nothing wrong with taken that approach for your levels. And if you're going to use DrawableGameComponents it's definitely best to use them at a higher level (like you are for Levels) then say for example making every character and tile on your screen a DrawableGameComponent.

I personally never use them, much like the Platformer Kit I prefer just to create my own classes and mimic what the DrawableGameComponent does. But one way isn't necessarily better than the other.

Also as a side note, while StackOverflow is a fantastic site for Q&A, you're still going to get the fastest and best responses asking over on the XNA forums. The members there are fantastic and often the Microsoft XNA framework developers themselves jump in to answer questions.

George Clingerman
+1  A: 

Often it's easier to avoid using DrawableGameComponent for "gameplay stuff", so that you have better control over when things get drawn/loaded.

For "levels" this is a bit more borderline.

The first issue that comes to mind is that DrawableGameComponent.LoadContent() is only called from Game.LoadContent(). So you'd have to call it manually when switching out levels (same goes for UnloadContent()).

(And come to think of it, if your levels all have separate content, you might want to consider giving each its own ContentManager, so that you can use Unload() on just that level's content.)

You are going to have to do some manual calling anyway. So really this is the difference between implementing DrawableGameComponent and adding it to the the component list, and having a level as a member of your game class and calling its Draw() and Update() functions yourself. There's not much difference, but personally I'd go for the latter.

Andrew Russell