IT tutorials
 
Technology
 

Windows Phone 7 : Using the Game Framework (part 2) - Adding Game Objects to the Game Host

9/14/2013 1:52:49 AM
- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019

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.

 
Others
 
- Windows Phone 7 : Using the Game Framework (part 1) - Creating Derived SpriteObject Classes
- SharePoint 2010 : Planning Your Search Deployment - Environment Planning and Metrics (part 3) - Typical Server Configurations
- SharePoint 2010 : Planning Your Search Deployment - Environment Planning and Metrics (part 2) - Initial Deployment Guidelines
- SharePoint 2010 : Planning Your Search Deployment - Environment Planning and Metrics (part 1)
- SharePoint 2010 Components : Web Server Role, Query Server Role, Database Server Role
- Windows 8 : Troubleshooting Common Disk Problems (part 3) - Defragmenting Disks
- Windows 8 : Troubleshooting Common Disk Problems (part 2) - Checking for Disk Errors
- Windows 8 : Troubleshooting Common Disk Problems (part 1) - Repairing Disk Errors and Inconsistencies
- Windows Small Business Server 2011 : Working with Shares (part 3) - Managing Shares
- Windows Small Business Server 2011 : Working with Shares (part 2) - Creating a New Share Using Windows Explorer
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
Technology FAQ
- Is possible to just to use a wireless router to extend wireless access to wireless access points?
- Ruby - Insert Struct to MySql
- how to find my Symantec pcAnywhere serial number
- About direct X / Open GL issue
- How to determine eclipse version?
- What SAN cert Exchange 2010 for UM, OA?
- How do I populate a SQL Express table from Excel file?
- code for express check out with Paypal.
- Problem with Templated User Control
- ShellExecute SW_HIDE
programming4us programming4us