4. Adding Game Objects to the Game Host
Let's focus now on the game class, which is named MultipleObjectsGame in the example project. Instead of deriving from Microsoft.Xna.Framework.Game, it derives from GameFramework.GameHost, so it picks up all the functionality that the GameHost class offers.
In LoadContent, the textures and fonts are
loaded in the same way as in the previous examples, but this time they
are added into the appropriate GameHostListing 7. collections, as shown in
Example 7. Loading game resources into the Host object
protected override void LoadContent() { // Create a new SpriteBatch, which can be used to draw textures. _spriteBatch = new SpriteBatch(GraphicsDevice);
// Load the object textures into the textures dictionary Textures.Add("Ball", this.Content.Load<Texture2D>("Ball")); Textures.Add("Box", this.Content.Load<Texture2D>("Box"));
// Load fonts Fonts.Add("Kootenay", this.Content.Load<SpriteFont>("Kootenay"));
// Reset the game ResetGame(); }
|
The final line of code in LoadContent calls into a new function named ResetGame, in which we create the objects that will do the work when the game is running; its code is shown in Listing 8.
Example 8. Resetting the game
private void ResetGame() { TextObject message;
// Remove any existing objects GameObjects.Clear();
// Add 10 boxes and 10 calls for (int i = 0; i < 10; i++) { GameObjects.Add(new BoxObject(this, Textures["Box"])); GameObjects.Add(new BallObject(this, Textures["Ball"])); }
// Add some text message = new TextObject(this, Fonts["Kootenay"], new Vector2(240, 400), "Windows Phone 7 Game Development", TextObject.TextAlignment.Center, TextObject.TextAlignment.Center); message.SpriteColor = Color.DarkBlue; message.Scale = new Vector2(1.0f, 1.5f); GameObjects.Add(message); }
|
ResetGame first removes any objects that
might have been added to the game host earlier on. Although this will
never happen in our example because the game is reset only once, in a
proper game this could be called each time the player runs out of lives
in order to prepare a new game. To stop the previous game from leaving
debris behind, we would need to remove those objects that the previous
game had been using.
The game then loops to add ten boxes and ten balls
to the game host. These are created using the derived game object
classes that we have been discussing over the last few pages. It also
adds a TextObject instance to display the writing in the middle of the screen. No custom functionality is needed for this object, so the TextObject class is instantiated directly.
All that is left is to implement the Update and Draw functions, both of which can be seen in Listing 9. As you can see, these are absolutely trivial, just telling the game host to perform the required updates and drawing.
Example 9. Updating and drawing the game
protected override void Update(GameTime gameTime) { // Allows the game to exit if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) this.Exit();
// Update all the game objects UpdateAll(gameTime);
base.Update(gameTime); }
protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.Wheat);
// Begin the spritebatch _spriteBatch.Begin(); // Draw the sprites DrawSprites(gameTime, _spriteBatch); // Draw the text DrawText(gameTime, _spriteBatch); // End the spritebatch _spriteBatch.End();
base.Draw(gameTime); }
|
The small amount of code presented in this section
is all that is needed to get the game example running. Please spend a
little time getting comfortable with the general approach used here:
the framework classes, the derived game object classes, and the code
within the game class.
The framework is very lightweight in nature,
essentially just allowing us to easily organize and access the objects
that we want to work with. It is deliberately unobtrusive and
transparent so that it doesn't get in the way of your game code,
allowing you instead to simply focus on the game rather than the
mechanics of managing your objects.
Because of the code present in the GameHost.UpdateAll
function for transferring the game object collection into an array for
processing , game objects can also add objects to the game during their Update
calls. If, for example, a sprite detects that it has landed in water
and wants to create a splash, it can add objects for the water droplets
at the point at which its movement into the water is being processed,
which makes for a much more readable project overall.
5. Removing Objects from the Game Host
So we can add objects to the game, but how do we remove them?
The game (in its Update method) has full access to the GameObjects collection within GameHost. If it determines that an object is no longer needed, it can simply remove the object from the list.
Just as game objects can add further objects in their Update
method, so they can remove objects too. If an object determines that it
is no longer needed, it can simply remove itself from the collection
and it will receive no further calls to Update or Draw. This includes the ability for an object to remove itself if it needs to.