Saturday, 18 August 2012

Running Windows phone 7 (mango) emulator in an emulator

I'm using a VMware emulator for Windows 7 on my Mac book Pro. Soon to be dumped for a Native Windows 7 machine. Now the WP7 emulator is a VM also and generally VM into VM does not go. SL will deploy to the WP7 emulator unless you are using anything to do with WDDM 1.1 (which is not supported by the ghost graphics Direct X 9 card emulated by VMware) which discounts all XNA deployment. I recently reduced the amount of memory allocated to the VM as I was getting warnings from VMware for something. This caused a problem in VS 2010. When I went to deploy to the WP7 emulator I got the following error "visual studio error the interface is unknown" which as usual means something totally different! It means that there is  not enough memory to support the WP7 emulator. I read around and it can happen on a native (non-VM) machine also. I have come to this conclusion VMs are not a solution for Windows 7 production machine on a MAC. Hence the MAC has to go!

Friday, 18 May 2012

Level Manager revisited–residual objects from deleted levels


In a previous post I went through a level manager. This post picks up were that one left off.

If you create objects in the sub class levels, then when you set the level to null, thus not activating it next time around the Array. Then the objects contained within the Level 1 for example will still exist. For this reason we could implement Idisposable and a destructor to cleanup after the Level 1 object is deleted (see here) but that is beyond the scope of what we know here. I will cover this technique at a later date. Another more simple is reported here.

say we play Sounds and a backing track within a level Level 1. Thus in the constructor assuming the appropriate Sound and Song variables are declared

sound = g.Content.Load<SoundEffect>("Tank-Moving");
backtrack = g.Content.Load<Song>("Sleep Away");
soundInst = sound.CreateInstance();
soundInst.IsLooped = true;
MediaPlayer.IsRepeating = true;

we must cleanup after ourselves before the level is set to null. Otherwise the objects using the external resources (media player) may not be disposed of as they are still active.

public void cleanup()
if (MediaPlayer.State == MediaState.Playing)
player = null;


It would be better put the cleanup routine in the Level class along with all the possible content objects and actually the player object should be there also. But as some objects are declared in the subclass the level manager must call the cleanup method of each Sub Class Level as when you delete a sub level the objects may not be decommissioned by the .NET garbage collector until they are no longer needed. The media player will continue to play as it is repeating and will definitely not be released.

The Level manager must be able to distinguish the type of the Level to call methods appropriate to that Level. So when we are iterating through the Level objects we check what type the level object is and call it’s appropriate clean up method. This method can be used to specialize actions (method calls) for levels also. So in the update for the level manager (look for the if (l.LevelState == LEVELSTATE.FINISHED)) statement when we are checking to see if the Level is finished we call the cleanup method thus

