次の方法で共有


SL4 XNA Platformer Level Editor for WP7: the XNA game (4/4)

This will be the last article of this series of 4. In the previous article, we have seen how we’ve handled the Windows Azure Storage from the Silverlight 4 application. We will now see the modifications done to the XNA Platformer game for Windows Phone 7. As I’ve already shown you how to build it for Windows Phone 7 in this previous article (updated for the April CTP here), this article will be much less longer than the 2 previous one. You will be able to download the source code of the XNA application built for the Beta version of the Windows Phone 7 tools at the end of this article.

Adding menus to the game

The first step was to add menus to let the player loading the levels from the cloud and launching the game:

XNASLEditor001XNASLEditor002

I’ve also added a menu displayed when you hit the back button during the game:

XNASLEditor003

In order to handle the menus, I’ve simply add to the Platformer source code I’ve shared with you in the previous article some logic found in the GameStateManagement sample: https://creators.xna.com/en-us/samples/gamestatemanagement . I’ve then slightly updated the sample for the Windows Phone 7 device and add a simple animation on the current selected menu element. For that, I’ve changed the scale computation used by default in the MenyEntry.cs file:

 float scale = 2.0f;

By this one:

 // Pulsate the size of the selected menu entry.
double time = gameTime.TotalGameTime.TotalSeconds;
float pulsate = (float)Math.Sin(time * 6) + 1;
float scale = 2 + pulsate * 0.05f * selectionFade;

You can see the final result thanks to the video available in the first article of this series.

Accessing to the Windows Azure Storage

Nothing as simple as that! We just need to use the Silverlight framework inside the XNA game to have access to classical object like the WebClient, LINQ, etc. I’ve simply copy/paste the code I’ve shown you in the previous article to list the available levels through an HTTP REST request, to build the LINQ to XML mapping and finally to load the levels. The only difference with the Silverlight 4 application is that I’m saving the loaded levels inside the Isolated Storage area dedicated to my Platformer game:

 private void SaveThisLevelToDisk(Stream stream, string fileName)
{
    IsolatedStorageFile isof = IsolatedStorageFile.GetUserStoreForApplication();

    IsolatedStorageFileStream isfs = new IsolatedStorageFileStream(fileName, FileMode.Create, isof);
    Int64 imgLen = (Int64)stream.Length;
    byte[] b = new byte[imgLen];
    stream.Read(b, 0, b.Length);
    isfs.Write(b, 0, b.Length);
    isfs.Flush();

    isfs.Close();
    isof.Dispose();
}

I’ve finally slightly modified the LoadNextLevel() and LoadTiles() methods in order to use the Isolated  Storage to load the levels files downloaded from Azure. The rest of the code is then working as-is.

XNASLEditor004

Well, you’ll get it. The XNA part of this demo was clearly the simplest to handle. We could imagine enhancing this demo by adding some tiles notifications on the main screen of the phone when new levels are available for instance. But you now have the source code: you should let your skills & imagination working on! :-)

Download the Source Code

Here is the Visual Studio 2010 solution compatible with the Beta version of the Windows Phone 7 tools:

Note: to be able to play with the host keyboard inside the emulator, you first need to touch the screen with your mouse and then press the “Pause” key. Then, you’ll be able to move the player with the left/right arrow and make it jump with the spacebar.

I’ve then accomplished the objective I had before going on vacation. Have some nice holidays and feel free to imagine and develop some wonderful Windows Phone 7 applications during this free time!

David

Comments

  • Anonymous
    September 28, 2012
    Excellent Demo