8
\$\begingroup\$

I have a small 2D game I'm working on in XNA. So far, I have a player-controlled ship that operates on vector thrust and is terribly fun to spin around in circles. I've implemented this as a DrawableGameComponent and registered it with the game using game.Components.Add(this) in the Ship object constructor.

How can I implement features like pausing and a menu system with my current implementation? Is it possible to set certain GameComponents to not update?

Is this something for which I should even be using a DrawableGameComponent? If not, what are more appropriate uses for this?

\$\endgroup\$

1 Answer 1

6
\$\begingroup\$

You can set the Enabled property on the game component to false. The game won't update it. You can also set the Visible property to false and the game won't tell it to draw. It can get kind of messy when you have a lot of game components. You might want to consider using some game state management and sub dividing your game components into separate states.

That way game components that are part of the game play can be told to not draw/update when the game state is told not to update or draw, etc.

This is a pretty good example of game state management: Game State Management Sample

So then you could have your PlayScreen (the state with the game play in it) and have a the PauseScreen come on top of it. The PlayScreen wouldn't be updated, but it would still draw. (or something of that nature)

\$\endgroup\$
3
  • 2
    \$\begingroup\$ So the Enabled and Visible properties control whether or not the Update() and Draw() methods, respectively, will be called? If Visible is false, will ANY code in the Draw() method execute or is it simply not called? Do they affect anything else? \$\endgroup\$
    – Bill
    Feb 5, 2011 at 18:24
  • 3
    \$\begingroup\$ They won't be called, and they only affect Update (Enabled) and Draw (Visible). \$\endgroup\$
    – r2d2rigo
    Feb 6, 2011 at 1:20
  • \$\begingroup\$ Then this is exactly the functionality I was hoping for. Thanks. \$\endgroup\$
    – Bill
    Feb 6, 2011 at 5:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .