Book Image

Windows Phone 7 XNA Cookbook

By : Zheng Yang
Book Image

Windows Phone 7 XNA Cookbook

By: Zheng Yang

Overview of this book

Developing games for Windows Phone 7, a new mobile platform, is your big chance to impact the world of mobile games. The XNA 4.0 for Windows Phone 7 integrates a lot of capabilities from software and hardware for you to create incredible games. The next generation of mobile games will be built by you. Windows Phone 7 XNA Cookbook is the best choice for you to make a game on Windows Phone 7. The book helps you to master the indispensable techniques to create your games using XNA 4.0. From the basics such as animating a 2D sprite and interacting with the customized graphical user interface to the more challenging such as 3D graphic rendering and collision detection. This comprehensive cookbook covers all the essential areas of XNA game development for Windows Phone 7, such as approaches to control the sensors, gestures and typical kinds of cameras. We also have recipes for sprite animation, texture rendering, and graphical user interface development that will give you a powerful tool to work with 2D effects. After this we move onto the more juicy stuff with recipes covering 3D graphic rendering and collision detection, and major ways to improve your loading efficiency. You will also work with Xbox live so you can take your game global. Finally, no mobile game development book would be complete without a look at performance optimization to make your games run faster. Windows Phone 7 XNA Cookbook will equip you with the firepower to rock the game world.
Table of Contents (17 chapters)
Windows Phone 7 XNA Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface

Using DrawableComponent and GameServices


In XNA game programming, a lot of built-in or customized objects exist in the game data container. When the quantity increases and it becomes onerous for you to manage, you will need a service to make your XNA programming life easier; this recipe will give you the necessary guidance.

Getting ready

In an XNA application, some parts or objects of your game need to be separately updated or drawn, such as radar, player, or monster. Within your XNA game, you need to create separate defined classes for the substances, then build an instance of one of them, initialize it, update it, and finally, render it to the screen within the Draw() method reserved for game loop calling. Hence, you need to define these kind of classes, which have their own Initialize(), Load/UnloadContent(), Update(), and Draw() methods, so that you can easily call them in the XNA game loop.

Again, it is better to inherit these classes from GameComponent, which promises your classes are added to the Component List, Game.Component a global objects manager in XNA application. It simplifies the management of the game shared objects to the delight of the XNA programmer

Furthermore, if you are sure your separated class has the rendering ability, DrawableGameComponent could be the best choice for you because the overridden Draw() method will be called automatically from DrawableGameComponent.

How to do it...

  1. 1. As an example, define a Radar class inherited from DrawableGameComponent:

    public class Radar : DrawableGameComponent
    {
    . . .
    Rectangle destRec;
    Rectangle playerPositionRec;
    Vector2 playerVector;
    List<Vector2> enemiesPosition;
    Texture2D texture;
    SpriteBatch spriteBatch;
    Texture2D texPlayerPosition;
    public Radar(Texture2D texture, Rectangle rectangle,
    SpriteBatch spriteBatch, Game game) : base(game)
    {
    // Initialize the radar member variables
    . . .
    }
    protected override void Draw(GameTime gameTime)
    {
    spriteBatch.Draw(texture, destRec, Color.White);
    spriteBatch.Draw(texPlayerPosition, playerVector,
    Color.Red);
    foreach (Vector2 vec in this.enemiesPosition)
    {
    spriteBatch.Draw(texPlayerPosition, vec,
    Color.Yellow);
    }
    base.Draw(gameTime);
    }
    protected override void Initialize()
    {
    // Initialize the device
    . . .
    base.Initialize();
    }
    protected override void LoadContent()
    {
    // Load radar and player texture content
    . . .
    base.LoadContent();
    }
    protected override void UnloadContent()
    {
    base.UnloadContent();
    }
    public override void Update(GameTime gameTime)
    {
    // Update the player position
    . . .
    base.Update(gameTime);
    }
    }
    
  2. 2. Now, that you have defined your GameComponent, the next step is to insert the Component to the GameComponent list of the Game class. Once added, the overridden method will be called automatically:

    public Game1()
    {
    . . .
    Components.Add(new Radar(this));
    }
    
  3. 3. Another way is to initiate the instance first and then pass it to the Add() method. The second approach provides the door. Sometimes, you might need to update some public variables of the component. For the Radar class, you can update the player's position:

    Radar radar;
    public Game1()
    {
    . . .
    Components.Add(radar);
    }
    
  4. 4. The Draw() method in the Game class is a very simple ascribe to the GameComponent list. The Draw() method of DrawableComponent belongs to the GameComponent list and will be called automatically. The code snippet is similar to the following code:

    protected override void Draw(GameTime gameTime)
    {
    GraphicsDevice.Clear(Color.CornflowerBlue);
    base.Draw(gameTime);
    }
    

How it works...

In step 1, the code presents a brief code snippet of the Radar class inherited from DrawableGameComponet. So far, your focus is on how to override the Initialize(), Update(), LoadContent(), UnloadContent(), and Draw() methods.