<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Nerd Herder</title><link>http://blogs.msdn.com/b/dejohn/</link><description>Dean Johnson blogs about life on the XNA platform and tools team</description><dc:language>en-US</dc:language><generator>Telligent Evolution Platform Developer Build (Build: 5.6.50428.7875)</generator><item><title>Moving to a new blog</title><link>http://blogs.msdn.com/b/dejohn/archive/2011/04/12/moving-to-a-new-blog.aspx</link><pubDate>Tue, 12 Apr 2011 05:24:27 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10152364</guid><dc:creator>Dean Johnson - MSFT</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/dejohn/rsscomments.aspx?WeblogPostID=10152364</wfw:commentRss><comments>http://blogs.msdn.com/b/dejohn/archive/2011/04/12/moving-to-a-new-blog.aspx#comments</comments><description>&lt;p&gt;I recently created a new personal blog. I will no longer be updating this blog. Please come join me over at my new blog at &lt;a href="http://www.deadlygenius.com"&gt;http://www.deadlygenius.com&lt;/a&gt;. &lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10152364" width="1" height="1"&gt;</description></item><item><title>Calling Guide.IsTrialMode on Windows Phone 7</title><link>http://blogs.msdn.com/b/dejohn/archive/2011/02/21/calling-guide-istrialmode-on-windows-phone-7.aspx</link><pubDate>Mon, 21 Feb 2011 07:58:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10132071</guid><dc:creator>Dean Johnson - MSFT</dc:creator><slash:comments>2</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/dejohn/rsscomments.aspx?WeblogPostID=10132071</wfw:commentRss><comments>http://blogs.msdn.com/b/dejohn/archive/2011/02/21/calling-guide-istrialmode-on-windows-phone-7.aspx#comments</comments><description>&lt;p&gt;As you may have &lt;a href="http://klucher.com/blog/trials-and-tribulations/"&gt;heard&lt;/a&gt; calling Guide.IsTrialMode can take around 60 milliseconds to return and thus should not be called each frame in your game. Unfortunately this performance penalty only occurs when your game is downloaded from the WP7 marketplace and not while you are in your development environment so you won't see that calls to Guide.IsTrialMode is slowing down your game&amp;rsquo;s performance.&lt;/p&gt;
&lt;p&gt;So you may be wondering when should you call Guide.IsTrialMode. You could check at startup or game activation and then cache the value but this means your game is depending on the game exiting or deactivating when you call Guide.ShowMarketplace.&lt;/p&gt;
&lt;p&gt;A better approach is to check the trial state on a background thread and cache the state for polling on the main thread. If the game is still in trial mode then the background thread can sleep for awhile until it wakes up and checks the state again. &lt;/p&gt;
&lt;p&gt;The TrialModeManager singleton class below implements this trial caching on a separate thread and can be used in your games to allow you to poll the trial state without hitting the 60 milliseconds penalty.&lt;/p&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="rem"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
&lt;span class="rem"&gt;/// Simple singleton class that will check the state of trial mode on a background thread&lt;/span&gt;
&lt;span class="rem"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; TrialModeManager
{
    &lt;span class="rem"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
    &lt;span class="rem"&gt;/// Property to get the one and only instance of this class&lt;/span&gt;
    &lt;span class="rem"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; TrialModeManager Instance 
    {
        get
        {
            &lt;span class="kwrd"&gt;if&lt;/span&gt; (instance == &lt;span class="kwrd"&gt;null&lt;/span&gt;)
            {
                instance = &lt;span class="kwrd"&gt;new&lt;/span&gt; TrialModeManager();
            }
            &lt;span class="kwrd"&gt;return&lt;/span&gt; instance;
        }
    }
    &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; TrialModeManager instance;

    &lt;span class="rem"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
    &lt;span class="rem"&gt;/// Returns the trial state&lt;/span&gt;
    &lt;span class="rem"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;bool&lt;/span&gt; TrialMode
    {
        get
        {
            &lt;span class="kwrd"&gt;return&lt;/span&gt; trialMode;
        }
    }
    &lt;span class="rem"&gt;// Default to trial mode&lt;/span&gt;
    &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;volatile&lt;/span&gt; &lt;span class="kwrd"&gt;bool&lt;/span&gt; trialMode = &lt;span class="kwrd"&gt;true&lt;/span&gt;;

    &lt;span class="rem"&gt;// The background thread&lt;/span&gt;
    &lt;span class="kwrd"&gt;private&lt;/span&gt; Thread trialThread;

    &lt;span class="rem"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
    &lt;span class="rem"&gt;/// Simple constructor that create the background thread and starts it up&lt;/span&gt;
    &lt;span class="rem"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;private&lt;/span&gt; TrialModeManager()
    {
        trialThread = &lt;span class="kwrd"&gt;new&lt;/span&gt; Thread(CheckTrial);
        trialThread.IsBackground = &lt;span class="kwrd"&gt;true&lt;/span&gt;;
        trialThread.Start();
        &lt;span class="rem"&gt;// Wait for the thread to start up&lt;/span&gt;
        &lt;span class="kwrd"&gt;while&lt;/span&gt; (!trialThread.IsAlive);
    }

    &lt;span class="rem"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
    &lt;span class="rem"&gt;/// The thread function that will check the trial state in the background&lt;/span&gt;
    &lt;span class="rem"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; CheckTrial()
    {
        &lt;span class="rem"&gt;// Stop the thread once we are no longer in trial since the state can't change anymore&lt;/span&gt;
        &lt;span class="kwrd"&gt;while&lt;/span&gt; (trialMode)
        {
            trialMode = Guide.IsTrialMode;

            &lt;span class="rem"&gt;// Sleep if the game is in trial &lt;/span&gt;
            &lt;span class="kwrd"&gt;if&lt;/span&gt; (trialMode)
            {
                Thread.Sleep(TimeSpan.FromSeconds(3));
            }
        }
    }
}&lt;/pre&gt;
&lt;p&gt;

&lt;/p&gt;
&lt;p&gt;Now in your game when you need to check if the game is in trial mode call TrialModeManager.Instance.TrialMode and you will not see a slowdown in your game.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10132071" width="1" height="1"&gt;</description></item><item><title>Simple Sound Effect Cue Class</title><link>http://blogs.msdn.com/b/dejohn/archive/2011/02/21/simple-sound-effect-cue-class.aspx</link><pubDate>Mon, 21 Feb 2011 06:51:39 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10132056</guid><dc:creator>Dean Johnson - MSFT</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/dejohn/rsscomments.aspx?WeblogPostID=10132056</wfw:commentRss><comments>http://blogs.msdn.com/b/dejohn/archive/2011/02/21/simple-sound-effect-cue-class.aspx#comments</comments><description>&lt;p&gt;In my last &lt;a href="http://blogs.msdn.com/b/dejohn/archive/2011/02/15/sound-cue-variations.aspx"&gt;post&lt;/a&gt; I talked about varying the sounds played for events in your game. I also said I would write about a simple sound effect cue system that would work on Windows Phone 7 and all of the other platforms XNA Game Studio 4.0 supports. &lt;/p&gt;  &lt;p&gt;Below is a simple class that will manage the playback of the several sound effects. The class will handle two types of playback. &lt;/p&gt;  &lt;pre class="csharpcode"&gt;&lt;span class="rem"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
&lt;span class="rem"&gt;/// Determines how the next sound effect in the cue will be selected&lt;/span&gt;
&lt;span class="rem"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;enum&lt;/span&gt; SelectionMode
{
    RandomNoRepeats,
    Shuffle,
}&lt;/pre&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;The first selection mode &lt;strong&gt;RandomNoRepeats&lt;/strong&gt; does exactly what the name suggests and plays a random sound effect without playing the same sound effect twice in a row. The second &lt;strong&gt;Shuffle&lt;/strong&gt; plays the list of sound effects in order. Once the end is reached the list is shuffled.&lt;/p&gt;

&lt;p&gt;Now lets create the class that will manage our cue of sound effects.&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="rem"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
&lt;span class="rem"&gt;/// Simple class to play a cue of sound effects&lt;/span&gt;
&lt;span class="rem"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; SoundEffectCue
{
    &lt;span class="rem"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
    &lt;span class="rem"&gt;/// We hold a list of sound effect instances for each type of sound effect in the cue&lt;/span&gt;
    &lt;span class="rem"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
    List&amp;lt;List&amp;lt;SoundEffectInstance&amp;gt;&amp;gt; soundEffectInstances;
    &lt;span class="rem"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
    &lt;span class="rem"&gt;/// The current selection mode&lt;/span&gt;
    &lt;span class="rem"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
    SelectionMode mode;
    &lt;span class="rem"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
    &lt;span class="rem"&gt;/// The index into the soundEffectInstances list of the last sound we played&lt;/span&gt;
    &lt;span class="rem"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;int&lt;/span&gt; lastPlayedIndex;

    Random random;

    &lt;span class="rem"&gt;// ...&lt;/span&gt;
}&lt;/pre&gt;

&lt;p&gt;The class has a few simple members. The first is a list that holds lists of sound effect instances. There is one list of sound effect instances per sound effect. Having multiple instances allows playback of the same sound effect even if a previous playback has not completed. The other variables hold the selection mode and the index of the last sound effect we played.&lt;/p&gt;

&lt;p&gt;The constructor for the class is the following.&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="rem"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
&lt;span class="rem"&gt;/// Constructor used to create a new instance of a SoundEffectCue. &lt;/span&gt;
&lt;span class="rem"&gt;/// The cue contains the sound effects that are passed into the constructor via the soundEffects parameter.&lt;/span&gt;
&lt;span class="rem"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
&lt;span class="kwrd"&gt;public&lt;/span&gt; SoundEffectCue(IEnumerable&amp;lt;SoundEffect&amp;gt; soundEffects, &lt;span class="kwrd"&gt;int&lt;/span&gt; instanceCount, SelectionMode mode)
{
    &lt;span class="rem"&gt;// Create the list that will hold the list of sound effect instances&lt;/span&gt;
    soundEffectInstances = &lt;span class="kwrd"&gt;new&lt;/span&gt; List&amp;lt;List&amp;lt;SoundEffectInstance&amp;gt;&amp;gt;();

    &lt;span class="rem"&gt;// Loop over the passed in sound effect instances&lt;/span&gt;
    &lt;span class="kwrd"&gt;foreach&lt;/span&gt; (SoundEffect soundEffect &lt;span class="kwrd"&gt;in&lt;/span&gt; soundEffects)
    {
        &lt;span class="rem"&gt;// Create a new list of sound effect instances for each sound effect&lt;/span&gt;
        List&amp;lt;SoundEffectInstance&amp;gt; instances = &lt;span class="kwrd"&gt;new&lt;/span&gt; List&amp;lt;SoundEffectInstance&amp;gt;();
        &lt;span class="kwrd"&gt;for&lt;/span&gt; (&lt;span class="kwrd"&gt;int&lt;/span&gt; i = 0; i &amp;lt; instanceCount; i++)
        {
            instances.Add(soundEffect.CreateInstance());
        }
        soundEffectInstances.Add(instances);
    }

    &lt;span class="kwrd"&gt;this&lt;/span&gt;.mode = mode;
    random = &lt;span class="kwrd"&gt;new&lt;/span&gt; Random();
    lastPlayedIndex = -1;

    &lt;span class="rem"&gt;// If we are using the shuffle selection mode then shuffle for the first time&lt;/span&gt;
    &lt;span class="kwrd"&gt;if&lt;/span&gt; (mode == SelectionMode.Shuffle)
    {
        Shuffle();
    }
}&lt;/pre&gt;

&lt;p&gt;The constructor creates the list of sound effect instances from the list of sound effects passed into the constructor. If the selection mode was set to shuffle the list is shuffled for the first time.&lt;/p&gt;

&lt;p&gt;The only other public method in the class is the Play method which will play a sound effect in the cue.&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="rem"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
&lt;span class="rem"&gt;/// Called from the game when you want to play a sound from the cue&lt;/span&gt;
&lt;span class="rem"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Play()
{
    &lt;span class="rem"&gt;// Call the appropriate play method based on the selection mode&lt;/span&gt;
    &lt;span class="kwrd"&gt;switch&lt;/span&gt; (mode)
    {
        &lt;span class="kwrd"&gt;case&lt;/span&gt; SelectionMode.RandomNoRepeats:
            PlayRandomNoRepeats();
            &lt;span class="kwrd"&gt;break&lt;/span&gt;;
        &lt;span class="kwrd"&gt;case&lt;/span&gt; SelectionMode.Shuffle:
            PlayShuffle();
            &lt;span class="kwrd"&gt;break&lt;/span&gt;;
        &lt;span class="kwrd"&gt;default&lt;/span&gt;:
            Debug.Assert(&lt;span class="kwrd"&gt;false&lt;/span&gt;, &lt;span class="str"&gt;&amp;quot;Invalid playback mode&amp;quot;&lt;/span&gt;);
            &lt;span class="kwrd"&gt;break&lt;/span&gt;;
    };
}&lt;/pre&gt;


&lt;p&gt;The Play method calls out to different play methods depending on the selection mode. &lt;/p&gt;

&lt;p&gt;The first play method PlayRandomNoRepeats will play a random sound effect from the cue without playing the same sound effect twice in a row.&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="rem"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
&lt;span class="rem"&gt;/// Plays a random sound effect from the cue and prevents repeating sound effects from playing.&lt;/span&gt;
&lt;span class="rem"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
&lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; PlayRandomNoRepeats()
{
    &lt;span class="rem"&gt;// Select a random index&lt;/span&gt;
    &lt;span class="kwrd"&gt;int&lt;/span&gt; playIndex = random.Next(soundEffectInstances.Count);

    &lt;span class="rem"&gt;// Did we just play this sound effect?&lt;/span&gt;
    &lt;span class="kwrd"&gt;if&lt;/span&gt; (playIndex == lastPlayedIndex)
    {
        &lt;span class="rem"&gt;// Just play the next index&lt;/span&gt;
        playIndex++;
        &lt;span class="rem"&gt;// Bounds check&lt;/span&gt;
        &lt;span class="kwrd"&gt;if&lt;/span&gt; (playIndex &amp;gt;= soundEffectInstances.Count)
        {
            playIndex = 0;
        }
    }

    &lt;span class="rem"&gt;// Loop over all of the instances looking for one that can be played&lt;/span&gt;
    &lt;span class="kwrd"&gt;for&lt;/span&gt; (&lt;span class="kwrd"&gt;int&lt;/span&gt; i = 0; i &amp;lt; soundEffectInstances[playIndex].Count; i++)
    {
        &lt;span class="rem"&gt;// If the instance is not playing already we can play the sound&lt;/span&gt;
        &lt;span class="kwrd"&gt;if&lt;/span&gt; (soundEffectInstances[playIndex][i].State != SoundState.Playing)
        {
            lastPlayedIndex = playIndex;
            soundEffectInstances[playIndex][i].Play();
            &lt;span class="kwrd"&gt;return&lt;/span&gt;;
        }
    }

    &lt;span class="rem"&gt;// Could not find a sound effect instance to play&lt;/span&gt;
    Debug.Assert(&lt;span class="kwrd"&gt;false&lt;/span&gt;, &lt;span class="str"&gt;&amp;quot;All sound effect instances were already playing. Increase the number of sound effect instances.&amp;quot;&lt;/span&gt;);
}&lt;/pre&gt;

&lt;p&gt;If all of the sound effect instance for the selected sound effect index are already playing the assert will be hit and lets you know that you should increate the number of instances in the cue.&lt;/p&gt;

&lt;p&gt;The other playback method PlayShuffle is similar except it plays the sound effects in the list order until it reaches the end of the list where it then shuffles the list.&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="rem"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
&lt;span class="rem"&gt;/// Plays sound effects in order until reaching the end of the list then shuffle the list&lt;/span&gt;
&lt;span class="rem"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
&lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; PlayShuffle()
{
    &lt;span class="rem"&gt;// Increment to the next sound effect in the list&lt;/span&gt;
    lastPlayedIndex++;
    &lt;span class="rem"&gt;// Are we at the end of the list?&lt;/span&gt;
    &lt;span class="kwrd"&gt;if&lt;/span&gt; (lastPlayedIndex &amp;gt;= soundEffectInstances.Count)
    {
        Shuffle();
    }

    &lt;span class="rem"&gt;// Loop over all of the instances looking for one that can be played&lt;/span&gt;
    &lt;span class="kwrd"&gt;for&lt;/span&gt; (&lt;span class="kwrd"&gt;int&lt;/span&gt; i = 0; i &amp;lt; soundEffectInstances[lastPlayedIndex].Count; i++)
    {
        &lt;span class="rem"&gt;// If the instance is not playing already we can play the sound&lt;/span&gt;
        &lt;span class="kwrd"&gt;if&lt;/span&gt; (soundEffectInstances[lastPlayedIndex][i].State != SoundState.Playing)
        {
            soundEffectInstances[lastPlayedIndex][i].Play();
            &lt;span class="kwrd"&gt;return&lt;/span&gt;;
        }
    }
            
    &lt;span class="rem"&gt;// Could not find a sound effect instance to play&lt;/span&gt;
    Debug.Assert(&lt;span class="kwrd"&gt;false&lt;/span&gt;, &lt;span class="str"&gt;&amp;quot;All sound effect instances were already playing. Increase the number of sound effect instances.&amp;quot;&lt;/span&gt;);
}&lt;/pre&gt;

&lt;p&gt;The PlayShuffle method utilizes the last method in the class Shuffle which performs a &lt;a href="http://en.wikipedia.org/wiki/Knuth_shuffle"&gt;Fischer-Yates shuffle&lt;/a&gt; on the list of sound effect instances. &lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="rem"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
&lt;span class="rem"&gt;/// An implementation of the Fischer-Yates shuffle that does not allow for the last item in the list to become the &lt;/span&gt;
&lt;span class="rem"&gt;/// first item in the list. This prevents duplicate sounds after the shuffle.&lt;/span&gt;
&lt;span class="rem"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
&lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Shuffle()
{
    &lt;span class="rem"&gt;// Save the last sound effect played&lt;/span&gt;
    List&amp;lt;SoundEffectInstance&amp;gt; lastSoundPlayed = soundEffectInstances[soundEffectInstances.Count - 1];

    &lt;span class="rem"&gt;// Fischer-Yates shuffle&lt;/span&gt;
    &lt;span class="kwrd"&gt;for&lt;/span&gt; (&lt;span class="kwrd"&gt;int&lt;/span&gt; i = soundEffectInstances.Count; i &amp;gt; 1; i--)
    {
        &lt;span class="kwrd"&gt;int&lt;/span&gt; swapIndex = random.Next(i);
        List&amp;lt;SoundEffectInstance&amp;gt; temp = soundEffectInstances[swapIndex];
        soundEffectInstances[swapIndex] = soundEffectInstances[i - 1];
        soundEffectInstances[i - 1] = temp;
    }

    &lt;span class="rem"&gt;// Check to see if we just played this&lt;/span&gt;
    &lt;span class="kwrd"&gt;if&lt;/span&gt; (lastSoundPlayed == soundEffectInstances[0])
    {
        &lt;span class="rem"&gt;// Swap for some other random index&lt;/span&gt;
        &lt;span class="kwrd"&gt;int&lt;/span&gt; swapIndex = random.Next(1, soundEffectInstances.Count);
        soundEffectInstances[0] = soundEffectInstances[swapIndex];
        soundEffectInstances[swapIndex] = lastSoundPlayed;
    }

    &lt;span class="rem"&gt;// Reset the play index&lt;/span&gt;
    lastPlayedIndex = 0;
}&lt;/pre&gt;

