0

I am making a game based on Game State Managment pattern. I made 20 button on a 2nd page that let the user pick the level.

 LevelButton : TexturedDrawableGameComponent

Inside LoadContent() buttons are created and added to GameComponent collection. If I get this right the GameCompoenent is a sort of global collection, so when the user goes back from 4th page to 2nd app will add another 20 buttons.

I added a loop that clears all LevelButtons from the collection, but seems this is wrong.

Is it DrawableGameComponent the wrong tool for creating buttons?

Lukasz Madon
  • 14,664
  • 14
  • 64
  • 108

2 Answers2

3

Yes.

DrawableGameComponent and its friends are optional bits of the XNA framework. Consider them helpers. Simple example implementations.

If you need more or different functionality to what they provide, you must replace them with your own implementation!

In this case it seems that what you want is a "Button" object (like your LevelButton), probably with methods like Update and Draw.

And then you want some kind of "Button Container" object that will hold all these buttons, and keep them drawn and updated when they are on-screen. But not do anything with them while they are off-screen.

(You could create your own instance of GameComponentCollection to manage this, and keep using game components. But you still have to provide your own draw/update/load/unload/initialise logic. The logic for Game.Components is internal to Game and not reusable. I recommend just using a List - keep it simple!)

The alternative to creating multiple collections - which is to add and remove (or disable/enable) components from the global collection - is horrifically ugly and error-prone. Don't do it.

Community
  • 1
  • 1
Andrew Russell
  • 26,924
  • 7
  • 58
  • 104
-1

I think that it is not wrong if it runs.

Maybe you can approach this in several more elegant ways or improving the way you do it... but the matter is that it have to run.

Maybe you are trying to remove the button when you click it, this will give you problems because that button is being updated.

When the level selection screen becomes inactive or destroyed is the time to remove the buttons.

Of course, you can create a button collection class, that manages the list of buttons, and then you only have enable/disable or remove one DrawableGameComponent, and can be useful in other screens.

Blau
  • 5,742
  • 1
  • 18
  • 27
  • "I think that it is not wrong if it runs." LMFAO! There is ALOT that runs that is wrong/done wrong. Not best practice at all!! – Jason94 Nov 26 '12 at 09:17
  • it's wrong for doing a game of course, but this guy is learning, this answer has question context... why is that wrong? a button needs and update and a draw method, why can not use a DrawableGameComponent? is a first step for somebody that is not expert, let him to discover that it can be done better... the matter for him is that it has to run... I don't change my mind ;) – Blau Nov 26 '12 at 09:52