After installing the Visual Studio 2010 and Windows Phone Developer Tools, I am sure you cannot wait to begin your first Windows Phone XNA application. In this section, you will start the charming journey, learn about the basic and important code of XNA, and how this code works. It is easy to get started.
1. Once the XNA Game Studio 4.0 is successfully installed, click the top-left menu item File | New | Project. The XNA and Windows Phone Game Project Template will show up in the New Project window, as shown in the following screenshot:
2. In the New Project pop-up window, select Visual C# from the left-hand side pane, and then choose the Window Phone Game project template.
3. Next, give a Name, Location, and Solution name to the project. Solution name will be the same as your project name by default.
4. Finally, click OK to let Visual Studio 2010 automatically create the Windows Phone Game project for you.
5. The generated Windows Phone Game project WindowPhoneGame1 automatically has the main game functionalities; the other generated associate project WindowsPhoneGame1Content is responsible for the game content:
The basic methods in Game1
class residing in Game1.cs
of your first Windows Phone Game project WindowsPhoneGame1 are very useful. They are clear and easy to understand according to the method name and comments. This template is your first gift, and you will find new ways here to speed up your development. The complete game skeleton presents several significant methods:
Game1()
constructor method: The constructor, called before theInitialize
method, is a typical one found in any class and, therefore, has the same meaning used to set default values to required elements. For example, instantiating the graphics device manager, define the game frame rate, and so on.Initialize()
method: Sets default and preliminary values to your game, queries and initializes user-based information, such as starting positions and frame updating rate.LoadContent()
method: Loading all game content may include images, sprite sheet, sounds, models, and so on. In XNA game development, all artwork loading should be done in theLoadContent()
method before theUpdate()
andDraw()
methods.UnloadContent()
method: Unloads all game content and content managers, a controller of all contents with loading and unloading when the objects used in your game need specific disposing or unloading.Update()
method: This method is very important when your game is running. It performs ongoing game logic such as calculating current positions, physics, collisions, and states; collecting the input information from various input devices; updating animations. Note that at this stage you only decide upon the current frame to display. Drawing is not performed because all drawing should be done by theDraw()
method. It updates the camera, refers to the update animation note, plays audios, and so on. TheUpdate()
method updates the game logic, which will make your game more fun depending on the interaction with the game data, such as player life, experience value, and score.Draw()
method: As the method name implies, in this method your work is to render all the graphics, including 2D and 3D views, onto the screen to make the game data visible, so that players can experience the real game world.
For the recipes in the following chapters, we will be revising this code many times from different perspectives. When you build the project and run it, you will see an emulator window with a solid blue background by default. Your first XNA Windows Phone Game is done, although it is just a blank screen as shown in the following screenshot. Isn't it easy?