Click here to Skip to main content
Click here to Skip to main content

Usefel Tips For Windows Phone XNA Game Development

By , 14 Apr 2013
 

Introduction/Catalog

These two months I am developing XNA Game for Windows Phone 7. This was my first XNA programming, so I have a lot of headache problems, here I'll share with you.

  • Do Not Create A Lot Of SpriteBatch
  • Do Not Forget To Remove Events
  • Use Local ContentManager
  • Do Not Create A Lot Of SpriteBatch

At first, I set up a SpriteBatch object for each sprite in 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 don't seem to have any problems at first, but about 10 minutes the game will throw a exception of memory out of range.

So, then I only create one SpriteBatch object in the whole game, and add it as a service.

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

Then, the game did not appear errors when you create a SpriteBatch.

Do Not Forget To Remove Events

You need to explicitly remove event, 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 )
 {

  try
  {
   this.hardenEffect.Opened -= this.hardenOpened;
   this.hardenEffect.Closed -= this.hardenClosed;
  }
  catch { }

  base.Dispose ( disposing );
 }

 // ...

}

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

Use Local ContentManager

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

this.contentManager =
 new ContentManager ( this.World.Game.Services, contentDirectory );

// ...

this.contentManager.Load<texture2d> ( resource.Path )

// ...

this.contentManager.Unload ( );

License

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

About the Author

zoyobar
United States United States
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130513.1 | Last Updated 14 Apr 2013
Article Copyright 2013 by zoyobar
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid