Common mistakes when using XNA

Just wanted to spend a few minutes on some regular questions I get.
 
1: SpriteBatch and SaveState
First of all, the SaveState on the sprite batch.
When you are using the Begin() method of the SpriteBatch class, it will set some of the render/device states to different values. Once you call the End() method of the SpriteBatch object, the render states will not, by default, be restored. This can result in some wierd artifacts that makes your 3D models( if you render 3D and 2D ) look wierd, especially considering the UV-coordinates.
This can lead to a lot of frustration, but luckily, the solution to this is pretty simple:
 

spriteBatch.Begin(

SpriteBlendMode.AlphaBlend, SpriteSortMode.Deferred, SaveStateMode.SaveState);
spriteBatch.Draw(texture,
new Rectangle(0, 0, 200, 200), Color.White);
spriteBatch.End();

Here we use the SaveStateMode.SaveState to sve the state of our device. Once we call End(), the state that was saved in Begin will be restored!
 
 
2: AudioEngine, playing music is randomly crashing!??
 
Just got one comment on this one. Did you remember to update your AudioEngine instance? Say your instance is named audio, this problem can most of the time be solved by just calling this function from your Update loop:
 
audio.Update();
 
 
 
I will update this post with more soon!
Advertisements
This entry was posted in XNA. Bookmark the permalink.

One Response to Common mistakes when using XNA

  1. Laura Marie Cruz says:

    Cool, maybe you can make your own FAQ thing? Probably loads of those.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s