Enabling/Disabling GameComponents

By default, when you create an instance of a GameComponent and add it to the list of components in a game, the GameComponent is wired into the game loop. When the game's Update method is called, so is the Update method of the GameComponent, and so on.

There are two properties that can be used to enable and disable a GameComponent. The Enabled property of a GameComponent will determine whether its Update method is called when the game's own Update method is called. Likewise, the Visible property of a DrawableGameComponent will determine whether its Draw method is called when the game's Draw method is called. Both of these properties are set to true by default. Go to the Initialize method in your Game1 class and set both properties to false immediately after adding the component to your list of game components (added lines in bold):

spriteManager = new SpriteManager(this);
Components.Add(spriteManager);
spriteManager.Enabled = false;
spriteManager.Visible = false;

Tip

Why start the SpriteManager in a disabled state? Remember that the game starts in the GameState.Start state, which will be used for a splash screen of some sort. You're not going to want sprites flying in and out of the screen at this point in the game. Hence, you'll start the game with a disabled SpriteManager, and then, when the splash screen closes, you'll move to a game playing state and activate the SpriteManager.

Next, add some code to show some text when the game is in the GameState.Start ...

Get Learning XNA 3.0 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.