&lt;p&gt;So that's a simple sound effect cue class that will allow you to have multiple sound effects in a single cue that can be used for a sound event in your game and allow for sound variation.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10132056" width="1" height="1"&gt;</description></item><item><title>Sound Cue Variations</title><link>http://blogs.msdn.com/b/dejohn/archive/2011/02/15/sound-cue-variations.aspx</link><pubDate>Tue, 15 Feb 2011 06:44:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10129380</guid><dc:creator>Dean Johnson - MSFT</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/dejohn/rsscomments.aspx?WeblogPostID=10129380</wfw:commentRss><comments>http://blogs.msdn.com/b/dejohn/archive/2011/02/15/sound-cue-variations.aspx#comments</comments><description>&lt;p&gt;Last week I was reading my friend Shawn&amp;rsquo;s blog post &amp;ldquo;&lt;a href="http://blogs.msdn.com/b/shawnhar/archive/2011/02/09/random-shuffle.aspx"&gt;Random shuffle&lt;/a&gt;&amp;rdquo; and it reminded me of a system I worked on that controlled the playback of sound effects.&lt;/p&gt;
&lt;p&gt;When you play sound effects in a game you will often want to have a number of sound effects that can be played for each event in the game. For example you may have multiple sounds for when a player takes damage. Having multiple sound effects for events especially those that occur often in the game prevents the user from becoming annoyed and keeps the audio less predictable.&lt;/p&gt;
&lt;p&gt;If you build games using XNA for Windows or Xbox 360 you may be familiar with the Microsoft Cross-Platform Audio Creation Tool or XAct for short. XAct allows you to setup audio cues that consist of multiple wave files. In your game code you tell XAct to play a specific cue. The cue then determines which of the specific wave files to play.&lt;/p&gt;
&lt;p&gt;The specific wave to play is determined by the &amp;ldquo;&lt;a href="http://msdn.microsoft.com/en-us/library/ee415970(v=VS.85).aspx"&gt;Variation Playlist&lt;/a&gt;&amp;rdquo; setting for the audio cue.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/CommunityServer-Blogs-Components-WeblogFiles/00-00-00-77-97-metablogapi/5315.clip_5F00_image001_5F00_23FCC5CB.jpg"&gt;&lt;img height="366" width="569" src="http://blogs.msdn.com/cfs-file.ashx/__key/CommunityServer-Blogs-Components-WeblogFiles/00-00-00-77-97-metablogapi/7140.clip_5F00_image001_5F00_thumb_5F00_6CD337D1.jpg" alt="clip_image001" border="0" title="clip_image001" style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;XAct provides six different variation types: &lt;/p&gt;
&lt;p&gt;&amp;middot; Ordered &amp;ndash; The waves in the cue are played in a specific configured order.&lt;/p&gt;
&lt;p&gt;&amp;middot; Ordered from Random &amp;ndash; The waves are put into a random order and are then played in that order.&lt;/p&gt;
&lt;p&gt;&amp;middot; Random &amp;ndash; A random wave is selected and played.&lt;/p&gt;
&lt;p&gt;&amp;middot; Random (no immediate repeats) &amp;ndash; A random wave is played but is guaranteed not to be a repeat of the previous wave. Of course if there is only a single wave in the cue the sound will have to repeat.&lt;/p&gt;
&lt;p&gt;&amp;middot; Shuffle &amp;ndash; The list of wave that can be played are shuffled and then played in the shuffled order. Once all waves have been played the list is shuffled again.&lt;/p&gt;
&lt;p&gt;&amp;middot; Interactive &amp;ndash; Allows for interactive control between the game and XAct more info can be found &lt;a href="http://msdn.microsoft.com/en-us/library/ee416165(v=VS.85).aspx"&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Often shuffle or random (no immediate repeats) will be what you want to use for your game.&lt;/p&gt;
&lt;p&gt;If you are building a game on Windows Phone 7 you unfortunately won&amp;rsquo;t have access to the functionality provided by XAct and you will need to build the variation functionality into your game yourself. &lt;/p&gt;
&lt;p&gt;In my &lt;a href="http://blogs.msdn.com/b/dejohn/archive/2011/02/21/simple-sound-effect-cue-class.aspx"&gt;next post&lt;/a&gt; I will discuss a simple sound effect instance variation system that you can use in your WP7 games.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10129380" width="1" height="1"&gt;</description></item><item><title>XNA Game Studio 4.0 Programming: Developing for Windows Phone 7 and Xbox 360</title><link>http://blogs.msdn.com/b/dejohn/archive/2011/01/30/xna-game-studio-4-0-programming-developing-for-windows-phone-7-and-xbox-360.aspx</link><pubDate>Sun, 30 Jan 2011 02:33:28 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10121989</guid><dc:creator>Dean Johnson - MSFT</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/dejohn/rsscomments.aspx?WeblogPostID=10121989</wfw:commentRss><comments>http://blogs.msdn.com/b/dejohn/archive/2011/01/30/xna-game-studio-4-0-programming-developing-for-windows-phone-7-and-xbox-360.aspx#comments</comments><description>&lt;p&gt;I have been a avid reader of technical books, mostly around computer programming since I was very young. Growing up I read many more technical books than I ever did fiction novels. It has also been a goal to someday write a technical book of my own. &lt;/p&gt;  &lt;p&gt;During the development of XNA Game Studio 4.0 I thought it would a good time to write a book on XNA Game Studio 4.0. Luckily for me my coworker and friend Tom Miller was also looking to write another book. So we decided that we would work on a book together. &lt;/p&gt;  &lt;p&gt;The book is now finished an available at your preferred book store. The amazon.com link is below.&lt;/p&gt;  &lt;p&gt;&lt;a title="http://www.amazon.com/XNA-Game-Studio-4-0-Programming/dp/0672333457/ref=sr_1_3?ie=UTF8&amp;amp;qid=1296353406&amp;amp;sr=8-3#_" href="http://www.amazon.com/XNA-Game-Studio-4-0-Programming/dp/0672333457/ref=sr_1_3?ie=UTF8&amp;amp;qid=1296353406&amp;amp;sr=8-3#_"&gt;http://www.amazon.com/XNA-Game-Studio-4-0-Programming/dp/0672333457/ref=sr_1_3?ie=UTF8&amp;amp;qid=1296353406&amp;amp;sr=8-3#_&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;We also have a Facebook page where you can tell us what you think about the book and any bugs you might find.&lt;/p&gt;  &lt;p&gt;&lt;a title="http://www.facebook.com/home.php#!/pages/XNA-Game-Studio-40-Programming-Developing-for-Windows-Phone-and-Xbox-Live/148023561880764" href="http://www.facebook.com/home.php#!/pages/XNA-Game-Studio-40-Programming-Developing-for-Windows-Phone-and-Xbox-Live/148023561880764"&gt;http://www.facebook.com/home.php#!/pages/XNA-Game-Studio-40-Programming-Developing-for-Windows-Phone-and-Xbox-Live/148023561880764&lt;/a&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10121989" width="1" height="1"&gt;</description></item><item><title>Avatar API changes in XNA Game Studio 4.0</title><link>http://blogs.msdn.com/b/dejohn/archive/2011/01/30/avatar-api-changes-in-xna-game-studio-4-0.aspx</link><pubDate>Sun, 30 Jan 2011 02:09:06 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10121987</guid><dc:creator>Dean Johnson - MSFT</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/dejohn/rsscomments.aspx?WeblogPostID=10121987</wfw:commentRss><comments>http://blogs.msdn.com/b/dejohn/archive/2011/01/30/avatar-api-changes-in-xna-game-studio-4-0.aspx#comments</comments><description>&lt;p&gt;When we started to build XNA Game Studio 4.0 we made the decision to break the Microsoft.Xna.Framework.dll assembly into multiple assemblies to better support different functionality multiple platforms. Avatar functionality was pulled out into its own assembly called Microsoft.Xna.Framework.Avatar.dll. When you create a new Xbox 360 or Windows project in XNA Game Studio 4.0 your project will already contain this assembly reference by default. &lt;/p&gt;  &lt;p&gt;In order to move avatar functionality&amp;#160; into its own assembly we needed to ensure that types in other assemblies never referenced the types in the avatar assembly. In XNA Game Studio 3.1 the SignedInGamer type contained a property called Avatar which was of type AvatarDescription. Since SignedInGamer was moved into the Microsoft.Xna.Framework.GamerServices.dll assembly we had to remove the SignedInGamer.Avatar property. &lt;/p&gt;  &lt;p&gt;At the same time we also wanted to provide a way to allow developers to access the avatar of any Gamer they may have a reference to. That includes FriendGamer and NetworkGamer objects something that was not supported in XNA Game Studio 3.1. &lt;/p&gt;  &lt;p&gt;In order to obtain an AvatarDescription for a specific Gamer you now use the AvatarDescription.BeginGetFromGamer method. This method is asynchronous since it may take more than a few cycles to query the type of Gamer that you have passed in. &lt;/p&gt;  &lt;p&gt;To load the local players avatar the code now looks like this.&lt;/p&gt;  &lt;p&gt;First you will still need to initialize the GamerServicesComponent and subscribe to the SignedInGamer.SignedIn event in your Game class constructor.&lt;/p&gt;  &lt;pre class="csharpcode"&gt;Components.Add(&lt;span class="kwrd"&gt;new&lt;/span&gt; GamerServicesComponent(&lt;span class="kwrd"&gt;this&lt;/span&gt;));
SignedInGamer.SignedIn += &lt;span class="kwrd"&gt;new&lt;/span&gt; EventHandler&amp;lt;SignedInEventArgs&amp;gt;(SignedInGamer_SignedIn);&lt;/pre&gt;

