Intermission 9–Back to the future (Phone 7)

Marty you gotta see this…. (whoops, back to the tutorial)

Hope you are not getting to dizzy now going back and forth from XNA GS 3.1 and GS 4.0 and the windows phone.  But lets bring the phone project up to date with current efforts and also wrangle out some gotcha’s that have come to light.

Source updated for Final combined update project for GS 4.0 project here on Codeplex (Windows and WP7)


 

Game save settings

Granted we don’t have many settings to actually maintain on the phone at present, but we could have (like resolution, game speed, difficulty and such) and in the interest of at least attempting to make the GS 4.0 code ready for running everything we’ll add it in.

So first off copy the “FileManager.cs” and “KeyMappings.cs”, then update their namespace to match the rest of the project, “XNAStarTrooper2D_Phone7”.

Now it would be nice if that was it but first we have some alterations to handle, first up.  The windows phone, unlike XBOX and windows doesn’t use storage containers, instead it uses something called Isolated Storage.  Pretty much the same thing just a different name.  Second it has a slightly different way of creating files, so we’ll deal with that as well.  What it boils down to is that pretty much all of our code in the File Manager is useless.  So why copy the File Manager class in at all, well that is to try and keep the project unified.

So first we need to add compiler pre-directives around our existing code first, so add:

Just after the Class definition and add the following just after the SaveKeyMappings function:

If done right, you should see all the code greyed out, this pretty much leaves us with a blank class definition for windows phone while still retaining the functionality for other platforms.

But we still see several red wavy lines under the xml serialisation functions.  This is because the protection around DLL’s have changes (you will see this a lot) and the serialisation implementation has been updated slightly.  This is not a biggie, just right click “References” in the solution explorer and select add reference, then select “System.XML.Serialization” and click ok.  Finally add a using reference in the top of the class:

So we have added two new overloads which are Windows Phone specific thanks to the Pre-compiler statements, this means we don’t need to change the rest of the game code for saving and loading, although in hindsight, the name of the save and load functions could do with renaming now Open-mouthed smile (something for later when the projects unify).

The two new functions use the phone specific way of saving and loading data, which is very similar to the XBOX version:

    Instead of Storage Container we use Isolated Storage, the major advantage of this is that we don’t need to use the guide to get the user to choose a container, it’s all built in.
    How we access files is slightly different, we still use streams but as with the isolated storage, it’s a lot simpler and easier to use.
    No change in the serialisation, except that we now need to manually add a reference to the Serialisation DLL

To finish it off we need to add another pre processor directive to the using section of the class around the Storage statements and also add a reference for isolated storage, so replace:

With:

Last thing to update is in the KeyMappings class.  Just remove the [Serializable] references (apparently they are only needed for binary serialisation).  You can also remove all using statements except Microsoft.Xna.Framework.Input (this is a good general rule of thumb, to only declare what you actually intend to use in your code, same can be said of references).  I’ll tidy up more later.

To update the main game to load and save settings, simply add the following in to the LoadResources function of StarTrooperGame.cs:

And so that our settings are saved when the game finishes, change the exit code at the start of the Update section to:

This adds a call to Save Settings before we exit.

 


 

Particle Engine

The changes to the particle manager are also fairly cosmetic and mainly involve replacing:

To:

Then:

    Updating the namespace of all the classes
    Updating the type SpriteBlendMode in the ParticleEmitter class from SpriteBlendMode to BlendState (standard XNA 4.0 change) and the settings in each emitter to the BlendState equivalent.
    Updating the SpriteBatch.begin call in the particle manager draw call to “spriteBatch.Begin(SpriteSortMode.Texture, pe.SpriteBlendMode)

So copy in the “ParticleManager.cs”, “ParticleEmitter.cs” and “ParticleEmitters.cs” classes from the 3.1 project and make the above changes.

Then like before, we just need to add the particle engine references in the game itself like in the last post, these comprise of:

Particle Manager attribute in the start of the StarTrooperGame class:

Ant the particle manager initialisation in the StarTrooperGame constructor:

Then we need to add the new Fireball effect in the StarTrooperSprites.cs file, just after the TrooperFire function in the Trooper class:

And add the call to the TrooperFire function:

Then to finish it off we need to expose the helper features in the StarTrooperGame class, so update the Random attribute to:

 

 

The RandomBetween function towards the end of the class (after Load Resources):

 

 

And lastly the property to expose our random generator at the very end:

 

 

 


 

Game Updates

In the last few posts I also made a few basic program updates to Sprites, so we’ll update those here as well.

First off was to move the origin property to the sprite class from the animation class, this fixes the centre point of the texture on the screen for drawing sprites, so add the attribute to the bottom of the Sprite class, like so:

Then add the property to expose it, at the end of the properties section:

Then to ensure the attribute is properly populated when the Sprite is constructed, update the constructors as follows:

Adding the Origin references as appropriate.

Next we update the background creation logic to override the constructor and set the background Origin to Zero, makes it easier to draw background textures absolute:

That should do it but I also made one other change here that will help us later, I’ve set the velocity of the main background so that the sprite update loop will move the background instead of just incrementing it by 1 all the time in the StarTrooperBackground class.  This enables us to change the speed of the background later.

But by setting this property we also need to update the StarTrooperBackground classes update method, else it will always move twice as fast, so update this code to:

So all this update function does now is to shift the background up to the top once it has passed beyond view at the bottom of the screen.  However if the background was moving up this wouldn’t work.

Feel free to experiment with this if you want, to use a different velocity in a different direction.

To finish up you still need to remove all the references to m_Origin from the Animation.CS class m like the attribute at the end of the class, and the copy reference in the Clone constructor.

Basically search for all reference in the Animation class and remove them, the only exception is the Draw call which needs to be updated to:

Changing the Origin property to use the new Sprite Origin property.

 


 

Gotcha!!!

Now a good friend of mine sent me a little gotcha for Window Phone 7 projects, this refers to using Keyboard state.  if you use it the performance of your game will suck big time.  Turns out he was dead right.

After commenting out keyboard references, a certain amount of LAG (which I put down to the emulator on my laptop) disappeared, and the get felt a lot smoother. 

Since input seems to be one of those things that is ever changing in the CTP, I’ve just removed all references in the Input.CS class for Keyboard, so removing things like:

    Any Keyboard state attributes or updates
    The private functions for testing keys
    shortened the Player controls to remove the IsPressed function (just removed)
    Removed the TrooperFired function alltogether since we are currently using the touchscreen to fire   

Nice catch Charles!! (Randomchaos / Nemo Krad) Humphrey


 

Conclusion

 

That’s a lot of updates for one post, so I’m moving the rest of what I was going to cover (particle experiment and an accelerometer simulator) in the next intermission (last one in this section I promise Winking smile)

Now put down the keyboard and step away please sir, nobody needs to get hurt.

%d bloggers like this: