Porting an XNA Windows Phone Game to Android

Introduction

This article uses JMA’s gaming software to demonstrate these features. To download the games, please click here.

This article uses a sample 2D shooter game with MonoGame on Android. Click here to download the sample project.

Prerequisites

You need to download Visual Studio 2010 Professional or higher. You need Mono for Android.

Set-up and Sample Game

Go to this site:

http://create.msdn.com/en-US/education/tutorial/2dgame/drawing_user_interface

Scroll to the bottom and download the Windows Phone project.

Open the solution.

Right click the solution > add > new project > Mono for Android > Mono for Android application

Solution

Here is my file structure:

image

Here is my solution:

image

Lib has two assemblies; Lidgren.Network.Android and MonoGame.Framework.Android. These are available with the download. You can also download the MonoGame source, compile MonoGame.Framework.Android and ThirdParty/Lingren.Network.Android.

In your ShooterMonoAndroidApplication, add a reference to the two DLLs in the Lib folder. In the add reference window, add OpenTK. Here is the final result:

image

Adding Code Files

Right click ShooterMonoAndroidApplication > add > existing item. Navigate to the Shooter project and add all the files (except program.cs) as links:

 image

Adding Images

In ShooterMonoAndroidApplication, create a folder under Assets called Content. Navigate to Shooter\bin\Windows Phone\Debug\Content. Add the XIB files are links. In ShooterMonoAndroidApplication, select all the XNB files. Make them Android Asset > Copy if newer:

image

Adding Music and Sound Effects

Make a folder under Content called Sounds. Music must be in MP3 format. SoundEffects should be in WAV. Add them as links. Here is the end result:

image

XML

The shooter project has no XML files, however, you other games might use them. You need to use the asset manager to find this data. Here is some sample code:

#if ANDROID
            XElement doc = XElement.Load(Game.Activity.Assets.Open("Content/Gameplay/LevelDefinitions.xml"));

            var query = from a in doc.Elements("level")
                        select new
                        {
                            EnemyCount = (int)a.Element("EnemyCount"),
                            BonusCount = (int)a.Element("BonusCount"),
                            LevelNumber = (int)a.Element("LevelNumber"),
                            LevelLength = (int)a.Element("LevelLength"),
                            LevelPurchaseable = (bool)a.Element("LevelPurchaseable")
                        };

Make them Android Assets, copy if newer as shown above.

Conditional Comments

My Android phone has a bigger screen than my Windows Phone. I need to make the background bigger on Android by adding this code:

#if WINDOWS_PHONE
            spriteBatch.Draw(mainBackground, Vector2.Zero, Color.White);
#endif

#if MONOGAME
            spriteBatch.Draw(mainBackground, Vector2.Zero, null, Color.White, 0, Vector2.Zero, 1.3f, SpriteEffects.None, 0);
#endif

Game File

Game1 is usually the name of your game class. If not, change Game1 in your Activity1.cs file:

using System;

using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using Microsoft.Xna.Framework;

namespace Shooter
{
    [Activity (Label = "Shooter", MainLauncher = true)]
    public class Activity1 : AndroidGameActivity
    {

        protected override void OnCreate (Bundle bundle)
        {
            base.OnCreate(bundle);
            Game1.Activity = this;
            var g = new Game1();
            SetContentView(g.Window);
            g.Run();
        }
    }
}
Deployment

Right click on the Shooter for XNA or the ShooterMonoAndroidApplication > set up as start project. Press F5 and that will start your emulator or testing device.

Posted: 7/30/2012 9:21:57 AM