&lt;p&gt;Next we need to create the SignedInGamer_SignedIn method that will handle the event.&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;void&lt;/span&gt; SignedInGamer_SignedIn(&lt;span class="kwrd"&gt;object&lt;/span&gt; sender, SignedInEventArgs e)
{
    &lt;span class="rem"&gt;// Only handle player one sign in&lt;/span&gt;
    &lt;span class="kwrd"&gt;if&lt;/span&gt; (e.Gamer.PlayerIndex == PlayerIndex.One)
    {
        AvatarDescription.BeginGetFromGamer(e.Gamer, LoadGamerAvatar, &lt;span class="kwrd"&gt;null&lt;/span&gt;);
    }
}&lt;/pre&gt;

&lt;p&gt;Once the BeginGetFromGamer method has completed it will call the LoadGamerAvatar method that we define below.&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;void&lt;/span&gt; LoadGamerAvatar(IAsyncResult result)
{
    &lt;span class="rem"&gt;// Get the AvatarDescription for the gamer&lt;/span&gt;
    avatarDescription = AvatarDescription.EndGetFromGamer(result);

    &lt;span class="rem"&gt;// Load the AvatarRenderer if description is valid&lt;/span&gt;
    &lt;span class="kwrd"&gt;if&lt;/span&gt; (avatarDescription.IsValid)
        avatarRenderer = &lt;span class="kwrd"&gt;new&lt;/span&gt; AvatarRenderer(avatarDescription);
}&lt;/pre&gt;

&lt;p&gt;We call EndGetFromGamer with the IAsyncResult to obtain the AvatarDescription. In this case avatarDescription and avatarRenderer are defined as member variables in the class. Before creating the AvatarRenderer we verify that the description is valid. &lt;/p&gt;

&lt;p&gt;Another change that occurred with avatars in XNA Game Studio 4.0 was that their proportions were updated. This was a change that occurred across the Xbox 360 platform and we support in XNA Game Studio 4.0. &lt;/p&gt;

&lt;p&gt;This does mean that custom animations need to be authored with a new animation rig. Luckily the existing rigs for Maya and the Softimage Mod Tool were updated to the new proportions and a new 3D Studio Max version was also created. All of these can be found at the link below.&lt;/p&gt;

&lt;p&gt;&lt;a title="http://create.msdn.com/en-US/education/catalog/utility/avatar_animation_rig" href="http://create.msdn.com/en-US/education/catalog/utility/avatar_animation_rig"&gt;http://create.msdn.com/en-US/education/catalog/utility/avatar_animation_rig&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Along with the rigs the animation packs were also updated and can be found at the link below.&lt;/p&gt;

&lt;p&gt;&lt;a title="http://create.msdn.com/en-US/education/catalog/utility/avatar_animation_pack" href="http://create.msdn.com/en-US/education/catalog/utility/avatar_animation_pack"&gt;http://create.msdn.com/en-US/education/catalog/utility/avatar_animation_pack&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Finally the avatar samples have also been update to support XNA Game Studio 4.0 including the custom avatar animation sample.&lt;/p&gt;

