New Features in XNA 4.0 and Windows Phone 7
Unless you’ve been living under a rock, you’ve probably heard about Windows Phone 7. What you may not know is that there are two ways of developing applications and games for this device. Silverlight and XNA are your two choices and in this article, I cover the Games and XNA side of the house.
If you are brand new to game development, XNA or mobile device development, don’t worry. This article will get you started and productive in no time.
Where Do I Get It?
The first step in getting started with XNA for Windows Phone development is to visit the developer site at http://developer.windowsphone.com and click the “Get the free tools” link.
Running the web installer puts a number of things on your system, including:
- Microsoft XNA Game Studio 4.0
- Microsoft XNA Game Studio 4.0 Windows Phone Extensions
- Microsoft XNA Framework Redistributable 4.0
- Windows Phone Emulator
- Windows Phone 7 Add-in for Visual Studio 2010
- Microsoft Windows Phone 7 Developer Resources
You also get Expression 4.0 and Silverlight 4.0, though those are not relevant to this article.
How Do I Use It?
If you’ve spent any time with previous versions of XNA up to this point, you’re in luck. There isn’t much difference between XNA 3.1 and XNA 4.0 other than the new stuff added for phone-specific projects.
Hopefully you grabbed the installer and ran it by now, so I’ll just jump right in to your first project.
If you didn’t already have Visual Studio 2010 installed, you do now. The web installer puts the Visual Studio 2010 Express Edition for Windows Phone on your system for you.
Once you have Visual Studio 2010 open (you can use ANY version of Visual Studio 2010 you like) go ahead and select XNA Game Studio 4.0 in the Installed Templates section and then select Windows Phone Game (4.0) as your project type, as seen in Figure 1.
Figure 1: The Create Project screen in Visual Studio 2010.
Go ahead and leave the name alone and create your project.
Once your project has been created, the first thing you will see is the Game1.cs class. This is the meat of your game and where all the interesting things happen.
Regardless of what platform you are developing for (Windows, Xbox or Windows Phone) you will use the same basic concepts.
XNA Games follow a very simple pattern, as seen in Figure 2.
Figure 2: XNA game sequence.
As you can see, there isn’t a lot to it.
The Initialize() and LoadContent() methods are only called once per game and are used to query for any required services, load up content, etc.
Once you are in your game loop, you have two very important methods: Update() and Draw().
The Update() method is used to query for any player input along with processing any “world logic” like collision detection, artificial intelligence, weather, etc. Playing audio is also handled in this method.
The Draw() method is used to draw all the onscreen game objects, using the positional or other data that was manipulated in the Update() method.
Each pass through the game loop is called a frame. XNA games on Windows and Xbox 360 target 60 frames per second (fps), and Windows Phone games run at 30 fps.
If you take a look at the last line of the Game1() constructor, you will see this line:TargetElapsedTime = TimeSpan.FromTicks(3333333);
This is what tells XNA to run your phone game at 30 frames per second (a tick is roughly 100 nanoseconds or one ten-millionth of a second.)
While you can adjust the value to make your game run slower, you can’t just tweak it up to make your game run faster, so let’s just leave it alone for now.
If you scan the rest of the Game1 class, you can see there isn’t really anything else different from the previous template.
Take It for a Spin…
At this point, go ahead and run your game by hitting F5. This will do a quick build and will also launch the Windows Phone Emulator. After a few seconds, you should be looking at a nice cornflower blue screen in the emulator.
It doesn’t do much, but that’s ok. Click on the Back Button on the emulator to dismiss the game. Don’t close the emulator though. You’ll want to keep it running.
What’s New in the Framework?
With Windows Phone 7 comes a lot of new functionality that traditional Windows developers (and even many existing XNA developers) haven’t been exposed to, such as dealing with device orientation, the accelerometer sensor and multi-touch gesture input.
I will cover each of those in the following samples.