// if the current level has finished
if (l.LevelState == LEVELSTATE.FINISHED)
{ // Get rid of the level
if (l.GetType() == typeof(Level1))
Level1 current = (Level1)l;

Levels[CurrentLevel] = null;

What we do here is we check to see what type of level object we are dealing with. You can do this with any object using the GetType() function to return the Object type (you cannot use a switch statement with though). Then we cast the Level object as a Level1 object so we can call the cleanup routine for the appropriate level. You would need an if block for every level object that requires cleaning up.

Saturday, 5 May 2012

A conveyor belt of Spawning a horizontal Sprite

A Horizontal spawning Sprite is similar to a patrolling platform sprite in it’s behavior and also similar to a falling sprite. But simpler.

A simple algorithm for a collection of Spawning sprites is as follows.

  1. Set up a timer to control when to Spawn the next sprite. The class that creates the Spawning sprite collection (Game1 here) controls this timer.
  2. The Spawning Sprite object has a simple state that is either Spawned or not. So that is Boolean.
  3. An Un-spawned sprite will not be updated or drawn.
  4. The objects are spawned off screen to the left and have a target off screen to the right.
  5. While they are spawned they just move to the target position at a certain speed.
  6. When they get to the target position Spawned is set to false and they return to the starting position waiting to be Spawned again when the timer goes off.

So in a new Game Class we create an array to hold our Spawning Objects. Our Base classes are as we previously used



The Spawning enemy class variables and constructor is as follows

class Spawn : Enemy
Vector2 EndPosition;
public bool Spawned;
public Spawn(Game g, Texture2D texture,
Vector2 Position1, Vector2 Position2,
int framecount) : base(g,texture,Position1,framecount)
// start, Target and End positions are defined in Enemy
// as most if not all enemies will have them
startPosition = Position1;
TargetPosition = EndPosition = Position2;
Spawned = false;

public override void Update(GameTime gt)
if (Spawned)
// Move X * Velocity as we are moving Horizontally
position += new Vector2(1, 0) * Velocity;
if (Vector2.Distance(position, EndPosition) < Velocity)
position = startPosition;
TargetPosition = EndPosition;
Spawned = false;


When a Spawn is created it is initially false and it is activated by the timer in the Game1 class. The update of the Spawn just moves it horizontally. You could do the same vertically. When it reaches the target. It is reset to the start position Spawned is set to false this will cause it to be activated when the timer next goes off in Game1.

Now to Game1

The class variables

Arrays are used here as other collections have not been covered in First year programming

const float MAXSPWAN = 1000;
float SpawnTime = MAXSPWAN; // milliseconds
Spawn[] cokeBottles;

protected override void Initialize()
// TODO: Add your initialization logic here
cokeBottles = new Spawn[5];

We fill the array in the Load Content method. The start Vector is off to the left of the viewport. The End and target position are off the right of the viewport

for(int i = 0; i < 5;i++)
cokeBottles[i] = new Spawn(this,
new Vector2(-30, 300),
new Vector2(GraphicsDevice.Viewport.Width + 100, 300),1);

So now we have an array of 5 Spawn objects. The Update of Game1 controls the Spawning of a sprite based on the Timer.

if (SpawnTime > 0)
//Count down the spawn time
SpawnTime -= gameTime.ElapsedGameTime.Milliseconds;
// reset the spawn time
SpawnTime = MAXSPWAN;
// if any of the Spawn reach the target
// which is off to the right of the viewport
// then the Spawn will set it's
// Spawned property to False
foreach (Spawn s in cokeBottles)
if (!s.Spawned)
s.Spawned = true;
break; // This is Key only one spawned at a time
// Normal Update Spawned or not
foreach (Spawn s in cokeBottles)

The Draw for Game1 just draws the sprite collection

foreach (Spawn s in cokeBottles)

and that’s it

Monday, 30 April 2012

A simple State based level manager in XNA

This post is to explain a simple level manager that will handle the transition between levels of a simple sprite based game based on simple state management of a sequence of levels. It assumes linear progression from one level to the next

A level consists of a set of items (Sprite based items in this case) that you want to interact with or get them to interact independently, during game play. The key to level management is to have a parent class for each level that you want in your game and that will contain references to collections that you may have in each level of your game.

The level manager manages the transition between the levels based on the outcome of a particular level. Failure and re-doing a level is not dealt with here but could be easily accommodated by extending the state model for the Level Class and coding the Level Manager accordingly.

To keep it simple here and concentrate on the Level class and the Level Manager class I will create two levels of a game that will just transit through states based on a Timer. These two classes will be level1 and level2 respectively. I will also only have static (non-animated and non interactive) content in the Level class which can then be filled in the two sub classes (level1 and level2).

The level Manager is created, updated and drawn in the Game1 class. The Draw is implemented just to call the Draw methods of the level1 and level2 classes which in turn pass the draw message on to the components that they contain.

All the classes here only implement or override  a common Update or Draw method, which are most fundamental in Game programming.

So the Game creates the Game Manager.


The Level manager creates the Game Levels.


The Levels create instances of the level contents Sprites, sound, textures.

There are a couple of Key issues about the structure and control of the levels.

  1. As Level1 and Level2 inherit from Level they can be treated as being the general Level class in some instances. Such as iteration over the collection of Level Objects.
  2. The Sate of a level is kept in the parent class Level and is chosen from the enumerated type LEVELSTATE



So to the actual State Management and transition of the individual levels. The Level Manager maintains a collection of levels that are created and assigned in the constructor.

class LevelManager
// Indicate the last level is complete
bool gameOver = false;

public bool GameOver
get { return gameOver; }
set { gameOver = value; }
// Counter for the current level
int CurrentLevel = 0;
// Maximum amount of levels

const int MAXLEVEL = 2;

// Collection of levels which are created as subclasses of Level
Level[] Levels;

public LevelManager(Game g)
Levels = new Level[MAXLEVEL];
Levels[0] = new Level1(g);
Levels[1] = new Level2(g);

Levels[0].LevelState = LEVELSTATE.PLAYING;


Level1 is put into playing mode. It is up to Level1 to transit itself into Finished Mode when it’s end condition is met. In this case a timer is used to time down the fairly static levels. The levels here just show a collection of Chasing Sprites in a location and a player in Level1 that the Chasing sprites will chase.

The transition from playing for a Level is monitored in the Update method of the Level Manager class


public void Update(GameTime t)
if (!gameOver)
foreach (Level l in Levels)
if (l != null && l.LevelState == LEVELSTATE.PLAYING)
{ // Update the current playing level
// if the current level has finished
if (l.LevelState == LEVELSTATE.FINISHED)
{ // Get rid of the level should
Levels[CurrentLevel] = null;
// and if the not the last level finished
if (++CurrentLevel < MAXLEVEL)
// then play the next level
Levels[CurrentLevel].LevelState = LEVELSTATE.PLAYING;
//Or else we are finished
else gameOver = true;


The key thing here is that although level1 and level2 are objects in their own right they have a LevelState variable that can be monitored. The Level class definition is shown below

class Level

private LEVELSTATE levelState;
public LEVELSTATE LevelState
get { return levelState; }
set { levelState = value; }
protected ChasingEnemy[] enemies;
protected Sprite[] collectables;
protected Sprite BackGround;
protected Player player;

public virtual void Update(GameTime t)


public virtual void Draw(SpriteBatch sp)


The virtual methods are provided in case the methods need to be overridden in the future. Any collections of object that you want to have in levels are added as class variables here and can be populated in the constructor for the sub class for example here is the constructor for the Level1 class

class Level1 : Level
float Timer = 5000; // milliseconds

public Level1(Game g)
player = new Player(g, g.Content.Load<Texture2D>("FullTank"), new Vector2(400, 600), 1);
enemies = new ChasingEnemy[3];
enemies[0] = new ChasingEnemy(g, g.Content.Load<Texture2D>("EnemyDot"), new Vector2(100, 200), 1);
enemies[1] = new ChasingEnemy(g, g.Content.Load<Texture2D>("EnemyDot"), new Vector2(300, 200), 1);
enemies[2] = new ChasingEnemy(g, g.Content.Load<Texture2D>("EnemyDot"), new Vector2(300, 400), 1);

NOTE the sub-classing of the Level class is Key as it contains the exposed LevelState and allows all levels to iterated over using for each and treating them all as Level objects for the purposes of this iteration

Here is the state transition in the Level1 update method

public override void Update(GameTime t)
if (Timer > 0)
Timer -= t.ElapsedGameTime.Milliseconds;


foreach (ChasingEnemy c in enemies)

foreach (ChasingEnemy c in enemies)


the full code listing can be found here

Wednesday, 21 March 2012

Allowing for One Time collisions of sprites

This post is specifically for 1st Game Design students in IT Sligo 2012. We have two animated sprites in a game. One is an enemy moving around randomly by itself – the randomEnemy, one is controlled by a player – the player. Both have common attributes and methods (they are both animated and Drawn at their position) hence we have a sprite class to handle this. This is all placed in the Sprite class (sometimes called the base class). For convenience as we have other types of Enemies to be used later that share attributes we have an Enemy class in between RandomEnemy and the Sprite class.

Now the Sprite, Enemy, Player, randomEnemy classes are pre-written and can be found here.

In order to use them in a new game don’t forget to use the name space which in which all these objects reside

using AnimatedSprite;

Now I’m using the default Game1 class here for speed. But the example that the current assignment is based on uses a chase engine class that I have written. All actions happen in the Update of the Game and would happen in the Update of the Chase Engine.

Here are the classes involved in this post.


So onto the problem to be solved

The sprite class has a method called CollisionDetect which will check if one sprite collides with another.

public bool collisionDetect(Sprite otherSprite)
Rectangle myBound = new Rectangle((int)this.position.X,
(int)this.position.Y, this.spriteWidth, this.spriteHeight);
Rectangle otherBound = new Rectangle((int)otherSprite.position.X,
(int)otherSprite.position.Y, otherSprite.spriteWidth, this.spriteHeight);
if (myBound.Intersects(otherBound))
return true;
return false;


As it’s in the Sprite Class the Player and all the subclasses inherit it. So when the player object collides with the Random Enemy object we want to update a score for the player. The problem is that the score keeps increasing during the collision event which will happen 60 times a second while colliding. This gives unpredictable scoring. So we need 2  Boolean variables to make sure we only update the score one collision at a time.

So to the Variables to represent the score, the player, the random enemy, a Boolean variable to record if there is currently a collision and if there was a collision on the previous call to the update

int score = 0;
Player player;
RandomEnemy randomEnemy;
bool colliding = false;
SpriteFont scoreFont;

The player,enemy and font to draw the score can then be created in the Load Content method.

player = new Player(this, 
Content.Load<Texture2D>("PlayerDot"), new Vector2(10, 10), 2);
randomEnemy = new RandomEnemy(this,
Content.Load<Texture2D>("EnemyDot"), new Vector2(200, 200), 1);
scoreFont = Content.Load<SpriteFont>("scoreFont");

and so to the Update logic

protected override void Update(GameTime gameTime)
if (player.collisionDetect(randomEnemy) && !colliding)
score += 10;
colliding = true;
else if(!player.collisionDetect(randomEnemy))
colliding = false;


colliding protects the score from being updated many times in the one collision. colliding is false to begin so !colliding will be true and so if the player successfully detects a collision then we update the score but we set the colliding value to true. This makes !colliding false on the next Update call and hence it will not update the score.

colliding is set to false if the player is not colliding which will happen quite a bit actually.

Then it just remains to draw the score and update the new positions of the player and the Random Enemy


protected override void Draw(GameTime gameTime)

spriteBatch.DrawString(scoreFont, "Score: " + score.ToString(),
new Vector2(10, 10), Color.White);


and that’s it. The same would apply for a decrease in the Lives of a player and you would need an additional different Boolean variable to protect the lives variable. Until next time.

The final coded solution is here. Any problems give me a shout.

Thursday, 5 January 2012

First View of Android app structure behind the Scenes

Well I thought XAML and c# could be complicated, but the pattern that holds the Android UI together is no simple walk through the park either. And having not looked at Java in years, I presume it is similar for other Google Apps? Cryptic is not the word. The basis for the elements to be rendered is fair enough. It's XML and I like the render tool that is built into the the eclipse plugin for Android. But the model for linking it to the Java code behind the form is not intuitive and requires you to burrow down through resources to see how it all comes together and all this from a resource reference in the default Activity stub called R??

We go from this

public class Project1Activity extends Activity {
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {

which is fair enough to this which is the equivalent of .g.s.cs hidden code generated file in VS

package com.appsolo.adnroid;

public final class R {
public static final class attr {
public static final class drawable {
public static final int ic_launcher=0x7f020000;
public static final class layout {
public static final int main=0x7f030000;
public static final class string {
public static final int app_name=0x7f040001;
public static final int hello=0x7f040000;

which is not

and then we are down to the resources which I know XAML can rely heavily on as well

here is the XML for the form which is rendered in the tool

xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android=""



android:orientation="vertical" >




android:text="@string/hello" />


The XML is rendered nicely in the Graphical Tool of the Android plugin

Although there is a Android Resources values tool for keeping track of values

But there are a lot of Dots to join up here!!!

First Steps in Android

Ok. I Got the Android development kit installed on a Mac. Thats a plus for a start. It runs native, raw and wriggly on the Mac. The Mac book pro being a fine piece of hardware and the Apple software including the OS being lovely to use but awkward to work with and find your way around for Windows head like myself.

For the life of me I cannot fully remember the process as I never can when installing downloaded software, some you do yourself and some comes down as a result of what you do. Suffice to say the standard installation instructions are for installation on a Linux or Windows machine. I had to follow another few links to get the proper tools for OSX 10. Here is the gist of what was needed.

You need

The JDK 7 for OSX from Oracle. When you unbundle this and install it. It runs an may pull down additional updates for the Mac.

Download and install a suitable version of eclipse. I took the Mac version from HEANET in Ireland.

Then its over to the Android SDK download page for the the Android SDK and AVD Manager. Which when unzipped have to be added to the eclipse environment.

Eclipse is the SDE of choice. First impressions of eclipse are it's not bad for free, but shows all the signs of it. VS 2010 is miles ahead and I have always said MS should push this more. However when I installed as per instructions the Android SDK Manager plugin for eclipse it did have a download manager that fixed a lot of interdependencies that were missing.

I then had to set up a AVD Android Virtual Device and attach it to the currently open Workspace.

AVD manager

It did not register with eclipse and the Hello World App did not deploy to it until I shut down eclipse and started it up again. But following a reboot of eclipse (on intuition) up came the emulator. The AVD is very slow to load and I'm using a SDD and Macbook Pro with 8gigs of memory.

Next challenge was to figure out the structure of a Java project (as it has been a while since I have seen eclipse and how to load an existing sample Android app that comes with the Android SDK.

Cmd + n creates a new project. You then choose what type of project you want The basis for eclipse is a workspace that is a enclosing folder that holds the project folders. A project must have a Package name otherwise it will not create. Which is similar to a name space in c# but is represented as a directory structure of the project folder in the workspace. The workspace contains a collection of project folders.

Project setup window

Once the Android SDK plugin is installed you you have access to the sample projects which you can then base a new project on. Right click on the project window

Followed two screens later by your Android sample template

Well thats the development environment  set up and ready to go. Now on to look at the Android in more detail.