&lt;p&gt;&lt;a title="http://create.msdn.com/en-US/education/catalog/sample/custom_avatar_animation" href="http://create.msdn.com/en-US/education/catalog/sample/custom_avatar_animation"&gt;http://create.msdn.com/en-US/education/catalog/sample/custom_avatar_animation&lt;/a&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10121987" width="1" height="1"&gt;</description></item><item><title>Custom Avatar Animations PLUS 21 New Animations</title><link>http://blogs.msdn.com/b/dejohn/archive/2009/07/20/custom-avatar-animations-plus-21-new-animations.aspx</link><pubDate>Mon, 20 Jul 2009 23:15:25 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9842239</guid><dc:creator>Dean Johnson - MSFT</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/dejohn/rsscomments.aspx?WeblogPostID=9842239</wfw:commentRss><comments>http://blogs.msdn.com/b/dejohn/archive/2009/07/20/custom-avatar-animations-plus-21-new-animations.aspx#comments</comments><description>&lt;p&gt;We have just posted three new avatar items to the XNA Creators Club Online site. &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Custom Avatar Animation Sample     &lt;br /&gt;&lt;/strong&gt;This sample shows users how to import and play custom avatar animations though the content pipeline. &lt;a href="http://creators.xna.com/en-US/sample/customavataranimation"&gt;http://creators.xna.com/en-US/sample/customavataranimation&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Avatar Animation Rig&lt;/strong&gt;    &lt;br /&gt;We have provided both an Softimage Mod Tool 7.5 version and Maya 2008 version of the animation rig that can be used to create custom animations. &lt;a href="http://creators.xna.com/en-US/utility/avataranimationrig"&gt;http://creators.xna.com/en-US/utility/avataranimationrig&lt;/a&gt; Please take a look at the “readme” to understand how to load and export animations.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Avatar Animation Pack&lt;/strong&gt;    &lt;br /&gt;This pack contains 21 new animations that users can use in their games. We provided them in FBX format for direct use in your games or in source format for both Softimage Mod Tool 7.5 and Maya 2008. &lt;a href="http://creators.xna.com/en-US/utility/avataranimationpack"&gt;http://creators.xna.com/en-US/utility/avataranimationpack&lt;/a&gt; Please take a look at the “readme” to understand how to load and export animations.&lt;/p&gt;  &lt;p&gt;Enjoy&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9842239" width="1" height="1"&gt;</description></item><item><title>Simple example on how to load a signed in gamers avatar</title><link>http://blogs.msdn.com/b/dejohn/archive/2009/06/24/simple-example-on-how-to-load-a-sign-in-gamers-avatar.aspx</link><pubDate>Wed, 24 Jun 2009 23:27:01 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9802038</guid><dc:creator>Dean Johnson - MSFT</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/dejohn/rsscomments.aspx?WeblogPostID=9802038</wfw:commentRss><comments>http://blogs.msdn.com/b/dejohn/archive/2009/06/24/simple-example-on-how-to-load-a-sign-in-gamers-avatar.aspx#comments</comments><description>&lt;p&gt;Now that XNA Game Studio 3.1 has been released I have had time to notice the types of questions people are asking on the forums about avatars. One question that has come up a few times is how to load a signed in gamers avatar. There are a couple of pitfalls. One thing that can be confusing to developers is that a SignedInGamer from the Gamer.SignedInGamers collection may be null for the first few frames when the game loads. So when a developer tries to read the Avatar property of a signed in gamer they will receive a null reference exception. The developer needs to wait until the gamer is recognized as being signed in and then attempt to load the players avatar. You game should not just check for the signed in gamer in a tight loop. Your game should go about doing other things like updating input and rendering. Games also need to handle the case where the signed in gamer doesn't have an avatar.&lt;/p&gt;  &lt;p&gt;There are a few ways to wait for the SignedInGamer to not be null and load the appropriate avatar. I will show a couple of methods. The first just polls and checks to see if the signed in gamer is not null and when it is not it loads the players avatar. If the player doesn't have an avatar a random one is created. Also if the system takes longer than 3 seconds for the gamer to sign then the code will load a random one instead. Below is an example of the first method.&lt;/p&gt;  &lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; SimpleAvatarGame : Microsoft.Xna.Framework.Game
{
    GraphicsDeviceManager graphics;

    AvatarDescription avatarDescription;
    AvatarRenderer avatarRenderer;
    AvatarAnimation avatarAnimation;

    &lt;span class="kwrd"&gt;public&lt;/span&gt; SimpleAvatarGame()
    {
        graphics = &lt;span class="kwrd"&gt;new&lt;/span&gt; GraphicsDeviceManager(&lt;span class="kwrd"&gt;this&lt;/span&gt;);
        Content.RootDirectory = &lt;span class="str"&gt;&amp;quot;Content&amp;quot;&lt;/span&gt;;

        graphics.PreferMultiSampling = &lt;span class="kwrd"&gt;true&lt;/span&gt;;
        graphics.PreferredBackBufferWidth = 1280;
        graphics.PreferredBackBufferHeight = 720;

        Components.Add(&lt;span class="kwrd"&gt;new&lt;/span&gt; GamerServicesComponent(&lt;span class="kwrd"&gt;this&lt;/span&gt;)); 
    }

    &lt;span class="kwrd"&gt;protected&lt;/span&gt; &lt;span class="kwrd"&gt;override&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; LoadContent()
    {
        avatarAnimation = &lt;span class="kwrd"&gt;new&lt;/span&gt; AvatarAnimation(AvatarAnimationPreset.Celebrate);
    }

    &lt;span class="kwrd"&gt;protected&lt;/span&gt; &lt;span class="kwrd"&gt;override&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Update(GameTime gameTime)
    {
        &lt;span class="kwrd"&gt;if&lt;/span&gt; (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            &lt;span class="kwrd"&gt;this&lt;/span&gt;.Exit();

        LoadUserAvatar(gameTime);

        &lt;span class="rem"&gt;// You may want to check that the avatar renderer has loaded&lt;/span&gt;
        &lt;span class="rem"&gt;// so that the animation does not start playing until it has loaded&lt;/span&gt;
        avatarAnimation.Update(gameTime.ElapsedGameTime, &lt;span class="kwrd"&gt;true&lt;/span&gt;);

        &lt;span class="kwrd"&gt;base&lt;/span&gt;.Update(gameTime);
    }

    &lt;span class="rem"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
    &lt;span class="rem"&gt;/// Load player one's avatar&lt;/span&gt;
    &lt;span class="rem"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; LoadUserAvatar(GameTime gameTime)
    {
        &lt;span class="rem"&gt;// Avatar may already be loaded&lt;/span&gt;
        &lt;span class="kwrd"&gt;if&lt;/span&gt; (avatarRenderer != &lt;span class="kwrd"&gt;null&lt;/span&gt;)
            &lt;span class="kwrd"&gt;return&lt;/span&gt;;

        &lt;span class="rem"&gt;// Check to see if the user is signed in&lt;/span&gt;
        &lt;span class="kwrd"&gt;if&lt;/span&gt; (Gamer.SignedInGamers[PlayerIndex.One] != &lt;span class="kwrd"&gt;null&lt;/span&gt;)
        {
            &lt;span class="rem"&gt;// Get the users avatar description&lt;/span&gt;
            avatarDescription = Gamer.SignedInGamers[PlayerIndex.One].Avatar;

            &lt;span class="rem"&gt;// If this is not valid the user doen't have an avatar&lt;/span&gt;
            &lt;span class="kwrd"&gt;if&lt;/span&gt; (!avatarDescription.IsValid)
            {
                avatarDescription = AvatarDescription.CreateRandom();
            }

            avatarRenderer = &lt;span class="kwrd"&gt;new&lt;/span&gt; AvatarRenderer(avatarDescription);
        }
        &lt;span class="rem"&gt;// Check to see if it has been longer than 3 seconds&lt;/span&gt;
        &lt;span class="kwrd"&gt;else&lt;/span&gt; &lt;span class="kwrd"&gt;if&lt;/span&gt; (gameTime.TotalGameTime.TotalSeconds &amp;gt; 3)
        {
            avatarDescription = AvatarDescription.CreateRandom();
            avatarRenderer = &lt;span class="kwrd"&gt;new&lt;/span&gt; AvatarRenderer(avatarDescription);
        }
    }

    &lt;span class="kwrd"&gt;protected&lt;/span&gt; &lt;span class="kwrd"&gt;override&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.CornflowerBlue);

        &lt;span class="kwrd"&gt;if&lt;/span&gt; (avatarRenderer != &lt;span class="kwrd"&gt;null&lt;/span&gt;)
        {
            avatarRenderer.World = Matrix.CreateRotationY(MathHelper.ToRadians(180.0f));
            avatarRenderer.View = Matrix.CreateLookAt(&lt;span class="kwrd"&gt;new&lt;/span&gt; Vector3(0, 1, 3), &lt;span class="kwrd"&gt;new&lt;/span&gt; Vector3(0, 1, 0), Vector3.Up);&lt;br /&gt;            avatarRenderer.Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45.0f), &lt;br /&gt;                                                     GraphicsDevice.Viewport.AspectRatio, .01f, 200.0f); 
            avatarRenderer.Draw(avatarAnimation.BoneTransforms, avatarAnimation.Expression);
        }

        &lt;span class="kwrd"&gt;base&lt;/span&gt;.Draw(gameTime);
    }
}&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;&lt;/pre&gt;

