Click here to Skip to main content
12,953,230 members (83,561 online)
Click here to Skip to main content
Add your own
alternative version


3 bookmarked
Posted 22 Mar 2012

Useful Tips for Windows Phone XNA Game Development

, 22 Mar 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
Some lessons learned during Windows Phone XNA game devleopment

The original post can be found here.


For the last two months, I have been developing XNA Games for Windows Phone 7. This was my first stint at XNA programming, so I faced a lot of problems, and here I'll share my experience with you.

Do Not Create A Lot Of SpriteBatch

At first, I set up a SpriteBatch object for each sprite in the game, such as:

internal abstract class Spirit : DrawableGameComponent, ISpirit
 // ...
 protected override void LoadContent ( )
  this.spiritBatch = new SpriteBatch ( this.scene.World.Game.GraphicsDevice );

  base.LoadContent ( );
 // ...

It doesn't seem to have any problems at first, but about 10 minutes, the game will throw an exception of memory out of range.

So, I create a SpriteBatch object for the whole game and add it as a service.

this.spiritBatch = new SpriteBatch ( this.Game.GraphicsDevice );
this.Game.Services.AddService ( typeof ( SpriteBatch ), this.spiritBatch );

The game did not show errors when you create just one SpriteBatch.

Do not Forget to Remove Events

You need to explicitly remove events, such as:

internal abstract class Player
 : Spirit, ILiving, IUnmatchable, IHardenable

 // ...

 protected Player ( IPlayScene scene, string movieName, string extendMovieName,
  Pad pad, float speed, HitArea hitArea, int width, int height, int maxLife,
  double hardenSecond, double unmatchSecond )
  : base ( scene, movieName, extendMovieName, speed, hitArea, width, height )

  // ...

  this.hardenEffect =
   new HardenEffect ( this, hardenSecond <= 0 ? 0.1 : hardenSecond );
  this.hardenEffect.Opened += this.hardenOpened;
  this.hardenEffect.Closed += this.hardenClosed;

  // ...

 protected override void Dispose ( bool disposing )
   this.hardenEffect.Opened -= this.hardenOpened;
   this.hardenEffect.Closed -= this.hardenClosed;
  catch { }

  base.Dispose ( disposing );

 // ...

The Dispose method of the Player class removes the events which are added in the constructor. If you just add an event, but do not remove it, also may cause a memory out of range error.

Use the Local ContentManager

Use the ContentManager, you can unload the resources that you no longer need, so you can save memory.

this.contentManager = new ContentManager ( this.World.Game.Services, contentDirectory );
// ...
this.contentManager.Load<texture2d> ( resource.Path )
// ...
this.contentManager.Unload ( );


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

United States United States
No Biography provided

You may also be interested in...


Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170525.1 | Last Updated 22 Mar 2012
Article Copyright 2012 by zoyobar
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid