0
\$\begingroup\$

I am using the Windows Phone Template "Windows Phone XAML and XNA Apps" and targeting Windows Phone 7/8. Most examples show your game inheriting from Microsoft.Xna.Framework.Game and then adding Microsoft.Xna.Framework.GameComponent items to the Components collection. But as my game page inherits from PhoneApplicationPage there isn't a Components collection or a Game property.

How can I use GameComponent from within PhoneApplicationPage?

\$\endgroup\$
3
\$\begingroup\$

You can't. Important parts of their infrastructure are contained within the Game class. (Also, GameComponent's constructor takes a Game.)

My advice (as always) is to not use GameComponent. Like the Game class itself, game components are a completely optional part of the framework. And often they're more trouble than they're worth.

You're better off just creating "normal" classes - however you like - and making a normal List to contain them. If you need any other functionality that the game component infrastructure provides, that's pretty simple to implement too.

For reference, that functionality basically amounts to: a mechanism for adding/removing/re-ordering items while the list is being iterated (as simple as copying the list before iterating), sorting components, disabling/hiding components, and calling the various methods for you at the obvious times.

That being said, if you really want to use something identical to GameComponent, you could take the relevant code from ExEn (or MonoGame, although I'm not sure what their code for this is like). The code in question is confined to the GameComponents directory and should be fairly independent. Unlike XNA itself, in ExEn the magic happens in the resuable GameComponentCollection class instead of deep inside the Game class.

(I looked into using XNA's own code for this - but it's not really possible to do in a way that isn't terrifying.)

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.