&lt;p&gt;Another technique for determining when the gamer has signed in and to read their Avatar property is to subscribe to the SignedInGamer.SignedIn event handler. Below is an example how to do this.&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; SimpleAvatarGame : Microsoft.Xna.Framework.Game
{
    GraphicsDeviceManager graphics;

    AvatarDescription avatarDescription;
    AvatarRenderer avatarRenderer;
    AvatarAnimation avatarAnimation;

    &lt;span class="kwrd"&gt;public&lt;/span&gt; SimpleAvatarGame()
    {
        graphics = &lt;span class="kwrd"&gt;new&lt;/span&gt; GraphicsDeviceManager(&lt;span class="kwrd"&gt;this&lt;/span&gt;);
        Content.RootDirectory = &lt;span class="str"&gt;&amp;quot;Content&amp;quot;&lt;/span&gt;;

        graphics.PreferMultiSampling = &lt;span class="kwrd"&gt;true&lt;/span&gt;;
        graphics.PreferredBackBufferWidth = 1280;
        graphics.PreferredBackBufferHeight = 720;

        Components.Add(&lt;span class="kwrd"&gt;new&lt;/span&gt; GamerServicesComponent(&lt;span class="kwrd"&gt;this&lt;/span&gt;));

        SignedInGamer.SignedIn += &lt;span class="kwrd"&gt;new&lt;/span&gt; EventHandler&amp;lt;SignedInEventArgs&amp;gt;(LoadGamerAvatar); 
    }

    &lt;span class="kwrd"&gt;void&lt;/span&gt; LoadGamerAvatar(&lt;span class="kwrd"&gt;object&lt;/span&gt; sender, SignedInEventArgs e)
    {
        avatarDescription = e.Gamer.Avatar;

        &lt;span class="rem"&gt;// Check to see if the player has an avatar&lt;/span&gt;
        &lt;span class="kwrd"&gt;if&lt;/span&gt; (!avatarDescription.IsValid)
        {
            avatarDescription = AvatarDescription.CreateRandom();
        }

        avatarRenderer = &lt;span class="kwrd"&gt;new&lt;/span&gt; AvatarRenderer(avatarDescription);
    } 

    &lt;span class="kwrd"&gt;protected&lt;/span&gt; &lt;span class="kwrd"&gt;override&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; LoadContent()
    {
        avatarAnimation = &lt;span class="kwrd"&gt;new&lt;/span&gt; AvatarAnimation(AvatarAnimationPreset.Celebrate);
    }

    &lt;span class="kwrd"&gt;protected&lt;/span&gt; &lt;span class="kwrd"&gt;override&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Update(GameTime gameTime)
    {
        &lt;span class="kwrd"&gt;if&lt;/span&gt; (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            &lt;span class="kwrd"&gt;this&lt;/span&gt;.Exit();

        &lt;span class="rem"&gt;// You may want to check that the avatar renderer has loaded&lt;/span&gt;
        &lt;span class="rem"&gt;// so that the animation does not start playing until it has loaded&lt;/span&gt;
        avatarAnimation.Update(gameTime.ElapsedGameTime, &lt;span class="kwrd"&gt;true&lt;/span&gt;);

        &lt;span class="kwrd"&gt;base&lt;/span&gt;.Update(gameTime);
    }

    &lt;span class="kwrd"&gt;protected&lt;/span&gt; &lt;span class="kwrd"&gt;override&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.CornflowerBlue);

        &lt;span class="kwrd"&gt;if&lt;/span&gt; (avatarRenderer != &lt;span class="kwrd"&gt;null&lt;/span&gt;)
        {
            avatarRenderer.World = Matrix.CreateRotationY(MathHelper.ToRadians(180.0f));&lt;br /&gt;            avatarRenderer.View = Matrix.CreateLookAt(&lt;span class="kwrd"&gt;new&lt;/span&gt; Vector3(0, 1, 3), &lt;span class="kwrd"&gt;new&lt;/span&gt; Vector3(0, 1, 0), Vector3.Up);
            avatarRenderer.Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45.0f), &lt;br /&gt;                                                     GraphicsDevice.Viewport.AspectRatio, .01f, 200.0f);
            avatarRenderer.Draw(avatarAnimation.BoneTransforms, avatarAnimation.Expression);
        }

        &lt;span class="kwrd"&gt;base&lt;/span&gt;.Draw(gameTime);
    }
}&lt;/pre&gt;
&lt;style type="text/css"&gt;
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;

