<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	>

<channel>
	<title>XNAGameDev</title>
	<atom:link href="http://xnagamedev.com/feed" rel="self" type="application/rss+xml" />
	<link>http://xnagamedev.com</link>
	<description>XNA Game Development At It's Finest</description>
	<pubDate>Tue, 04 Mar 2008 13:06:39 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.5.1</generator>
	<language>en</language>
			<item>
		<title>How to Animate Sprites via Sprite Sheets in XNA Game Studio Express</title>
		<link>http://xnagamedev.com/xna-basics/how-to-animate-sprites-via-sprite-sheets-in-xna-game-studio-express</link>
		<comments>http://xnagamedev.com/xna-basics/how-to-animate-sprites-via-sprite-sheets-in-xna-game-studio-express#comments</comments>
		<pubDate>Mon, 03 Mar 2008 00:21:57 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[Sprites]]></category>

		<category><![CDATA[XNA Basics]]></category>

		<category><![CDATA[3d game development]]></category>

		<category><![CDATA[computer game programming]]></category>

		<category><![CDATA[game develoment tools]]></category>

		<category><![CDATA[game development software]]></category>

		<category><![CDATA[game makers]]></category>

		<category><![CDATA[game programming tutorial]]></category>

		<category><![CDATA[online game development]]></category>

		<category><![CDATA[programming games]]></category>

		<category><![CDATA[video game development]]></category>

		<guid isPermaLink="false">http://xnagamedev.com/xna-basics/how-to-animate-sprites-via-sprite-sheets-in-xna-game-studio-express</guid>
		<description><![CDATA[So far we’ve dabbled with cheap sprite effects such as tint, opacity, or even rotation and scaling. But what we created wasn’t exactly professional, per se (and rightly so, we just begun learning XNA Game Studio Express!). But now the time has come to create a professional looking animation. In fact, we will be recreating [...]]]></description>
			<content:encoded><![CDATA[<p class="MsoNormal">So far we’ve dabbled with cheap sprite effects such as tint, opacity, or even rotation and scaling. But what we created wasn’t exactly professional, per se (and rightly so, we just begun learning XNA Game Studio Express!). But now the time has come to create a professional looking animation. In fact, we will be recreating a combination attack that consists of a total of 15 different images!<span id="more-26"></span></p>
<p style="border: 1px solid #c2d5de; padding: 3px; background-color: #e3eef3; color: black; text-align: center">Lost? View the previous section: <a href="http://xnagamedev.com/xna-basics/how-to-rotate-sprites-in-xna-game-studio-express">How to Rotate Sprites in XNA Game Studio Express</a></p>
<h3><strong><span style="font-size: 12pt; line-height: 115%">Theory and Prerequisites to Animating a Sprite Sheet in XNA Game Studio Express<o:p></o:p></span></strong></h3>
<p class="MsoNormal">So what is a sprite sheet, anyways? Sprite sheets are images that consist of multiple images. This single image is used to animate all sorts of things in games. In our example, we will be using one single image to animate 15 different frames of animation.</p>
<p class="MsoNormal">The image in question can be seen (and downloaded) below.</p>
<p><center><img src="http://xnagamedev.com/wp-content/uploads/spritesheet.jpg" alt="xna sprite sheet" /></center></p>
<p class="MsoNormal">You might have noticed that each image is evenly spaced from one image to the next. If we were to measure each individual image, we would find that each image is 50 pixels in width. If we know how many pixels each frame is, we can make a loop to scroll through each image- one at a time!</p>
<p class="MsoNormal">What is going to happen, specifically, is we will be moving a rectangle along the entire image. Only the part of the image that appears in the rectangle will be drawn on the screen. This allows us to scroll through a bunch of images for animation in a nifty little way.</p>
<h3><strong><span style="font-size: 12pt; line-height: 115%">How to Animate Sprites with Sprite Sheets in XNA Game Studio Express<o:p></o:p></span></strong></h3>
<p class="MsoNormal">If you don’t have the latest source code, you can get it <a href="http://xnagamedev.com/source/sprite4.txt">here</a>. Be sure you also have the SpriteSheet.jpg image shown above, and upload it to the XNA Content Pipeline like we have done with our sprites in previous sections.</p>
<p class="MsoNormal">Directly below the <strong>Game1 </strong>class, we need to add a variable for our sprite sheet. The code below will do just that.</p>
<h4>Declaring The Texture Variable for The Sprite Sheet</h4>
<pre>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'; color: gray"><span>        </span>//Load Character Texture<span>      </span>*XNAGameDev.com<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'; color: gray"><span>        </span>protected Texture2D texture_mcharacter = null;<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'; color: gray"><span>        </span>protected Texture2D texture_shuriken = null;<o:p></o:p></span>
<p class="MsoNormal"><span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'"><span>        </span><span style="color: blue">protected</span> <span style="color: #2b91af">Texture2D</span> texture_spritesheet = <span style="color: blue">null</span>;<o:p></o:p></span>
</pre>
<p class="MsoNormal"><span>With our new variable, <em>texture_spritesheet</em>, in place we can load the sprite into memory in our <strong>LoadGraphicsContent</strong> method.</span></p>
<h4>Loading a Sprite Sheet into Memory with XNA Game Studio Express</h4>
<pre>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'; color: gray"><span>                </span>// load our textures<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'; color: gray"><span>                </span>texture_mcharacter = content.Load&lt;Texture2D&gt;(&#8221;sprite1_transp&#8221;);<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'; color: gray"><span>                </span>texture_shuriken = content.Load&lt;Texture2D&gt;(&#8221;sprite_shuriken&#8221;);<o:p></o:p></span>
<p class="MsoNormal"><span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'"><span>                </span>texture_spritesheet<span>  </span>= content.Load&lt;<span style="color: #2b91af">Texture2D</span>&gt;(<span style="color: #a31515">&#8220;SpriteSheet&#8221;</span>);<o:p></o:p></span>
</pre>
<p class="MsoNormal"><span>Now the real fun begins! We’ll have to go through our <strong>Update</strong> method and ensure that the proper logic is created to scroll through our SpriteSheet.jpg image. Before we do so, we have eight new variables to declare just above the <strong>Update</strong> method.</span></p>
<h4>Necessary Variables for Sprite Sheet Animation in XNA Game Studio Express</h4>
<pre>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'; color: blue"><span>        </span>float</span><span style="font-size: 10pt; font-family: 'Courier New'"> Timer = 0f;<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span>        </span><span style="color: blue">float</span> Interval = 1000f / 10f;<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span>        </span><span style="color: blue">int</span> FrameCount = 15;<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span>        </span><span style="color: blue">int</span> CurrentFrame = 0;<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span>        </span><span style="color: blue">int</span> SpriteWidth = 50;<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span>        </span><span style="color: blue">int</span> SpriteHeight = 93;<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span>        </span><span style="color: #2b91af">Rectangle</span> SourceRect;<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span>        </span><span style="color: #2b91af">Rectangle</span> DestRect;<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span>        </span><span style="color: gray">private float RotationAngle = 0f;<o:p></o:p></span></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'; color: gray"><span>        </span>private Vector2 rotation_point;<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'; color: gray"><span>        </span>protected Rectangle ScaleSprite;<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'; color: gray"><span>        </span>protected double ScaleValueX = 161;<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'; color: gray"><span>        </span>protected double ScaleValueY = 266;<o:p></o:p></span>
<p class="MsoNormal"><span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: gray"><span>        </span>protected bool ScaleIncrement = true;</span><span style="color: gray"><o:p></o:p></span>
</pre>
<p class="MsoNormal">Our new timer variable will be keeping time in milliseconds, so we can’t use the timer from the last section. The interval, which is the time in between frame changes, is set at 10 frames per second. Our <em>FrameCount</em> holds the number of frames we have, and the next to variables hold the size of each image within the sprite sheet. Lastly, we have the <em>SourceRect</em> and <em>DestRect</em>, which are used to keep track of one rectangle to another.</p>
<p class="MsoNormal">As for the actual logic, we can see how to animate a sprite sheet with the below code in the <strong>Update</strong> method.</p>
<h4>IF Logic for Sprite Sheet Animation in XNA Game Studio Express</h4>
<pre>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span>            </span><span style="color: gray">// Allows the game to exit<o:p></o:p></span></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'; color: gray"><span>            </span>if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'; color: gray"><span>                </span>this.Exit();<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'; color: gray"><o:p> </o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'; color: gray"><span>            </span>// TODO: Add your update logic here<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'; color: green"><o:p> </o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span>            </span><span style="color: green">// Sprite sheet logic<o:p></o:p></span></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span>    </span><span>        </span>Timer += (<span style="color: blue">float</span>)gameTime.ElapsedGameTime.TotalMilliseconds;<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span>            </span><span style="color: blue">if</span> (Timer &gt; Interval)<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span>            </span>{<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span>             </span>CurrentFrame++;<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span>            </span><span style="color: blue">if</span> (CurrentFrame &gt; FrameCount - 1)<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span>             </span>{<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span>               </span>CurrentFrame = 0;<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span>             </span>}<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span>             </span>Timer = 0f;<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span>            </span>}<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span>            </span>SourceRect = <span style="color: blue">new</span> <span style="color: #2b91af">Rectangle</span>(CurrentFrame * SpriteWidth, 0, SpriteWidth, SpriteHeight);<o:p></o:p></span>
<p class="MsoNormal"><span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'"><span>            </span>DestRect = <span style="color: blue">new</span> <span style="color: #2b91af">Rectangle</span>(400, 400, 50, 93);<o:p></o:p></span>
</pre>
<p class="MsoNormal"><span>It’s a lot to take in at first, but rest assured, the logic is easy to understand. The first IF statement simply checks to see if the Timer is greater than the Interval- if it is, then it’s time for a new frame! The next IF statement checks to see if the <em>CurrentFrame</em> is larger than 15- if it is, than we reached the end of the image and need to restart! Finally, we set the Timer to 0 when this does indeed happen.<o:p></o:p></span></p>
<p class="MsoNormal"><span>We update our <em>SourceRect</em> and <em>DestRect</em> immediately following this code. The <em>SourceRect</em> has a variable X value, meaning it changes based on time. The <em>DestRect</em> is actually static- meaning it doesn’t ever change. (This is where we draw the sprite on the screen.)<o:p></o:p></span></p>
<p class="MsoNormal"><span>All that we need to do now is draw the sprite to screen! In our <strong>Draw</strong> method, let’s add a new item for our Sprite Batch to create.</span></p>
<h4>Using The Draw Method to Animate Sprite Sheets in XNA Game Studio Express</h4>
<pre>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'; color: gray"><span>            </span>s_batch.Draw(texture_mcharacter, loc3, OpacityValue);<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'; color: gray"><span>            </span>s_batch.Draw(texture_mcharacter, loc4, tint);<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'; color: gray"><span>            </span>s_batch.Draw(texture_shuriken, loc5,null, Color.White, RotationAngle, rotation_point, SpriteEffects.None, 0);<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span>          </span><span>  </span>s_batch.Draw(texture_spritesheet, DestRect, SourceRect, <span style="color: #2b91af">Color</span>.White);<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span>            </span><o:p></o:p></span>
<p class="MsoNormal"><span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: gray"><span>            </span>s_batch.End();</span><span style="color: gray"><o:p></o:p></span>
</pre>
<p class="MsoNormal">We should be familiar with how to draw a sprite to screen by now, but we are including a new variable we haven’t worked with before: the <em>DestRect</em>. This value is responsible for sectioning off the image into one rectangle. In this instance, we use this feature to section off a series of rectangles to make the illusion of animation.</p>
<p><center><img src="http://xnagamedev.com/wp-content/uploads/xna-sprite-sheet-animation.jpg" alt="xna sprite sheet animation" /></center></p>
<p class="MsoNormal">If all went well, you should have a very energetic Ryu throwing a heavy hit in a very smooth animation.</p>
<h3><strong><span style="font-size: 12pt; line-height: 115%">More Fun with Sprite Sheets and XNA Game Studio Express<o:p></o:p></span></strong></h3>
<p class="MsoNormal">As for a little fun, we can play with the timer and size options for a little amusement. Try out the following code found just above the <strong>Update</strong> method. (<em>The 10f is changed to a 100f</em>)</p>
<h4>Increasing Interval for Sprite Sheets in XNA Game Studio Express</h4>
<pre>
<p class="MsoNormal"><span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'"><span>        </span><span style="color: blue">float</span> Interval = 1000f / 100f;</span>
</pre>
<p class="MsoNormal">Instead of Ryu the amazing fighter, we now have a DDR master. Now revert your changes and try the following code.</p>
<h4>Increasing Sprite Width for the Sprite Sheet in XNA Game Studio Express</h4>
<pre>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span>        </span><span style="color: gray">float Interval = 1000f / 10f;<o:p></o:p></span></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span>        </span><span style="color: gray">int FrameCount = 15;<o:p></o:p></span></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'; color: gray"><span>        </span>int CurrentFrame = 0;<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span>   </span><span>     </span><span style="color: blue">int</span> SpriteWidth = 60;<o:p></o:p></span>
<p class="MsoNormal"><span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'"><span>        </span><span style="color: blue">int</span> SpriteHeight = 93;<o:p></o:p></span>
</pre>
<p class="MsoNormal"><span>All we changed was the <em>SpriteWidth</em>. Notice how Ryu actually has an appearance of moving now! Since our width doesn’t fit the image rectangle right, it shows part of the next image. This gives the illusion of Ryu moving.<o:p></o:p></span></p>
<p class="MsoNormal"><span>In the next section we will be taking a look at depth. When you select a character in some fighting games, there are nifty little animations present. But we’ll take it a step further and implement what we’ve learned so far to create a rather impressive character select screen! (And we’ll be using a new project next section- so you can keep the finished code thus far in a safe place for future reference)<o:p></o:p></span></p>
]]></content:encoded>
			<wfw:commentRss>http://xnagamedev.com/xna-basics/how-to-animate-sprites-via-sprite-sheets-in-xna-game-studio-express/feed</wfw:commentRss>
		</item>
		<item>
		<title>How to Rotate Sprites in XNA Game Studio Express</title>
		<link>http://xnagamedev.com/xna-basics/how-to-rotate-sprites-in-xna-game-studio-express</link>
		<comments>http://xnagamedev.com/xna-basics/how-to-rotate-sprites-in-xna-game-studio-express#comments</comments>
		<pubDate>Sun, 02 Mar 2008 19:23:39 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[Sprites]]></category>

		<category><![CDATA[XNA Basics]]></category>

		<category><![CDATA[3d game development]]></category>

		<category><![CDATA[computer game programming]]></category>

		<category><![CDATA[game develoment tools]]></category>

		<category><![CDATA[game development software]]></category>

		<category><![CDATA[game makers]]></category>

		<category><![CDATA[game programming tutorial]]></category>

		<category><![CDATA[online game development]]></category>

		<category><![CDATA[programming games]]></category>

		<category><![CDATA[video game development]]></category>

		<guid isPermaLink="false">http://xnagamedev.com/xna-basics/how-to-rotate-sprites-in-xna-game-studio-express</guid>
		<description><![CDATA[We’ve finally gotten to the stage where we can create a fully animated sprite in XNA Game Studio Express. But before we get into the specifics of frame-by-frame animation (which we cover in the next section), we will be taking a look at how to rotate our very own shuriken for weapon animation. (And more [...]]]></description>
			<content:encoded><![CDATA[<p class="MsoNormal">We’ve finally gotten to the stage where we can create a fully animated sprite in XNA Game Studio Express. But before we get into the specifics of frame-by-frame animation (which we cover in the next section), we will be taking a look at how to rotate our very own shuriken for weapon animation. (And more importantly- we will be genetically altering our Ryu sprite to have a blood stained shuriken for a hand- just because we can!)<span id="more-23"></span></p>
<p style="border: 1px solid #c2d5de; padding: 3px; background-color: #e3eef3; color: black; text-align: center">Lost? View the previous section: <a href="http://xnagamedev.com/xna-basics/how-to-set-opacity-and-scale-sprites-in-xna-game-studio-express">How to Set Opacity and Scale Sprites in XNA Game Studio Express</a></p>
<h3><strong><span style="font-size: 12pt; line-height: 115%">How to Rotate a Sprite in XNA Game Studio Express<o:p></o:p></span></strong></h3>
<p class="MsoNormal">If a ninja throws a shuriken, the shuriken should rotate in flight for realistic effect. (And if you are somehow fortunate to enough to have a shuriken implant for a hand- it should be rotating at all times for best use.) Below is a shuriken sprite in the PNG format, which allows for transparency. Go ahead and download it now.</p>
<p><center><img src="http://xnagamedev.com/wp-content/uploads/sprite_shuriken.png" alt="xna sprite rotate" /></center></p>
<p class="MsoNormal">Add the above image, named <em>shuriken_sprite</em>, to the content pipeline in XNA Game Studio Express. Before we get any further, make sure you have the latest update to our source code, which can be found <a href="http://xnagamedev.com/source/sprite3.txt">here</a>.</p>
<p class="MsoNormal">We’ll start out by declaring our new sprite right after the Game1 class in our XNA code. Look at the following code to see what we added.</p>
<h4>Declaring The Texture Variable in XNA Game Studio Express</h4>
<pre>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'; color: gray"><span>    </span>public class Game1 : Microsoft.Xna.Framework.Game<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'; color: gray"><span>    </span>{<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'; color: gray"><span>        </span>GraphicsDeviceManager graphics;<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'; color: gray"><span>        </span>ContentManager content;<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'; color: gray"><o:p> </o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'; color: gray"><span>        </span>//Make Our Sprite Batch<span>      </span>*XNAGameDev.com<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'; color: gray"><span>        </span>protected SpriteBatch s_batch = null;<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'; color: gray"><o:p> </o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'; color: gray"><span>        </span>//Load Character Texture<span>      </span>*XNAGameDev.com<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'; color: gray"><span>        </span>protected Texture2D texture_mcharacter = null;<o:p></o:p></span>
<p class="MsoNormal"><span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'"><span>        </span><span style="color: blue">protected</span> <span style="color: #2b91af">Texture2D</span> texture_shuriken = <span style="color: blue">null</span>;</span>
</pre>
<p class="MsoNormal">The <em>texture_shuriken</em> texture will be used to assign the sprite we just uploaded to our XNA Content Pipeline to a variable. Since we already uploaded the sprite to the Content Pipeline, we can continue to make a reference to it within the <strong>LoadGraphicsContent</strong> method as seen below.</p>
<h4>Loading a Sprite into Memory with XNA Game Studio Express</h4>
<pre>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'; color: gray"><span>        </span>protected override void LoadGraphicsContent(bool loadAllContent)<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'; color: gray"><span>        </span>{<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'; color: gray"><span>            </span>if (loadAllContent)<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'; color: gray"><span>            </span>{<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'; color: gray"><span>                </span>// TODO: Load any ResourceManagementMode.Automatic content<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'; color: gray"><span>                </span>// initialize our sprite batch<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'; color: gray"><span>                </span>s_batch = new SpriteBatch(graphics.GraphicsDevice);<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'; color: gray"><o:p> </o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'; color: gray"><span>                </span>// load our textures<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'; color: gray"><span>                </span>texture_mcharacter = content.Load&lt;Texture2D&gt;(&#8221;sprite1_transp&#8221;);<o:p></o:p></span>
<p class="MsoNormal"><span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'"><span>                </span>texture_shuriken = content.Load&lt;<span style="color: #2b91af">Texture2D</span>&gt;(<span style="color: #a31515">&#8220;sprite_shuriken&#8221;</span>);<o:p></o:p></span>
</pre>
<p class="MsoNormal">You’ll notice that all we added was a reference to the <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: #a40000">sprite_shuriken</span> sprite- nothing else. Now we need to work with the XNA <strong>Update</strong> method to actually move the sprite. To accomplish this, we will move the sprite based on a timer that ticks every time the <strong>Update </strong>method is called. Before we get started, however, we need to declare two new variables above our <strong>Update</strong> method to help us out.</p>
<h4>Rotation Variables in XNA Game Studio Express</h4>
<pre>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'; color: blue">private</span><span style="font-size: 10pt; font-family: 'Courier New'"> <span style="color: blue">float</span> RotationAngle = 0f;<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'; color: blue">private</span><span style="font-size: 10pt; font-family: 'Courier New'"> <span style="color: #2b91af">Vector2</span> rotation_point;<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'; color: gray">protected Rectangle ScaleSprite;<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'; color: gray">protected double ScaleValueX = 161;<o:p></o:p></span>
<p class="MsoNormal"><span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: gray">protected double ScaleValueY = 266;<o:p></o:p></span>
</pre>
<p class="MsoNormal">Next, let’s put in our code to calculate the angle and rotation. We placed it in front of our previous tinting code for easier reading.</p>
<h4>Rotation Logic Code in XNA Game Studio Express</h4>
<pre>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'; color: gray">protected override void Update(GameTime gameTime)<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'; color: gray"><span>        </span>{<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'; color: gray"><span>            </span>// Allows the game to exit<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'; color: gray"><span>            </span>if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'; color: gray"><span>                </span>this.Exit();<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'; color: gray"><o:p> </o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'; color: gray"><span>            </span>// TODO: Add your update logic here<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'; color: green"><o:p> </o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span>            </span><span style="color: green">// elapsed time<o:p></o:p></span></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span>            </span><span style="color: blue">float</span> elapsed = (<span style="color: blue">float</span>)gameTime.ElapsedGameTime.TotalSeconds;<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><o:p> </o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span>            </span><span style="color: green">//rotation<o:p></o:p></span></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span>            </span>RotationAngle += elapsed;<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span>            </span><span style="color: blue">float</span> circle = <span style="color: #2b91af">MathHelper</span>.Pi * 2;<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span>            </span>RotationAngle = RotationAngle % circle;<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span>            </span>rotation_point.X = texture_shuriken.Width / 2;<o:p></o:p></span>
<p class="MsoNormal"><span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'"><span>            </span>rotation_point.Y = texture_shuriken.Height / 2;<o:p></o:p></span>
</pre>
<p class="MsoNormal">The <em>elapsed</em> variable obtains the necessary assignment to count the amount of seconds that have elapsed in our game. In our <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: green">//rotation</span> code, we increment the <em>RotationAngle</em> by the elapsed time each tick. We then use a little geometry math to put the number into a corresponding angle. Finally, the <em>roation_point</em> variable stores the center point of the sprite- which will be our point of rotation.</p>
<p class="MsoNormal">Lastly, we need to actually draw our sprite to the screen. We achieve this by declaring a new rectangle and then using our Sprite Batch to output the results to the screen.</p>
<h4>Drawing The Rotating Sprite to The Screen in XNA Game Studio Express</h4>
<pre>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'; color: gray">// TODO: Add your drawing code here<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span>            </span><span style="color: gray">s_batch.Begin();<o:p></o:p></span></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'; color: gray"><span>            </span>Vector2 loc = new Vector2(50, 50);<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'; color: gray"><span>            </span>Vector2 loc2 = new Vector2(200, 50);<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'; color: gray"><span>            </span>Vector2 loc3 = new Vector2(350, 50);<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'; color: gray"><span>            </span>Vector2 loc4 = new Vector2(500, 50);<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span>            </span><span style="color: #2b91af">Rectangle</span> loc5 = <span style="color: blue">new</span> <span style="color: #2b91af">Rectangle</span>(195, 133, 50,49);<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span>            </span><span style="color: gray">s_batch.Draw(texture_mcharacter, loc, Color.White);<o:p></o:p></span></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'; color: gray"><span>            </span>s_batch.Draw(texture_mcharacter, ScaleSprite, null, Color.White, 0, rot, SpriteEffects.None,0);<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'; color: gray"><span>            </span>s_batch.Draw(texture_mcharacter, loc3, OpacityValue);<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'; color: gray"><span>            </span>s_batch.Draw(texture_mcharacter, loc4, tint);<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span>            </span>s_batch.Draw(texture_shuriken, loc5,<span style="color: blue">null</span>, <span style="color: #2b91af">Color</span>.White, RotationAngle, rotation_point, <span style="color: #2b91af">SpriteEffects</span>.None, 0);<o:p></o:p></span>
<p class="MsoNormal"><span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: gray"><span>            </span>s_batch.End();<o:p></o:p></span>
</pre>
<p class="MsoNormal"><span style="color: black">The rectangle, <em>loc5</em>, is put at the location of (195, 133)- which just so happens to be Ryu’s hand. The actual size of the sprite is the following numbers, (50, 49). Further down we use our Sprite Batch to draw the texture, specify the location, and then pass the <em>RotationAngle</em> and <em>rotation_point</em> variables. We now have a working rotated sprite- which coincidentally serves as Ryu’s frightening shuriken hand.<o:p></o:p></span></p>
<p><center><img src="http://xnagamedev.com/wp-content/uploads/xna-rotation-sprite.jpg" alt="xna sprite rotation" /></center></p>
<p class="MsoNormal"><span style="color: black">Ok, so it isn’t exactly frightening. But you have to admit, the smooth rotation the shuriken follows is mesmerizing. Beautiful, in fact!<o:p></o:p></span></p>
<p class="MsoNormal"><span style="color: black">If we were deadset on creating a killer buzzsaw for an arm, we could definitely do so. But we can focus on specifics down the road- in the next section we will be learning how to animate a sprite using what is called a sprite sheet. This uses a frame-by-frame animation that is actually only one image file! Specifically, we will be using a sprite sheet to create a fully animated (and devastating) attack for Ryu. (As if a shuriken hand wasn’t enough!)<o:p></o:p></span></p>
]]></content:encoded>
			<wfw:commentRss>http://xnagamedev.com/xna-basics/how-to-rotate-sprites-in-xna-game-studio-express/feed</wfw:commentRss>
		</item>
		<item>
		<title>How to Set Opacity and Scale Sprites in XNA Game Studio Express</title>
		<link>http://xnagamedev.com/xna-basics/how-to-set-opacity-and-scale-sprites-in-xna-game-studio-express</link>
		<comments>http://xnagamedev.com/xna-basics/how-to-set-opacity-and-scale-sprites-in-xna-game-studio-express#comments</comments>
		<pubDate>Thu, 28 Feb 2008 05:09:07 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[Sprites]]></category>

		<category><![CDATA[XNA Basics]]></category>

		<category><![CDATA[3d game development]]></category>

		<category><![CDATA[computer game programming]]></category>

		<category><![CDATA[game develoment tools]]></category>

		<category><![CDATA[game development software]]></category>

		<category><![CDATA[game makers]]></category>

		<category><![CDATA[game programming tutorial]]></category>

		<category><![CDATA[online game development]]></category>

		<category><![CDATA[programming games]]></category>

		<category><![CDATA[video game development]]></category>

		<guid isPermaLink="false">http://xnagamedev.com/xna-basics/how-to-set-opacity-and-scale-sprites-in-xna-game-studio-express</guid>
		<description><![CDATA[We got our feet wet with tinting sprites in the previous section- but we still haven’t created anything too stunning. In this section we will take a look at setting the opacity, and even learn how to scale sprites! Specifically, we will focus on giving Ryu a slight oscillating size; this will give the effect [...]]]></description>
			<content:encoded><![CDATA[<p class="MsoNormal">We got our feet wet with tinting sprites in the previous section- but we still haven’t created anything too stunning. In this section we will take a look at setting the opacity, and even learn how to scale sprites! Specifically, we will focus on giving Ryu a slight oscillating size; this will give the effect of him breathing. And if things couldn’t get cooler, we’ll add some invisibility to the mix.<span id="more-20"></span></p>
<p style="border: 1px solid #c2d5de; padding: 3px; background-color: #e3eef3; color: black; text-align: center">Lost? View the previous section: <a href="http://xnagamedev.com/xna-basics/how-to-tint-sprites-in-xna-game-studio-express">How to Tint Sprites in XNA Game Studio Express</a></p>
<h3><strong><span style="font-size: 12pt; line-height: 115%">How to Set Opacity with Sprites in XNA Game Studio Express<o:p></o:p></span></strong></h3>
<p class="MsoNormal">First, make sure you have the latest version of our code that can be found <a href="http://xnagamedev.com/source/sprite2.txt">here</a>. You’ll find that we have already dabbled with tint, which is actually related to opacity settings. If you remember correctly, the opacity setting can be set within the Color value we created. (The last number in the example below; found in the <strong>Update</strong> method)</p>
<h4>The Alpha (Opacity) Value in XNA Game Studio Express</h4>
<pre>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'">tint = <span style="color: blue">new</span> <span style="color: #2b91af">Color</span>(255, <span style="color: #2b91af">Convert</span>.ToByte(GreenBlueValue), <span style="color: #2b91af">Convert</span>.ToByte(GreenBlueValue), 100);<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><o:p> </o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><o:p> </o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span style="color: blue">base</span>.Update(gameTime);<o:p></o:p></span>
<p class="MsoNormal"><span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'">}<o:p></o:p></span>
</pre>
<p class="MsoNormal"><span>In the above example we are setting the tint of the sprite that we used in the last section with the <em>GreenBlueValue</em> variable. Take note that our last number, 100, is our opacity value (actually called <em>alpha</em>). We’ll be making the third sprite in our example oscillate from opaque to transparent- all without adding more logic code!<o:p></o:p></span></p>
<p class="MsoNormal"><span>Let’s add another declaration to the mix, called <em>OpacityValue</em>, directly above the <strong>Update</strong> method. We will also add a new line below the <em>tint</em> assignment statement- where we will configure our opacity values.<o:p></o:p></span></p>
<h4>Implementing Dynamic Opacity Values in XNA Game Studio Express</h4>
<pre>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'; color: blue">protected</span><span style="font-size: 10pt; font-family: 'Courier New'"> <span style="color: #2b91af">Color</span> OpacityValue;<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'; color: blue">protected</span><span style="font-size: 10pt; font-family: 'Courier New'"> <span style="color: #2b91af">Color</span> tint;<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'; color: blue">protected</span><span style="font-size: 10pt; font-family: 'Courier New'"> <span style="color: blue">int</span> GreenBlueValue = 255;<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'; color: blue">protected</span><span style="font-size: 10pt; font-family: 'Courier New'"> <span style="color: blue">bool</span> IncrementCount = <span style="color: blue">false</span>;<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'; color: blue">protected</span><span style="font-size: 10pt; font-family: 'Courier New'"> <span style="color: blue">override</span> <span style="color: blue">void</span> Update(<span style="color: #2b91af">GameTime</span> gameTime)<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'">{<o:p></o:p></span>
<p class="MsoNormal"><span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'"><span>   </span><span style="color: green">// Allows the game to exit<o:p></o:p></span></span>
<p class="MsoNormal"><span style="color: #0d0d0d">Now for the opacity configuration, near the bottom of the Update method:<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span>            </span>tint = <span style="color: blue">new</span> <span style="color: #2b91af">Color</span>(255, <span style="color: #2b91af">Convert</span>.ToByte(GreenBlueValue), <span style="color: #2b91af">Convert</span>.ToByte(GreenBlueValue), 100);<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span>            </span>OpacityValue = <span style="color: blue">new</span> <span style="color: #2b91af">Color</span>(255, 255, 255, <span style="color: #2b91af">Convert</span>.ToByte(GreenBlueValue));<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><o:p> </o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><o:p> </o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span>            </span><span style="color: blue">base</span>.Update(gameTime);<o:p></o:p></span>
<p class="MsoNormal"><span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'"><span>        </span>}<o:p></o:p></span>
</pre>
<p class="MsoNormal"><span style="color: #0d0d0d">Notice that we set the RGB color values back to 255- this means there will be no tinting. Instead we only control the alpha value via our <em>GreenBlueValue</em> variable. Note that we are only reusing this variable- if we weren’t so lazy we could implement a counter for the opacity. For now, let’s continue reusing the <em>GreenBlueValue</em> for the sake of less work. <o:p></o:p></span></p>
<p class="MsoNormal"><span style="color: #0d0d0d">Lastly, we need to do one more thing: pass a reference to the <em>OpacityValue</em> in our <strong>Draw</strong> method. You can see our changes in the code below.<o:p></o:p></span></p>
<h4>Passing The Opacity Value to the Draw Method</h4>
<pre>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span>            </span>s_batch.Draw(texture_mcharacter, loc3, OpacityValue);<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span>            </span>s_batch.Draw(texture_mcharacter, loc4, tint);<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span>            </span>s_batch.End();<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><o:p> </o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span>            </span><span style="color: blue">base</span>.Draw(gameTime);<o:p></o:p></span>
<p class="MsoNormal"><span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'"><span>        </span>}<o:p></o:p></span>
</pre>
<p class="MsoNormal"><span style="color: #0d0d0d">Now we can run the project- go ahead and try it! If all went well, you should have an XNA project similar to the one below.<o:p></o:p></span></p>
<p><center><br />
<img src="http://xnagamedev.com/wp-content/uploads/xna-opacity-sprite-example.jpg" /></center></p>
<p class="MsoNormal"><span style="color: #0d0d0d">The third sprite now oscillates in opacity, and the fourth sprite oscillates in tint. Now we only have one more thing to cover: sizing the second sprite to make it appear as if he is breathing!<o:p></o:p></span></p>
<h3><strong><span style="font-size: 12pt; line-height: 115%; color: #0d0d0d">How to Scale a Sprite in XNA Game Studio Express<o:p></o:p></span></strong></h3>
<p class="MsoNormal"><span style="color: #0d0d0d">This time around we’ll be working with the second sprite. We will scale the X and Y coordinates of the image to make it appear as if our sprite Ryu is breathing (or at least has a bad case of ADD). To start out, let’s declare our variables before the <strong>Update</strong> method.<o:p></o:p></span></p>
<h4>Declaring Variables for Scaling Our Sprite in XNA Game Studio Express</h4>
<pre>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'; color: blue">protected</span><span style="font-size: 10pt; font-family: 'Courier New'"> <span style="color: #2b91af">Rectangle</span> ScaleSprite;<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'; color: blue">protected</span><span style="font-size: 10pt; font-family: 'Courier New'"> <span style="color: blue">double</span> ScaleValueX = 161;<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'; color: blue">protected</span><span style="font-size: 10pt; font-family: 'Courier New'"> <span style="color: blue">double</span> ScaleValueY = 266;<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'; color: blue">protected</span><span style="font-size: 10pt; font-family: 'Courier New'"> <span style="color: blue">bool</span> ScaleIncrement = <span style="color: blue">true</span>;<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'; color: blue">protected</span><span style="font-size: 10pt; font-family: 'Courier New'"> <span style="color: #2b91af">Color</span> OpacityValue;<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'; color: blue">protected</span><span style="font-size: 10pt; font-family: 'Courier New'"> <span style="color: #2b91af">Color</span> tint;<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'; color: blue">protected</span><span style="font-size: 10pt; font-family: 'Courier New'"> <span style="color: blue">int</span> GreenBlueValue = 255;<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'; color: blue">protected</span><span style="font-size: 10pt; font-family: 'Courier New'"> <span style="color: blue">bool</span> IncrementCount = <span style="color: blue">false</span>;<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'; color: blue">protected</span><span style="font-size: 10pt; font-family: 'Courier New'"> <span style="color: blue">override</span> <span style="color: blue">void</span> Update(<span style="color: #2b91af">GameTime</span> gameTime)<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span>        </span>{<o:p></o:p></span>
<p class="MsoNormal"><span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'"><span>            </span><span style="color: green">// Allows the game to exit<o:p></o:p></span></span>
</pre>
<p class="MsoNormal"><span style="color: #0d0d0d">We have four new variables to work with. The first variable, <em>ScaleSprite</em>, is our rectangle that will replace <em>loc2</em> as our location. Instead of just a location, this rectangle will define both X and Y values yet also width and height. (We will be using this in the <strong>Draw</strong> method later)<o:p></o:p></span></p>
<p class="MsoNormal"><span style="color: #0d0d0d">The <em>ScaleValueX</em> and <em>ScaleValueY</em> variables will be used to alter the height and width of the image. Notice that we predefine both variables as the image’s default width and height. We also have the <em>ScaleIncrement</em> variable that will let us switch between scaling up and scaling down. Look at the following logic code to get a better idea of how we’ll achieve the sprite scaling. (Place this code after the tint logic code)<o:p></o:p></span></p>
<h4>Scaling Logic Assembled in IF Statements</h4>
<pre>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'; color: green">//Logic to scale<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span>            </span><span style="color: blue">if</span> (ScaleIncrement == <span style="color: blue">true</span>)<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span>            </span>{<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span>                </span>ScaleValueX += 0.04;<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span>                </span>ScaleValueY += 0.01;<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span>            </span>}<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span>            </span><span style="color: blue">else<o:p></o:p></span></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span>            </span>{<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span>                </span>ScaleValueX -= 0.04;<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span>                </span>ScaleValueY -= 0.01;<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span>            </span>}<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span>            </span><span style="color: blue">if</span> (ScaleValueX &gt; 162)<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span>            </span>{<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span>                </span>ScaleIncrement = <span style="color: blue">false</span>;<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span>            </span>}<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span>            </span><span style="color: blue">else</span> <span style="color: blue">if</span> (ScaleValueX &lt; 161)<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span>            </span>{<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span>                </span>ScaleIncrement = <span style="color: blue">true</span>;<o:p></o:p></span>
<p class="MsoNormal"><span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'"><span>            </span>}<o:p></o:p></span>
</pre>
<p class="MsoNormal"><span style="color: #0d0d0d">This setup is extremely similar to the tinting IF logic that we placed this code after. Only this time we are incrementing and decrementing two values at different paces. Since characters don’t usually gain height when they breathe, we leave the Y value less affected than the X value.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="color: #0d0d0d">Now we can get back to the rectangle we mentioned earlier. We will be passing values to it below. Also notice that we tidied up the code before it so as to create a sense of organization.<o:p></o:p></span></p>
<h4>Implementing Our Rectangle (And Organizing our Code!)</h4>
<pre>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'; color: green">// Tint &amp; Opacity Assignments<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span>            </span>tint = <span style="color: blue">new</span> <span style="color: #2b91af">Color</span>(255, <span style="color: #2b91af">Convert</span>.ToByte(GreenBlueValue), <span style="color: #2b91af">Convert</span>.ToByte(GreenBlueValue), 100);<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span>            </span>OpacityValue = <span style="color: blue">new</span> <span style="color: #2b91af">Color</span>(255, 255, 255, <span style="color: #2b91af">Convert</span>.ToByte(GreenBlueValue));<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><o:p> </o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'; color: green">// Scaling assignment<o:p></o:p></span>
<p class="MsoNormal"><span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'"><span>            </span>ScaleSprite<span>  </span>= <span style="color: blue">new</span> <span style="color: #2b91af">Rectangle</span>(200, 50, <span style="color: #2b91af">Convert</span>.ToInt32(ScaleValueX) , <span style="color: #2b91af">Convert</span>.ToInt32(ScaleValueY));</span><span style="color: #0d0d0d"><o:p></o:p></span>
</pre>
<p class="MsoNormal"><span style="color: #0d0d0d">The rectangle works via (X value, Y value, Width, Height). Note that the width and height values have to be integers- so we use the conversion function to prevent errors while going from a </span><span style="color: blue">double</span><span style="color: #0d0d0d"> to an </span><span style="color: blue">int</span><span style="color: #0d0d0d">. With our new location and size aspects in hand, we can go to the <strong>Draw</strong> method and get this beast started!<o:p></o:p></span></p>
<p class="MsoNormal"><span style="color: #0d0d0d">We need to add one declaration before the <strong>Draw</strong> method before we begin.<o:p></o:p></span></p>
<h4>Setting The Point of Rotation in XNA Game Studio Express</h4>
<pre>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'; color: #2b91af">Vector2</span><span style="font-size: 10pt; font-family: 'Courier New'"> rot = <span style="color: blue">new</span> <span style="color: #2b91af">Vector2</span>(0, 0);<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span>        </span><span style="color: blue">protected</span> <span style="color: blue">override</span> <span style="color: blue">void</span> Draw(<span style="color: #2b91af">GameTime</span> gameTime)<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span>        </span>{<o:p></o:p></span>
<p class="MsoNormal"><span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'"><span>            </span>graphics.GraphicsDevice.Clear(<span style="color: #2b91af">Color</span>.White);<o:p></o:p></span>
</pre>
<p class="MsoNormal"><span>This new declaration is short for <em>rotation</em>. Right now, we haven’t dealt with rotating sprites, so don’t worry about it just yet. We only declare it now so that the compiler knows where the rotation should take place if we do intend on rotating the sprite (which we don’t- yet). </span><span style="color: #0d0d0d"><o:p></o:p></span></p>
<p class="MsoNormal"><span style="color: #0d0d0d">The following is what you should be part of your Draw method thus far.<o:p></o:p></span></p>
<h4>The Completed Draw Method</h4>
<pre>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'">s_batch.Draw(texture_mcharacter, loc, <span style="color: #2b91af">Color</span>.White);<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'">s_batch.Draw(texture_mcharacter, ScaleSprite , <span style="color: blue">null</span>, <span style="color: #2b91af">Color</span>.White, 0, rot, <span style="color: #2b91af">SpriteEffects</span>.None,0);<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'">s_batch.Draw(texture_mcharacter, loc3, OpacityValue);<o:p></o:p></span>
<p class="MsoNormal"><span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'">s_batch.Draw(texture_mcharacter, loc4, tint);<o:p></o:p></span>
</pre>
<p class="MsoNormal"><span style="color: #0d0d0d">Take a particular note on the second line- this is the sprite we are affecting. It accepts values via “Sprite name, Rectangle (the one we created earlier!), an attribute that allows us to section our rectangle into pieces (leave it null to keep the full sprite), tint, rotation angle, point of rotation, sprite effects, and depth.”<o:p></o:p></span></p>
<p class="MsoNormal"><span style="color: #0d0d0d">Don’t worry if the arguments these functions take confuses you- we’ll get into this more in later sections. For now, just make sure that you understand the role of ScaleSprite in the operation. After you feel you are comfortable with the logic, run the game! You should be greeted by a living, breathing, game sprite aptly named Ryu.<o:p></o:p></span></p>
<p><center><br />
<img src="http://xnagamedev.com/wp-content/uploads/xna-sprite-scale.jpg" /></center></p>
<p class="MsoNormal"><span style="color: #0d0d0d">If you can’t see much of a difference- it’s because we only scaled our sprite enough to see subtle breathing. For fun, try quickening the addition and subtraction rate. Also try taking out the maximum size aspect- and watch Ryu grow into Godzilla!<o:p></o:p></span></p>
<p class="MsoNormal"><span style="color: #0d0d0d">We aren’t exactly to the point where professional standards are met- but we’re getting close! We’ve already created a breathing sprite with just a few lines of code: Imagine what we can create in the coming sections!<o:p></o:p></span></p>
<p class="MsoNormal"><span style="color: #0d0d0d">By now we have four sprites that exhibit different traits. The plain Ryu, breathing Ryu, invisible Ryu, and heated Ryu. Next up: rotating a weapon sprite for Ryu to use!<o:p></o:p></span></p>
]]></content:encoded>
			<wfw:commentRss>http://xnagamedev.com/xna-basics/how-to-set-opacity-and-scale-sprites-in-xna-game-studio-express/feed</wfw:commentRss>
		</item>
		<item>
		<title>How to Tint Sprites in XNA Game Studio Express</title>
		<link>http://xnagamedev.com/xna-basics/how-to-tint-sprites-in-xna-game-studio-express</link>
		<comments>http://xnagamedev.com/xna-basics/how-to-tint-sprites-in-xna-game-studio-express#comments</comments>
		<pubDate>Tue, 26 Feb 2008 20:36:18 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[Sprites]]></category>

		<category><![CDATA[XNA Basics]]></category>

		<category><![CDATA[3d game development]]></category>

		<category><![CDATA[C#]]></category>

		<category><![CDATA[computer game programming]]></category>

		<category><![CDATA[game design]]></category>

		<category><![CDATA[game develoment tools]]></category>

		<category><![CDATA[game development software]]></category>

		<category><![CDATA[game makers]]></category>

		<category><![CDATA[game programming tutorial]]></category>

		<category><![CDATA[online game development]]></category>

		<category><![CDATA[pc game development]]></category>

		<category><![CDATA[programming games]]></category>

		<category><![CDATA[video game development]]></category>

		<category><![CDATA[xbox 360 development]]></category>

		<category><![CDATA[xbox game development]]></category>

		<category><![CDATA[xbox programming]]></category>

		<category><![CDATA[XNA]]></category>

		<category><![CDATA[XNA Game Studio Exress]]></category>

		<guid isPermaLink="false">http://xnagamedev.com/uncategorized/how-to-tint-sprites-in-xna-game-studio-express</guid>
		<description><![CDATA[In the previous section we learned about how sprites are added and drawn in XNA Game Studio Express. Ryu (the name of the sprite in the previous section) couldn’t move, blink, talk, or anything more than stand perfectly still! And while just looking at the handsome Ryu can hold one’s attention span for a good [...]]]></description>
			<content:encoded><![CDATA[<p class="MsoNormal">In the previous section we learned about how sprites are added and drawn in XNA Game Studio Express. Ryu (the name of the sprite in the previous section) couldn’t move, blink, talk, or anything more than stand perfectly still! And while just looking at the handsome Ryu can hold one’s attention span for a good 10 seconds, beyond that we will need to add effects to Ryu. (If we are to conform to today’s attention deficit disorder standards)<span id="more-16"></span></p>
<p style="border: 1px solid #c2d5de; padding: 3px; background-color: #e3eef3; color: black; text-align: center">Lost? View the previous section: <a href="http://xnagamedev.com/xna-basics/how-to-add-a-sprite-in-xna-game-studio-express">How to Add a Sprite in XNA Game Studio Express</a></p>
<h3>Prerequisites to Our XNA Tinting Tutorial</h3>
<p class="MsoNormal">What if Ryu got bashed in the face with a baseball bat? Not just a tap- but a full on knockout! Our character is bound to get a little red in the face. We can tint Ryu with a little blush to show his pain (or anger) from the blow rather easily in XNA.</p>
<p class="MsoNormal">First you will need the source code from the previous section, which can be found <a href="http://xnagamedev.com/source/sprite1.txt">here</a>. Next, you will need to delete the sprite we used in the previous section. Remember how we said the sprite wasn’t transparent? Well if we tinted the sprite, whitespace would show! To counter this we need a transparent sprite- commonly saved in the PNG format as seen below. (Go ahead and download the following image!)</p>
<p><center><img src="http://xnagamedev.com/wp-content/uploads/sprite1_transp.png" alt="xna sprite transparent" /></center></p>
<p class="MsoNormal">Once you have uploaded the new sprite to XNA via the Content Pipeline, we will need to change a reference to the old texture in our previous code. Find and replace the code below located in the <strong>LoadGraphicsContent</strong> method.</p>
<h4>Syntax for Loading a Sprite in the XNA LoadGraphicsContent Method</h4>
<pre>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'; color: green">// *Old Code!*      *XNAGameDev.com<o:p></o:p></span>
<p class="MsoNormal"><span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'">texture_mcharacter = content.Load&lt;<span style="color: #2b91af">Texture2D</span>&gt;(<span style="color: #a31515">&#8220;sprite1&#8243;</span>);</span>
</pre>
<p class="MsoNormal"><span style="color: #0d0d0d">Simply replace  </span><em><span style="color: #a31515">sprite1 </span></em><span style="color: #0d0d0d">with the new filename, which should be </span><em><span style="color: #a31515">sprite1_transp</span></em><em>.</em><o:p></o:p></p>
<p>Now that we have our new transparent sprite, we need to draw more sprites onto the screen for testing. We will eventually be adding several effects to separate sprites- for now, let’s draw three more sprites with the following code. Look for additions to the following code found in the <strong>Draw</strong> method.</p>
<h4>Drawing Multiple Sprites with Vector2 and Sprite Batch</h4>
<pre>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'; color: green">// EDIT “loc” to have a location of (50,50)      *XNAGameDev.com<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'; color: green">// ADD loc2, loc3, and loc4 declarations, add correct locations!<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'; color: green">// ADD references to draw texture_mcharacter multiple times with s_batch.Draw<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'; color: green">// TODO: Add your drawing code here<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'">            s_batch.Begin();<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><o:p> </o:p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'">            <span style="color: #2b91af">Vector2</span> loc = <span style="color: blue">new</span> <span style="color: #2b91af">Vector2</span>(50, 50);<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'">            <span style="color: #2b91af">Vector2</span> loc2 = <span style="color: blue">new</span> <span style="color: #2b91af">Vector2</span>(200, 50);<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'">            <span style="color: #2b91af">Vector2</span> loc3 = <span style="color: blue">new</span> <span style="color: #2b91af">Vector2</span>(350, 50);<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'">            <span style="color: #2b91af">Vector2</span> loc4 = <span style="color: blue">new</span> <span style="color: #2b91af">Vector2</span>(500, 50);<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><o:p> </o:p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'">            s_batch.Draw(texture_mcharacter, loc, <span style="color: #2b91af">Color</span>.White);<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'">            s_batch.Draw(texture_mcharacter, loc2, <span style="color: #2b91af">Color</span>.White);<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'">            s_batch.Draw(texture_mcharacter, loc3, <span style="color: #2b91af">Color</span>.White);<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'">            s_batch.Draw(texture_mcharacter, loc4, <span style="color: #2b91af">Color</span>.White);<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><o:p> </o:p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'">            s_batch.End();<o:p></o:p></span>
</pre>
<p class="MsoNormal">When you run the new project, you will see multiple sprites drawn onto the screen as seen in the example below.</p>
<p><center><img src="http://xnagamedev.com/wp-content/uploads/xna-multiple-sprites.jpg" alt="xna multiple sprites" /></center></p>
<p class="MsoNormal">So far, it’s been rather easy. Just a few edits here and there. Now let’s look at an example of dynamically tinting a sprite in XNA!</p>
<h3>How to Tint a Sprite in XNA Game Studio Express</h3>
<p class="MsoNormal">Believe it or not, you already have experience with tinting sprites- you just didn’t know it! Remember when we viewed the following code in the <strong>Draw</strong> method:</p>
<h4>An Example of Tint in XNA&#8217;s Draw Method</h4>
<pre>
<span style="font-size: 10pt; font-family: 'Courier New'; color: green">// OLD code- just an example!    *XNAGameDev.com</span><span style="font-size: 10pt; font-family: 'Courier New'">
s_batch.Begin();</span>

<span style="font-size: 10pt; font-family: 'Courier New'; color: #2b91af">Vector2</span> loc = <span style="font-size: 10pt; font-family: 'Courier New'; color: blue">new</span> <span style="font-size: 10pt; font-family: 'Courier New'; color: #2b91af">Vector2</span>(100, 100);

<span style="font-size: 10pt; font-family: 'Courier New'">s_batch.Draw(texture_mcharacter, loc, </span><span style="font-size: 10pt; font-family: 'Courier New'; color: #2b91af">Color</span><span style="font-size: 10pt; font-family: 'Courier New'">.White);</span>
<span style="font-size: 10pt; font-family: 'Courier New'">s_batch.End();</span></pre>
<p>The <em>Color</em><em>.White</em> attribute is our tint! All we have to do is modify it to get the tint we need. Before we do anything, however, let’s modify our <strong>Update</strong> method so we can update the tint dynamically. The following declarations should be added above the <strong>Update</strong> method, as seen in the first three lines in the below example.</p>
<h4>Necessary Declarations for Tinting in XNA Game Studio Express</h4>
<pre><span style="color: blue">protected</span> <span style="color: #2b91af">Color</span> tint;<u1:p></u1:p>
<span style="color: blue">protected</span> <span style="color: blue">int</span> GreenBlueValue = 255;
<span style="color: blue">protected</span> <span style="color: blue">bool</span> IncrementCount = <span style="color: blue">false</span>;

<o:p> </o:p>
<span style="color: blue">protected</span> <span style="color: blue">override</span> <span style="color: blue">void</span> Update(<span style="color: #2b91af">GameTime</span> gameTime)
<u1:p></u1:p>
{
<u1:p></u1:p>
<span style="color: green"><span>    </span>// Allows the game to exit<u1:p></u1:p><o:p></o:p></span>
<span>     </span><span style="color: blue">if</span> (<span style="color: #2b91af">GamePad</span>.GetState(<span style="color: #2b91af">PlayerIndex</span>.One).Buttons.Back == <span style="color: #2b91af">ButtonState</span>.Pressed)
<u1:p></u1:p>
<span>         </span><span style="color: blue">this</span>.Exit();</pre>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal">The <em>protected</em><em> <span style="color: #2b91af">Color</span> tint; </em>is what we will be using to pass the tint value to our Draw method. For now, we can forget about it.</p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal">&nbsp;</p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><o:p> </o:p></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal">The <em>protected</em><em> <span style="color: blue">int</span> GreenBlueValue = 255; </em>line is what we will use to decrement the hexadecimal value of green and blue colors. (If you aren’t familiar with the RGB color system, it’s simple- three numbers ranged from 0 to 255 make up red, green, and blue colors.If we have 255 in every slot, we have the color white- which means no tint at all. So if we start out with white and decrement the blue and green numbers, we can fade to a red tint!)</p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal">&nbsp;</p>
<p>Lastly, we have the <span style="font-size: 10pt; font-family: 'Courier New'; color: blue">protected</span><span style="font-size: 10pt; font-family: 'Courier New'"> <span style="color: blue">bool</span> IncrementCount = <span style="color: blue">false</span>;</span><span style="font-size: 10pt; font-family: 'Courier New'"> </span>line. This line will act for our IF statement. If our tint gets too red, we will fade back to white. If we reach white again, we need to start the red tint again! This boolean value will allow us to do just that with an IF statement.</p>
<p class="MsoNormal"> Below we show the rest of our <strong>Update</strong> method code; take a look!</p>
<h4>IF Statement Logic for Program Flow in XNA Game Studio Express</h4>
<pre><span style="color: green">// TODO: Add your update logic here<u1:p></u1:p><o:p></o:p></span>
<span style="color: green">//logic to tint<span>      </span>*XNAGameDev.com<u1:p></u1:p><o:p></o:p></span>
<span>            </span><span style="color: blue">if</span> (IncrementCount == <span style="color: blue">true</span>)
<u1:p></u1:p>
<span>            </span>{
<span>             </span><span>   </span>GreenBlueValue += 2;
<span>            </span>}
<u1:p></u1:p>
<span>            </span><span style="color: blue">else<u1:p></u1:p><o:p></o:p></span>
<span>            </span>{
<span>                </span>GreenBlueValue -= 2;
<span>            </span>}
<u1:p></u1:p>
<span>            </span><span style="color: blue">if</span> (GreenBlueValue == 255)
<span>            </span>{
<span>                </span>IncrementCount = <span style="color: blue">false</span>;
<span>            </span>}
<span>            </span><span style="color: blue">else</span> <span style="color: blue">if</span> (GreenBlueValue == 101)
<span>            </span>{
<span>                </span>IncrementCount = <span style="color: blue">true</span>;
<span>            </span>}
<u1:p></u1:p>
<o:p> </o:p>
<span>            </span>tint = <span style="color: blue">new</span> <span style="color: #2b91af">Color</span>(255, <span style="color: #2b91af">Convert</span>.ToByte(GreenBlueValue), <span style="color: #2b91af">Convert</span>.ToByte(GreenBlueValue), 100);
<u1:p></u1:p>
<o:p> </o:p>
<span>            </span><span style="color: blue">base</span>.Update(gameTime);</pre>
<p class="MsoNormal">First, we’ll start with the IF statement. The logic is fairly simplistic- if <em>IncrementCount</em> is true, then we need to start adding green and blue values to the tint. Otherwise, we need to add a redder tint by decrementing the green and blue values.</p>
<p class="MsoNormal">Following the first IF statement is another IF statement that acts as a switch. If our values reach a certain maximum or minimum, we switch the direction of our color change. (If we reach the fully white tint of 255, we decrement. If we reach the fully red tint of 101, we switch the variable to increment)</p>
<p class="MsoNormal"><em>tint = <span style="color: blue">new</span> <span style="color: #2b91af">Color</span>(255, <span style="color: #2b91af">Convert</span>.ToByte(BlueGreenValue), <span style="color: #2b91af">Convert</span>.ToByte(BlueGreenValue), 100);</em><span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'"> </span>is what we use to store our tint value. Note that in its original form, the tint would be in the format of an RGB color such as <span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: blue">new</span><span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'"> <span style="color: #2b91af">Color</span>(255, <span style="color: #0d0d0d">255, 255</span>, 100); - </span>whereas the first three values are our red, green, and blue colors respectively. The last number, 100, is our transparency value. Obviously, we should keep this at 100 since we only want to tint. The <em>Convert</em><em>.ToByte(BlueGreenValue) </em>line is what we call a type conversion. Our <em>BlueGreenValue</em> is an integer- not a byte. But the Color function accepts the byte, not the integer! To do this, we simply use the <em>Convert</em><em>.ToByte</em> function, and pass our <em>BlueGreenValue</em> to it.<o:p></o:p></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><o:p></o:p></p>
<p class="MsoNormal">We only need to do one more thing: pass our new tint value to the <strong>Draw</strong> method.<em> </em>This can be done with the following modification to our <strong>Draw</strong> method (be sure to edit the last <em>texture_mcharacter</em>):</p>
<h4>Using Our Tint Variable in the Draw Method of XNA Game Studio Express</h4>
<pre>s_batch.Draw(texture_mcharacter, loc4, tint);<o:p></o:p></pre>
<p>Notice that instead of a color, we simply pass the tint variable. Now run your program- you should have a very pissed Ryu fading in and out of anger!  (Note that the following image is an example of the tint. Only the fourth Ryu should be showing a tint as of now.The other three sprites will be used in the next section!)</p>
<p class="MsoNormal">&nbsp;</p>
<p><center><img src="http://xnagamedev.com/wp-content/uploads/sprite-tint-xna.jpg" alt="sprite tint xna" /></center>Tinting is a lot of fun, isn’t it? It sure does keep our attention span occupied longer than the sprite we worked with in the last section! But there are still <em>more</em> things we could do to our sprites to make them stand out in video games. In the next section we’ll take a look at a few more ways to keep sprites interesting! Be sure to keep your code saved, because we will be using the three sprites that we didn’t use in this section.</p>
]]></content:encoded>
			<wfw:commentRss>http://xnagamedev.com/xna-basics/how-to-tint-sprites-in-xna-game-studio-express/feed</wfw:commentRss>
		</item>
		<item>
		<title>How to Add a Sprite in XNA Game Studio Express</title>
		<link>http://xnagamedev.com/xna-basics/how-to-add-a-sprite-in-xna-game-studio-express</link>
		<comments>http://xnagamedev.com/xna-basics/how-to-add-a-sprite-in-xna-game-studio-express#comments</comments>
		<pubDate>Tue, 26 Feb 2008 02:30:46 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[Sprites]]></category>

		<category><![CDATA[XNA Basics]]></category>

		<category><![CDATA[3d game development]]></category>

		<category><![CDATA[computer game programming]]></category>

		<category><![CDATA[game develoment tools]]></category>

		<category><![CDATA[game development software]]></category>

		<category><![CDATA[game makers]]></category>

		<category><![CDATA[game programming tutorial]]></category>

		<category><![CDATA[online game development]]></category>

		<category><![CDATA[programming games]]></category>

		<category><![CDATA[video game development]]></category>

		<guid isPermaLink="false">http://xnagamedev.com/xna-basics/how-to-add-a-sprite-in-xna-game-studio-express</guid>
		<description><![CDATA[There are two commonly accepted definitions of a sprite: one is the refreshingly crisp soft drink that was released in 1961 by the Coca-Cola Company. To the rest of us (and XNA developers in particular) a sprite is just a simple 2D in-game graphic.
Although sprites commonly appear 3D, they are actually 2D images- which have [...]]]></description>
			<content:encoded><![CDATA[<p class="MsoNormal">There are two commonly accepted definitions of a sprite: one is the refreshingly crisp soft drink that was released in 1961 by the Coca-Cola Company. To the rest of us (and XNA developers in particular) a sprite is just a simple 2D in-game graphic.<span id="more-11"></span></p>
<p class="MsoNormal">Although sprites commonly appear 3D, they are actually 2D images- which have led some of them to be cleverly referred to as 2.5D. Sprites are essentially the basis of all 2D games; and make up particle systems, text, and heads-up displays in 3D games. Since the sprite is the building block of most games, what better place is there to start learning XNA Game Studio Express?</p>
<p style="border: 1px solid #c2d5de; padding: 3px; background-color: #e3eef3; color: black; text-align: center">Lost? View the previous section: <a href="http://xnagamedev.com/xna-basics/getting-started-with-making-games-in-xna-game-studio-express">Getting Started with Making Games in XNA Game Studio Express</a></p>
<h3><strong><span style="font-size: 12pt; line-height: 115%">How Sprites Function within XNA Game Studio Express<o:p></o:p></span></strong></h3>
<p class="MsoNormal">There are three basic terms associated with sprites XNA: <strong>textures</strong>, the <strong>sprite</strong> itself, and the <strong>Sprite Batch</strong>.</p>
<p class="MsoNormal">A <strong>texture</strong> and sprite are very similar in definition. A texture is a digital image that remains in-memory, and is represented by an array of colored pixels. This is just a lot of fancy wording for something as simple as a wood texture you may use to make a rectangle look like a board.</p>
<p class="MsoNormal"><strong>Sprites</strong> are two-dimensional images that can act as textures, but normally have a certain purpose in the game. You will see sprites commonly used for characters, objects (such as trees), enemies, or even tools such as weapons.</p>
<p class="MsoNormal">The <strong>Sprite Batch</strong> is a class that draws ours sprites onto the screen. This class can work with groups of sprites while applying the same settings to all of them. This class is necessary to get the sprite onto the screen- so we’ll review this in more detail further on.</p>
<p class="MsoNormal">Before we get any further into creating and animating our own sprite, let’s do a little review on the XNAGame Studio Express interface and syntax.</p>
<h3><strong><span style="font-size: 12pt; line-height: 115%">Finding Your Way around XNA Game Studio Express<o:p></o:p></span></strong></h3>
<p class="MsoNormal">Now that we have the theory (hopefully) in memory, we can move onto opening XNA Game Studio Express. You will want to start a new <strong>Windows Game</strong> project by selecting <strong>File</strong> and then <strong>New Project</strong>. You should see a screen similar to the one below.</p>
<p><center><img src="http://xnagamedev.com/wp-content/uploads/xna-new-project.jpg" /></center></p>
<p class="MsoNormal">You can name the project whatever you would like, but make sure that you select Windows Game. Also make note of where you store the project. By default, this will be the Projects folder in Visual Studio 2005.</p>
<p class="MsoNormal">You’ll see a code template filled with vibrant green and blue colors. This template is necessary for game operation, so do not erase anything unless you know what you are doing. While we won’t dwell on the specifics of what each code block does at this point, a few things should be noted.</p>
<p class="MsoNormal">First, the green text is what is called a <strong>comment</strong>. Comments are not parsed by the game engine- they are only for the developer to make references and reminders in the code. Comments in XNA Game Studio Express are created with two backslashes (“//”). Multi-line comments can be made starting with <strong>/*</strong> and ending with <strong>*/</strong>.</p>
<h4>Comment Syntax in XNA Game Studio Express</h4>
<pre>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span>        </span><span style="color: green">// This is my very own comment!<o:p></o:p></span></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'; color: green"><o:p> </o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span>        </span><span style="color: green">/* this is<o:p></o:p></span></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'; color: green"><span>         </span>* a longer<o:p></o:p></span>
<p class="MsoNormal"><span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'; color: green"><span>         </span>* comment! */</span>
</pre>
<p class="MsoNormal">Also note that XNA Game Studio Express uses blue and other colored texts for certain commands. This is merely to help organize data- there is no other significance of this color coding. You will notice that classes, for instance, are dark blue- while other structures are lighter blue.</p>
<h4>An Example of Code Highlighting</h4>
<pre>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'; color: blue">public</span><span style="font-size: 10pt; font-family: 'Courier New'"> Game1()<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span>        </span>{<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span>            </span>graphics = <span style="color: blue">new</span> <span style="color: #2b91af">GraphicsDeviceManager</span>(<span style="color: blue">this</span>);<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span>            </span>content = <span style="color: blue">new</span> <span style="color: #2b91af">ContentManager</span>(Services);<o:p></o:p></span>
<p class="MsoNormal"><span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'"><span>        </span>}<o:p></o:p></span>
</pre>
<p class="MsoNormal"><span>Before we get to the sprite aspects of XNA, we need to learn one more thing: how the four basic methods that XNA created for us actually <em>work</em>!<o:p></o:p></span></p>
<h3><strong><span style="font-size: 12pt; line-height: 115%">How Do The Methods in XNA Game Studio Express Work?<o:p></o:p></span></strong></h3>
<p class="MsoNormal">There are four methods that were created for us: <strong>LoadGraphicsContent</strong>, <strong>Update</strong>, <strong>Draw</strong>, and <strong>UnloadGraphicsContent</strong>. These four methods act as the heartbeat of an XNA game- so be prepared to memorize what they do and how.</p>
<p class="MsoNormal"><strong>LoadGraphicsContent</strong> is a method that is somewhat explanatory. Any images or other media files that you upload via the Content Pipeline will be called to here. This will load the media into memory, making it ready for use in the game you have created. Below is an example of calling to a media file (something we will actually learn further on).</p>
<h4>An Example of The XNA LoadGraphicsContent Method</h4>
<pre>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'; color: blue">protected</span><span style="font-size: 10pt; font-family: 'Courier New'"> <span style="color: blue">override</span> <span style="color: blue">void</span> LoadGraphicsContent(<span style="color: blue">bool</span> loadAllContent)<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span>        </span>{<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span>            </span><span style="color: blue">if</span> (loadAllContent)<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span>            </span>{<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span>                </span><span style="color: green">// TODO: Load any ResourceManagementMode.Automatic content<o:p></o:p></span></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'; color: green"><o:p> </o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span>                </span><span style="color: green">//Load Sprite Batch<span>    </span>*XNAGameDev.com<o:p></o:p></span></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span>                </span>spriteBatch = <span style="color: blue">new</span> <span style="color: #2b91af">SpriteBatch</span>(<span style="color: blue">this</span>.graphics.GraphicsDevice);<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><o:p> </o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span>                </span><span style="color: green">//Load The Image<span>      </span>*XNAGameDev.com<o:p></o:p></span></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span>                </span>MainCharacter = contentLoader.Load&lt;<span style="color: #2b91af">Texture2D</span>&gt;(<span style="color: #a31515">&#8220;Sprites\\Main_Character&#8221;</span>) <span style="color: blue">as</span> <span style="color: #2b91af">Texture2D</span>;<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span>            </span>}<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><o:p> </o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span>            </span><span style="color: green">// TODO: Load any ResourceManagementMode.Manual content<o:p></o:p></span></span>
</pre>
<p class="MsoNormal"><span>Note the above example will not work until we learn more about sprites- but you get the general idea how sprites and textures are loaded via the LoadGraphicsContent method! Simply replace the <em><font color="#800000">Sprites\\Main_character</font></em> with the image filename you have, and voila; you have successfully referenced your sprite!<o:p></o:p></span></p>
<p class="MsoNormal"><span>The <strong>Update</strong> method is where most of the action takes place. This is where the “game loop” as it is called operates. If you have an objects that need to be updated (check to see if a key was pressed, for instance) they will have to be referenced here. This method updates once every frame- before the graphics are drawn to the screen. Below is an example of how we find out if a key was pressed or not (something we will not cover until later- but it&#8217;s good to  see the power of XNA!).<o:p></o:p></span></p>
<h4>An Example of The XNA Update Method</h4>
<pre>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span>        </span><span style="color: blue">protected</span> <span style="color: blue">override</span> <span style="color: blue">void</span> Update(<span style="color: #2b91af">GameTime</span> gameTime)<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span>        </span>{<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span>            </span><span style="color: green">// Allows the game to exit<o:p></o:p></span></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span>            </span><span style="color: blue">if</span> (<span style="color: #2b91af">GamePad</span>.GetState(<span style="color: #2b91af">PlayerIndex</span>.One).Buttons.Back == <span style="color: #2b91af">ButtonState</span>.Pressed)<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span>                </span><span style="color: blue">this</span>.Exit();<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><o:p> </o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span>            </span><span style="color: green">// TODO: Add your update logic here<o:p></o:p></span></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span>            </span><span style="color: green">//If the UP key is pressed, move the player up!<span>      </span>*XNAGameDev.com<o:p></o:p></span></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span>            </span><span style="color: blue">if</span> (ksKeyboardState.IsKeyDown(<span style="color: #2b91af">Keys</span>.Up))<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span>            </span>{<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span>                </span>Move_Player_Up = <span style="color: blue">true</span>;<o:p></o:p></span>
<p class="MsoNormal"><span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'"><span>            </span>}<o:p></o:p></span>
</pre>
<p class="MsoNormal"><span>The <strong>Draw</strong> method is for the process of drawing the graphics onto the screen. With every single frame, the screen is redrawn from the bottom up. <span> </span>In the below example, we see how we might call to several different functions to draw multiple sprites with the same Sprite Batch.<o:p></o:p></span></p>
<h4>An Example of The XNA Draw Method</h4>
<pre>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'; color: blue">protected</span><span style="font-size: 10pt; font-family: 'Courier New'"> <span style="color: blue">override</span> <span style="color: blue">void</span> Draw(<span style="color: #2b91af">GameTime</span> gameTime)<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span>        </span>{<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span>            </span>graphics.GraphicsDevice.Clear(<span style="color: #2b91af">Color</span>.CornflowerBlue);<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><o:p> </o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span>            </span><span style="color: green">// TODO: Add your drawing code here<o:p></o:p></span></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span>            </span>spriteBatch.Begin()<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span>            </span>DrawPlayer(spriteBatch);<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span>            </span>DrawBackground(spriteBatch);<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span>            </span>DrawEnemies(spriteBatch);<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span>            </span>spriteBatch.End()<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><o:p> </o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span>   </span><span>         </span><span style="color: blue">base</span>.Draw(gameTime);<o:p></o:p></span>
<p class="MsoNormal"><span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'"><span>        </span>}<o:p></o:p></span>
</pre>
<p class="MsoNormal">Lastly, we need to unload any resources we have with <strong>UnloadGraphicsContent</strong>. This method will free up resources for us, so it’s important to unload any graphics you create. In most cases you could simply dispose of the Content Manager itself. If you manually managed resources (which we won’t be doing in our example) you will need to unload them separately. Below you can see how simple it is to unload everything by disposing of the Content Manager with <em>content.Unload();.</em></p>
<h4>An Example of The XNA UnloadGraphicsContent Method</h4>
<pre>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'; color: blue">protected</span><span style="font-size: 10pt; font-family: 'Courier New'"> <span style="color: blue">override</span> <span style="color: blue">void</span> UnloadGraphicsContent(<span style="color: blue">bool</span> unloadAllContent)<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span>        </span>{<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span>            </span><span style="color: blue">if</span> (unloadAllContent)<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span>            </span>{<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span>                </span><span style="color: green">// TODO: Unload any ResourceManagementMode.Automatic content<o:p></o:p></span></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span>                </span>content.Unload();<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span>            </span>}<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><o:p> </o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span>            </span><span style="color: green">// TODO: Unload any ResourceManagementMode.Manual content<o:p></o:p></span></span>
<p class="MsoNormal"><span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'"><span>        </span>}<o:p></o:p></span>
</pre>
<p class="MsoNormal">If you’re eager to get to the sprite animation, don’t worry- it’s coming up next!</p>
<h3><strong><span style="font-size: 12pt; line-height: 115%">How to Add a Sprite in XNA Game Studio Express<o:p></o:p></span></strong></h3>
<p class="MsoNormal">First you will want to create a new project- we called ours <em>WindowsGame3</em>. The first thing we should do is declare our <strong>Sprite Batch</strong> and texture. Look at the following code; we place the new lines directly after the class declaration for <strong>WindowsGame3</strong>.</p>
<h4>Declaring Sprite Batch and The Texture Variable</h4>
<pre>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'; color: green">//Make Our Sprite Batch<span>      </span>*XNAGameDev.com<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span>        </span><span style="color: blue">protected</span> <span style="color: #2b91af">SpriteBatch</span> s_batch = <span style="color: blue">null</span>;<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><o:p> </o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span>        </span><span style="color: green">//Load Character Texture<span>    </span><span>  </span>*XNAGameDev.com<o:p></o:p></span></span>
<p class="MsoNormal"><span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'"><span>        </span><span style="color: blue">protected</span> <span style="color: #2b91af">Texture2D</span> texture_mcharacter = <span style="color: blue">null</span>;<o:p></o:p></span>
</pre>
<p class="MsoNormal"><span>It&#8217;s usually a good idea to assign objects to the state of null, since we want to make sure we know what the object is holding. (In some cases, the absence of null may fill a variable with garbage that could crash our game.) And in case you are wondering what our <strong>texture_mcharacter</strong> is, we are using a certain JPG image that you may remember form videogame past. You may download the image pictured below and use it in your own project.<o:p></o:p></span></p>
<p><center><img src="http://xnagamedev.com/wp-content/uploads/sprite1.jpg" /></center></p>
<p class="MsoNormal">To add the handsomely ripped Ryu to your project, simply go to <strong>Project</strong> and choose <strong>Add Existing Item</strong>.</p>
<p><center><img src="http://xnagamedev.com/wp-content/uploads/add-existing-item.png" /></center></p>
<p class="MsoNormal">In the screen that comes up, find the image you want to use for the sprite animation. Be sure to select “All Files,” “Content Pipeline Files,” or “Image Files” for the file type, or you may not see the image in the file view. To avoid complications, make sure that your sprite file is named <strong>Sprite1</strong>- this is the name we use in our example.</p>
<p class="MsoNormal">Now that we have our declarations and image files in place, we need to reference the image files within the <strong>LoadGraphicsContent</strong> method.</p>
<h4>Loading The Sprite Into The XNA Content Pipeline</h4>
<pre>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'; color: blue">protected</span><span style="font-size: 10pt; font-family: 'Courier New'"> <span style="color: blue">override</span> <span style="color: blue">void</span> LoadGraphicsContent(<span style="color: blue">bool</span> loadAllContent)<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span>        </span>{<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span>            </span><span style="color: blue">if</span> (loadAllContent)<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span>            </span>{<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span>                </span><span style="color: green">//Initialize Sprite Batch<span>     </span>*XNAGameDev.com<o:p></o:p></span></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span>               </span><span> </span>m_batch = <span style="color: blue">new</span> SpriteBatch(graphics.GraphicsDevice);<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><o:p> </o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span>                </span><span style="color: green">//Load Textures<span>      </span>*XNAGameDev.com<o:p></o:p></span></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span>                </span>texture_mcharacter = content.Load&lt;Texture2D&gt;(<span style="color: #a31515">@&#8221;sprite1&#8243;</span>);<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span>            </span>}<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><o:p> </o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span>            </span><span style="color: green">// TODO: Load any ResourceManagementMode.Manual content<o:p></o:p></span></span>
</pre>
<p class="MsoNormal"><span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'"><span>        </span>}</span></p>
<p class="MsoNormal">The above method is rather simple. Just replace <em><font color="#800000">sprite1</font></em> with the media you uploaded via the Content Pipeline, and it&#8217;ll be loaded for further use! Next we should draw the sprite to the screen with the <strong>Draw</strong> method. The following code will do just that.</p>
<h4>Using The XNA Draw Method to Output The Sprite To The Screen</h4>
<pre>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'; color: blue">protected</span><span style="font-size: 10pt; font-family: 'Courier New'"> <span style="color: blue">override</span> <span style="color: blue">void</span> Draw(<span style="color: #2b91af">GameTime</span> gameTime)<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span>        </span>{<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span>            </span>graphics.GraphicsDevice.Clear(<span style="color: #2b91af">Color</span>.White);<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><o:p> </o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span>            </span><span style="color: green">// TODO: Add your drawing code here<o:p></o:p></span></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span>            </span>s_batch.Begin();<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span>            </span><span style="color: #2b91af">Vector2</span> loc = <span style="color: blue">new</span> <span style="color: #2b91af">Vector2</span>(100, 100);<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span>            </span>s_batch.Draw(texture_mcharacter, loc, <span style="color: #2b91af">Color</span>.White);<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span>      </span><span>      </span>s_batch.End();<o:p></o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><o:p> </o:p></span>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal"><span style="font-size: 10pt; font-family: 'Courier New'"><span>            </span><span style="color: blue">base</span>.Draw(gameTime);<o:p></o:p></span>
<p class="MsoNormal"><span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'"><span>        </span>}<o:p></o:p></span>
</pre>
<p class="MsoNormal"><span>The above code is rather simple once you get the hang of things. We have to begin the Sprite Batch, define the location, draw the sprite, and then end the Sprite Batch. In our example we drew the sprite at the location (100, 100)- you can use anything you’d like. Be sure to call the <strong>s_batch.end();</strong> - try running the program without it and see what happens. (The error is confusing at first, but easily explained. Since each frame forces the <strong>Draw</strong> method to be called, the s_batch would begin more than once without an end- and that will result in an error!)<o:p></o:p></span></p>
<p class="MsoNormal"><span>Notice that the <em>graphics.GraphicsDevice.Clear(<font color="#2b91af">Color</font>.White);</em> line was changed from the default blue color. If we had kept the blue color, the whitespace in our image would show (and it isn’t pretty). In the future, we could use transparency to make the image look great on all backgrounds- but we will save that for a later day!<o:p></o:p></span></p>
<p class="MsoNormal"><span>If all has gone well at this point, you should see something like the window below.<o:p></o:p></span></p>
<p><center><img src="http://xnagamedev.com/wp-content/uploads/xna-sprite-example.jpg" /></center></p>
<p class="MsoNormal">It couldn’t be more boring could it? Our rugged little fighter Ryu can’t punch, move, talk, or even blink! There is no environment, physics, lighting, or anything at all. But what we <em>did</em> accomplish is grasp the concept of a sprite.</p>
<p class="MsoNormal">So where do we go from here? We can add tint, transparency, animate, scale, and add all sorts of effects to the sprite. We could give the player the ability to move the character- and even make it jump!</p>
<p class="MsoNormal">All this (and more!) will be forthcoming in future sections.</p>
]]></content:encoded>
			<wfw:commentRss>http://xnagamedev.com/xna-basics/how-to-add-a-sprite-in-xna-game-studio-express/feed</wfw:commentRss>
		</item>
		<item>
		<title>Getting Started with Making Games in XNA Game Studio Express</title>
		<link>http://xnagamedev.com/xna-basics/getting-started-with-making-games-in-xna-game-studio-express</link>
		<comments>http://xnagamedev.com/xna-basics/getting-started-with-making-games-in-xna-game-studio-express#comments</comments>
		<pubDate>Mon, 25 Feb 2008 03:16:45 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[XNA Basics]]></category>

		<category><![CDATA[featured]]></category>

		<category><![CDATA[3d game development]]></category>

		<category><![CDATA[computer game programming]]></category>

		<category><![CDATA[game develoment tools]]></category>

		<category><![CDATA[game development software]]></category>

		<category><![CDATA[game makers]]></category>

		<category><![CDATA[game programming tutorial]]></category>

		<category><![CDATA[online game development]]></category>

		<category><![CDATA[programming games]]></category>

		<category><![CDATA[video game development]]></category>

		<guid isPermaLink="false">http://xnagamedev.com/xna-basics/getting-started-with-making-games-in-xna-game-studio-express</guid>
		<description><![CDATA[At some point or another, we have all had the dream to make a video game. There has always been a certain brick wall between our dreams and reality. This brick wall is composed of mathematics, physics, computer programming, and of course the luxury of time to burn.
But Microsoft tore the wall down with the [...]]]></description>
			<content:encoded><![CDATA[<p class="MsoNormal">At some point or another, we have all had the dream to make a video game. There has always been a certain brick wall between our dreams and reality. This brick wall is composed of mathematics, physics, computer programming, and of course the luxury of time to burn.</p>
<p class="MsoNormal">But Microsoft tore the wall down with the release of XNA Game Studio Express- simply referred to as XNA. This framework allows hopeful game developers to create video games in a simple environment, which requires much less time or effort as compared to conventional means.<span id="more-7"></span></p>
<p class="MsoNormal">Although Microsoft graced the game development community with XNA, there is still a learning curve present. Don’t expect to create the next Half Life 2 in a few days. Do, however, expect to get on the right track to releasing the next killer game for the PC or Xbox with a little determination and hard work.</p>
<h3>What Is XNA Game Studio Express?</h3>
<p class="MsoNormal">In case you were wondering, XNA doesn’t stand for anything cool. In fact, it doesn’t stand for anything at all! So what kind of power can a tool that doesn’t have any substance to its name have, anyway?</p>
<p class="MsoNormal"><span> </span>XNA Game Studio Express is a combination of tools that allows developers, students, and hobbyists to create games with relative ease. Although you won’t need to know the specifics about each tool, it’ll help aid in the development process and your general knowledge of how things work. The basic tools and components that XNA is composed of or interacts with are listed below. (Don’t worry if these seem intimidating- we’ll focus on some components more in future sections!)</p>
<h4>Basic Tools and Components XNA is Composed of or Interacts With</h4>
<ul style="margin-top: 0in" type="disc">
<li class="MsoNormal"><strong>XNA Framework</strong> – The XNA framework is a bundle of libraries      used for development. It functions via an application program interface-      or API. This API allows games to be ported to and from Windows machines      and the Xbox 360 console. The API also handles DirectX and the .NET      platform- two tools used for general programming and multimedia      programming.</li>
<li class="MsoNormal"><strong>XNA Framework Content Pipeline </strong>– The “Content Pipeline,” as it      is commonly called, is a set of tools that helps reduce the learning curve      associated with game development programming. It streamlines many actions      related to getting the content into the actual game. (For instance- you      can add an image file to a game with a simple upload and code reference.      Then the Content Pipeline automatically converts the file into a usable      entity!)</li>
<li class="MsoNormal"><strong>Microsoft Visual C# Express –</strong> Microsoft Visual C# Express is a      toolset that allows programmers to create and manage programs with      relative ease. In many aspects, this is just like XNA Game Studio Express,      with the emphasis on general programming rather than game development. XNA      Game Studio Express actually uses this program to operate- so be sure to      brush up on your C# skills!</li>
<li class="MsoNormal"><strong>DirectX</strong> – DirectX is a set of APIs that were created to work      with multimedia development. The reason why XNA Game Studio Express is      preferred by beginners is because DirectX can be very tough- it has a very      steep learning curve. Thankfully, we won’t have to work directly with      DirectX to make a game- which makes the development process much speedier.</li>
</ul>
<h3>What Kind of Games Can I Create With XNA Game Studio Express?</h3>
<p><span style="font-size: 11pt; line-height: 115%; font-family: 'Calibri','sans-serif'">This is the million dollar question: how far can you take the XNA Game Studio Express? You wouldn’t want to waste your time learning a language and development scheme, just to realize you can’t make the game you had in mind- would you?</span></p>
<p style="text-align: center"><img src="http://xnagamedev.com/wp-content/uploads/xna-games.png" alt="xna games" /></p>
<p class="MsoNormal">As you can see from the above example, there isn’t much limit with XNA. We can create 2D, 2.5D, and some very stunning 3D environments. Given, it would probably be impractical to create Halo 3 with XNA Game Studio- but don’t let that discourage you in the least bit.</p>
<p class="MsoNormal">We are only seeing the very beginning of what’s to come. XNA Game Studio Express is still fairly new to the game development community- we’re still eagerly awaiting those “No way you made that with XNA!” games that converts every last developer to the XNA cause. (Not that those games don’t already exist- we just want more of them!)</p>
<h3>Software Requirements for Developing XNA Game Studio Games</h3>
<p class="MsoNormal">Sadly, you won’t be up and running with a simple download. Instead, Microsoft thought it’d be fun to make developers go through several separate download and installation processes. But first thing is first: let’s make sure your computer can run the software before downloading anything!</p>
<h4>Minimum Requirements for Creating XNA Game Studio Games</h4>
<ul style="margin-top: 0in" type="disc">
<li class="MsoNormal">Windows XP (With Service      Pack 2) or Windows Vista</li>
<li class="MsoNormal">1 GHz CPU or higher is      recommended</li>
<li class="MsoNormal">256MB RAM is recommended</li>
<li class="MsoNormal">At least 500MB free space-      over 1GB is recommended</li>
</ul>
<h4>Requirements for Playing XNA Game Studio Games</h4>
<ul style="margin-top: 0in" type="disc">
<li class="MsoNormal">Graphics card that      supports DirectX 9.0c Shader model (Model 2.0 recommended)</li>
<li class="MsoNormal">Sound card that supports      DirectX 9.0c</li>
<li class="MsoNormal">If you would like to      develop games for the Xbox, a control is necessary</li>
</ul>
<h3>Installing the Necessary Software for XNA</h3>
<p class="MsoNormal">Before continuing, be sure that any previous versions or betas of the software you are about to download are removed from your computer. Otherwise, the versions may conflict with each other and you’ll have a mess on your hands. The download process can be followed as seen below.</p>
<ol>
<li class="MsoNormal"><a href="http://www.microsoft.com/express/2005/download/default.aspx">Download      Visual C# 2005 Express Edition</a> – (Be sure to select C#, not C++)</li>
<p></p>
<li class="MsoNormal"><a href="http://www.microsoft.c