&lt;p&gt;If you have any questions please ask on the forums here &lt;a href="http://forums.xna.com"&gt;http://forums.xna.com&lt;/a&gt;.&amp;#160; &lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9802038" width="1" height="1"&gt;</description></item><item><title>AvatarDescription.Description</title><link>http://blogs.msdn.com/b/dejohn/archive/2009/05/28/avatardescription-description.aspx</link><pubDate>Thu, 28 May 2009 21:54:22 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9648489</guid><dc:creator>Dean Johnson - MSFT</dc:creator><slash:comments>3</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/dejohn/rsscomments.aspx?WeblogPostID=9648489</wfw:commentRss><comments>http://blogs.msdn.com/b/dejohn/archive/2009/05/28/avatardescription-description.aspx#comments</comments><description>&lt;p&gt;In a couple of previous posts &lt;a href="http://blogs.msdn.com/dejohn/archive/2009/05/08/avatar-api-preview-for-xna-game-studio-3-1.aspx"&gt;here&lt;/a&gt; and &lt;a href="http://blogs.msdn.com/dejohn/archive/2009/05/20/avatardescription-isvalid-when-why-the-description-returned-is-invalid.aspx"&gt;here&lt;/a&gt; I talked about the AvatarDescription.Description property that returns a byte[].&lt;/p&gt;  &lt;p&gt;The AvatarDescription.Description property is provided to allow developers to recreate a previously created AvatarDescription object. The byte[] from the property is used in conjunction with the AvatarDescription(byte[] ) constructor to recreate the description object.&lt;/p&gt;  &lt;p&gt;There are three main scenarios this enables. &lt;/p&gt;  &lt;p&gt;&lt;b&gt;Avatars in online games&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;When a player joins an online game their game sends their AvatarDescription.Description to all of the other players in the session. Each of the other players can then create the AvatarDescription object using the AvatarDescription(byte[] ) constructor. &lt;/p&gt;  &lt;p&gt;&lt;b&gt;Saving a players random avatar choice&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;You may want to allow a player to select some of the characters in their game. For example a user may pick party members in an RPG. The developer can continue to generate random AvatarDescription objects by calling AvatarDescription.CreateRandom(). The developer can then save the AvatarDescription.Description for the random avatars the player as selected along with other save game data. The data can then be read when the player loads the game so that they see the same characters they had in a previous game.&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Loading a avatar designed by the developer&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;The final scenario allows the developer to design a specific avatar for use as a character in their game. This can be done by creating the desired look for the avatar in the Xbox 360 dash avatar editor. Then use an Xbox 360 XNA Game Studio game to load the AvatarDescription object from the SignedInGamer.Avatar property for the profile associated with the avatar you want to use. Put a breakpoint somewhere in your code after loading the AvatarDescription. Debug the code and put the AvatarDescription object you created in the watch window in Visual Studio. &lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/dejohn/WindowsLiveWriter/AvatarDescription.Description_A760/clip_image002_2.jpg"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="clip_image002" border="0" alt="clip_image002" src="http://blogs.msdn.com/blogfiles/dejohn/WindowsLiveWriter/AvatarDescription.Description_A760/clip_image002_thumb.jpg" width="591" height="366" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;Next you will need to copy the contents of the Description property and format the text. You can then use these values to construct a new byte[] that can be used to create the AvatarDescription in your game. You can either hard code this byte[] in your source code or save it to a file and create a content processor.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9648489" width="1" height="1"&gt;</description></item><item><title>AvatarDescription.IsValid… When &amp; Why the description returned is invalid?</title><link>http://blogs.msdn.com/b/dejohn/archive/2009/05/20/avatardescription-isvalid-when-why-the-description-returned-is-invalid.aspx</link><pubDate>Wed, 20 May 2009 23:51:37 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9632864</guid><dc:creator>Dean Johnson - MSFT</dc:creator><slash:comments>4</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/dejohn/rsscomments.aspx?WeblogPostID=9632864</wfw:commentRss><comments>http://blogs.msdn.com/b/dejohn/archive/2009/05/20/avatardescription-isvalid-when-why-the-description-returned-is-invalid.aspx#comments</comments><description>&lt;p&gt;In previous posts I talked about how the AvatarDescription contains data to define what an avatar should look like so that the AvatarRenderer knows what assets to load in order to render the avatar. The AvatarDescription type contains a property called IsValid which is a read only bool. So what does this property tell the developer? It tells the developer if the description is valid of course! So when can a developer expect a description to be valid and invalid?&lt;/p&gt;  &lt;p&gt;First let’s go over the different ways a user can create an AvatarDescription object. &lt;/p&gt;  &lt;p&gt;Gamer.SignedInGamers[PlayerIndex.One].Avatar – Returns the avatar description from a gamers profile.&lt;/p&gt;  &lt;p&gt;AvatarDescription.CreateRandom() – Returns a randomly created avatar description.&lt;/p&gt;  &lt;p&gt;AvatarDescription(byte[] data) – Constructor that allows the developer to construct a description from a byte[] that was returned from the AvatarDescription.Description property of an already created AvatarDescription object. A future post will talk more about this constructor and why it is useful.&lt;/p&gt;  &lt;p&gt;Now for the rules on when descriptions are valid vs. invalid.&lt;/p&gt;  &lt;p&gt;· On Windows the profile and random avatar descriptions will return as invalid descriptions because Windows does not support avatars.&lt;/p&gt;  &lt;p&gt;· On Xbox the random avatar description will return valid descriptions.&lt;/p&gt;  &lt;p&gt;· On Xbox the profile avatar description can return both valid and invalid descriptions. If a user does not have an avatar associated with their profile then the avatar description returned will be invalid.&lt;/p&gt;  &lt;p&gt;Not too confusing right?&lt;/p&gt;  &lt;p&gt;So how do invalid AvatarDescriptions behave? &lt;/p&gt;  &lt;p&gt;The properties on the AvatarDescription such as the Height, BodyType, and Description return default values. IsValid returns false as you would expect.&lt;/p&gt;  &lt;p&gt;So what happens if you use an invalid AvatarDescription to create an AvatarRenderer? Does it throw an exception? No. You will get back an AvatarRenderer object. You can do everything that you would do normally with an AvatarRenderer created with a valid AvatarDescription. The difference is when you call draw nothing will be displayed to the screen. This was done so that while you develop your game on Windows you will not need to wrap all of your AvatarRenderer constructor or draw calls in try/catch statements. While the avatar will not draw on Windows there is also a chance that the users profile on the Xbox will also be invalid. The developer should detect that the game can’t display an avatar and do something appropriate. For example the developer may create a stand in model to use when a user doesn’t have a valid avatar description.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9632864" width="1" height="1"&gt;</description></item></channel></rss>