<?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"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Ben Silvis</title>
	<atom:link href="http://www.bensilvis.com/?feed=rss2" rel="self" type="application/rss+xml" />
	<link>http://www.bensilvis.com</link>
	<description>Game Development in Flash</description>
	<lastBuildDate>Mon, 21 Dec 2009 04:11:58 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>AS3: SpriteSheet Class &#8211; Extracting Sprites</title>
		<link>http://www.bensilvis.com/?p=317</link>
		<comments>http://www.bensilvis.com/?p=317#comments</comments>
		<pubDate>Fri, 30 Oct 2009 00:18:02 +0000</pubDate>
		<dc:creator>Ben</dc:creator>
				<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[custom class]]></category>
		<category><![CDATA[Sprite]]></category>
		<category><![CDATA[SpriteSheet]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.bensilvis.com/?p=317</guid>
		<description><![CDATA[If you haven&#8217;t already read the intro to this tutorial, you can find it here. In it we are introduced to sprite sheets, what they are, and some examples for where to use them. One of those examples included combining many assets into a single sprite sheet. We can then extract a portion of this bitmap [...]]]></description>
			<content:encoded><![CDATA[<p>If you haven&#8217;t already read the intro to this tutorial, you can find it <a href="/?p=229" target="_self">here</a>. In it we are introduced to sprite sheets, what they are, and some examples for where to use them.</p>
<p>One of those examples included combining many assets into a single sprite sheet. We can then extract a portion of this bitmap as its own unique element and use it as if we had loaded it separately. This kind of sprite sheet could be used for a variety of things. Maybe you&#8217;re designing an interface and you want to group similar UI elements in order to load them all at once. You could also find yourself developing a game in which there are many items for the player to collect and you need an easy way to organize them. The example below shows such a sprite sheet:</p>
<p><img class=" alignnone" style="margin-bottom: -4px;" title="Sprite sheet of items in a SNES game" src="/tutorials/sprite_sheets/zelda_SS.png" alt="Sprite sheet of items in SNES game Zelda" width="430" height="56" /><br />
<span style="font-size: 10px;">Images © Nintendo</span></p>
<h5>Extracting Sprites</h5>
<p>Like we mentioned above, this kind of sprite sheet would be the most useful to us if we had a way to extract an individual element from it. Thankfully with the help of some code and a little Triforce of Wisdom we can do just that.</p>
<p><span id="more-317"></span></p>
<p><em>Note: From here on out I&#8217;ll be referring to my <strong>SpriteSheet </strong>class. It is available <a title="SpriteSheet class" href="/?p=229" target="_blank">here</a> for reference.</em></p>
<p><span style="font-size: 13px;"><object style="width: 450px; height: 150px;" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="450" height="150" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="src" value="/tutorials/sprite_sheets/sprite_sheet_1.swf" /><param name="bgcolor" value="#ffffff" /><param name="allowFullScreen" value="true" /><param name="allowfullscreen" value="true" /><embed style="width: 450px; height: 150px;" type="application/x-shockwave-flash" width="450" height="150" src="/tutorials/sprite_sheets/sprite_sheet_1.swf" allowfullscreen="true" bgcolor="#ffffff"></embed></object></span></p>
<p>This example uses the same sprite sheet that we first looked at in this tutorial. Click an area of it to extract it and blow it up, showing off its 16-bit glory. In the code below we will attempt to extract a sprite in the same way.</p>
<h4>Actionscript</h4>
<div class="codecolorer-container actionscript3 dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:700px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br /></div></td><td><div class="actionscript3 codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0033ff; font-weight: bold;">import</span> com<span style="color: #000066; font-weight: bold;">.</span>bensilvis<span style="color: #000066; font-weight: bold;">.</span>spriteSheet<span style="color: #000066; font-weight: bold;">.</span>SpriteSheet<span style="color: #000066; font-weight: bold;">;</span><br />
<br />
<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.display</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">Bitmap</span><span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.display</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">Sprite</span><span style="color: #000066; font-weight: bold;">;</span></div></td></tr></tbody></table></div>
<p>You will need to import the custom SpriteSheet class like this in order to have access to it, along with these other imports.</p>
<h4>Actionscript</h4>
<div class="codecolorer-container actionscript3 dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:700px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br /></div></td><td><div class="actionscript3 codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #6699cc; font-weight: bold;">var</span> currentSprite<span style="color: #000066; font-weight: bold;">:</span>SpriteSheet = <span style="color: #0033ff; font-weight: bold;">new</span> SpriteSheet<span style="color: #000000;">&#40;</span>sheet<span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000; font-weight:bold;">20</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000; font-weight:bold;">20</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<br />
<span style="color: #6699cc; font-weight: bold;">var</span> image<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Bitmap</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Bitmap</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
image<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">bitmapData</span> = currentSprite<span style="color: #000066; font-weight: bold;">.</span>DrawTile<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span>image<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span></div></td></tr></tbody></table></div>
<h5>What we do here is:</h5>
<ol>
<li>Instantiate a copy of the <strong>SpriteSheet</strong> class</li>
<li>Extract tile &#8217;0&#8242; with the DrawTile function</li>
<li>Add the extracted sprite to the stage</li>
</ol>
<p>Pretty simple huh?</p>
<h4>Actionscript</h4>
<div class="codecolorer-container actionscript3 dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:700px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="actionscript3 codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #6699cc; font-weight: bold;">var</span> currentSprite<span style="color: #000066; font-weight: bold;">:</span>SpriteSheet = <span style="color: #0033ff; font-weight: bold;">new</span> SpriteSheet<span style="color: #000000;">&#40;</span>sheet<span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000; font-weight:bold;">20</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000; font-weight:bold;">20</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span></div></td></tr></tbody></table></div>
<p>If we have imported the <strong>SpriteSheet</strong> class then we can create a new copy of it and access all of its functionality. The constructor takes 3 parameters:</p>
<pre>SpriteSheet(tileSheetBitmap:Bitmap, width:Number = 16, height:Number = 16)</pre>
<ul>
<li><strong>tileSheetBitmap</strong>: the Bitmap of the sprite sheet we will use</li>
<li><strong>width</strong>: the width of  tiles in the sheet</li>
<li><strong>height</strong>: the height of tiles in the sheet</li>
</ul>
<p>Assuming we have loaded and have access to our sprite sheet Bitmap, we pass it to our new class as variable &#8216;sheet&#8217;.</p>
<p>The width and height parameters need to be set to the size of the sprites in the sprite sheet (not the entire sprite sheet). In this particular example both dimensions are 20 pixels.</p>
<p><em>Note: Each tile in your sprite sheet must have the same dimensions in order to work properly.</em></p>
<h4>Actionscript</h4>
<div class="codecolorer-container actionscript3 dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:700px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br /></div></td><td><div class="actionscript3 codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #6699cc; font-weight: bold;">var</span> image<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Bitmap</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Bitmap</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
image<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">bitmapData</span> = currentSprite<span style="color: #000066; font-weight: bold;">.</span>DrawTile<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span>image<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span></div></td></tr></tbody></table></div>
<p>Here we create a new blank Bitmap to contain the pixel data of the sprite we&#8217;re going to copy. Next we call the <strong>DrawTile </strong>function of our recently created SpriteSheet class and pass it an integer. In short, this tells the class to locate an area of the sprite sheet Bitmap based on the parameter we just sent, and extract a rectangle of pixels from it determined by the width and height that we specified in the constructor. We then hand off this data to our blank Bitmap as its new <strong>bitmapData</strong>.</p>
<p>After that we simply attach the new Bitmap to the stage, which now displays sprite &#8217;0&#8242;, to use how we please.</p>
<h5>Numbering of a sprite sheet:</h5>
<p>In the example above we extract from our sprite sheet by passing the drawTile function an integer of &#8217;0&#8242;. This number represents a particular tile on our sprite sheet. When numbering our sprites we start at the top-left and increment by one for each tile down a row progressively. Since we&#8217;re starting the count with zero we need to offset the tile&#8217;s number by one <em>(tile number &#8211; 1).</em> Therefore with &#8217;0&#8242; our example is effectively extracting the very first sprite.</p>
<p><img class="alignnone" title="Sprite Sheet Numbering" src="/tutorials/sprite_sheets/sprite_sheet_chart.png" alt="" width="250" height="90" /></p>
<p>Sometime in the next couple of days I will post an article with tips for creating sprite sheets. <img src='http://www.bensilvis.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<h5>Concluding..</h5>
<p>This example may have been a little trickier than some in the past because it involves using a custom class, so feel free to download the complete Flex project to the top example!</p>
<p>Stay tuned for the next tutorial which covers sprite sheet animation!</p>
<blockquote><address><span style="font-style: normal;"><a href="mailto:ben@bensilvis.com">Ben</a></span></address>
</blockquote>
<h5 style="text-align: center;"><a title="Download Source" href="tutorials/sprite_sheets/downloads/Extracting_Sprites.zip"><img class="alignnone" style="margin-top: 20px; margin-bottom: -12px; margin-left: 4px; margin-right: 4px; border: 0px;" title="Download Source" src="/site_images/flash_icon_44.png" alt="" width="44" height="44" />Download Source</a></h5>
]]></content:encoded>
			<wfw:commentRss>http://www.bensilvis.com/?feed=rss2&amp;p=317</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Flash and the iPhone?</title>
		<link>http://www.bensilvis.com/?p=294</link>
		<comments>http://www.bensilvis.com/?p=294#comments</comments>
		<pubDate>Sun, 18 Oct 2009 18:32:39 +0000</pubDate>
		<dc:creator>Ben</dc:creator>
				<category><![CDATA[Flash]]></category>
		<category><![CDATA[General]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[App]]></category>
		<category><![CDATA[Apple]]></category>
		<category><![CDATA[CS5]]></category>

		<guid isPermaLink="false">http://www.bensilvis.com/?p=294</guid>
		<description><![CDATA[Certainly many of you heard some speculation recently that the iPhone might finally be getting Flash, only to be followed by the disappointing announcement of &#8211; No, you wish, keep dreaming. Quite frankly I wasn&#8217;t too surprised or therefore disappointed, but still a little bitter. The idea of developing for perhaps the biggest technological craze [...]]]></description>
			<content:encoded><![CDATA[<p>Certainly many of you heard some speculation recently that the iPhone might finally be getting Flash, only to be followed by the disappointing announcement of &#8211; No, you wish, keep dreaming. Quite frankly I wasn&#8217;t too surprised or therefore disappointed, but still a little bitter. The idea of developing for perhaps the biggest technological craze of my lifetime would be both financially and &#8216;<em>spiritually</em>&#8216; intriguing. Since Flash content makes up a huge percentage of the web now and the iPhone is all about that it makes sense right? I mean even some of the clunkiest of phones now have the ability to display Flash content. So why won&#8217;t the iPhone get with the freakin&#8217; program already?</p>
<p>Basically it all comes down to the fact that Apple can&#8217;t make any money off of browser based games and utilities. It would kill their App Store &#8211; Not to mention: Why would anyone still be willing to pay <strong>$100 A Year</strong> for the SDK if all you needed was some hosting space and a domain name?</p>
<p><span style="font-size: 9px;">It&#8217;s ok.. I didn&#8217;t think &#8220;<em>There&#8217;s a Website For That.</em>&#8221; was too catchy of a slogan anyways.</span></p>
<p><span id="more-294"></span></p>
<h5>Flash CS5</h5>
<p>But wait! In recent news about the new CS5 Suite, Adobe released that Flash developers will in fact be able to develop natively on the iPhone and iPod Touch for the App Store. In fact there are Apps on the App Store today that were built in Flash. Sure it&#8217;s nowhere near as powerful as a Flash Player, but it&#8217;s definitely a start. I mentioned this in my <a href="/?p=270">last post</a>, but I didn&#8217;t elaborate quite enough on how cool it is. I&#8217;m hoping that this also means I can develop on my PC as well!</p>
<h4>Check out these videos!:<br />
<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="640" height="385" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="allowFullScreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="src" value="http://www.youtube-nocookie.com/v/JTZmjMiK7Vc&amp;hl=en&amp;fs=1&amp;rel=0&amp;color1=0x006699&amp;color2=0x54abd6" /><param name="allowfullscreen" value="true" /><embed type="application/x-shockwave-flash" width="640" height="385" src="http://www.youtube-nocookie.com/v/JTZmjMiK7Vc&amp;hl=en&amp;fs=1&amp;rel=0&amp;color1=0x006699&amp;color2=0x54abd6" allowscriptaccess="always" allowfullscreen="true"></embed></object></h4>
<p><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="640" height="385" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="allowFullScreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="src" value="http://www.youtube-nocookie.com/v/mpEUNqfk4rw&amp;hl=en&amp;fs=1&amp;rel=0&amp;color1=0x006699&amp;color2=0x54abd6" /><param name="allowfullscreen" value="true" /><embed type="application/x-shockwave-flash" width="640" height="385" src="http://www.youtube-nocookie.com/v/mpEUNqfk4rw&amp;hl=en&amp;fs=1&amp;rel=0&amp;color1=0x006699&amp;color2=0x54abd6" allowscriptaccess="always" allowfullscreen="true"></embed></object></p>
<p>Flash CS5 beta is set to be released towards the end of this year. If you are interested in the beta notifications <em>including testing possibilities</em>: <a href="https://www.adobe.com/cfusion/mmform/index.cfm?name=fpcs5_notify" target="_blank">Sign Up Here</a></p>
<p>I&#8217;m sure you still have a lot of questions (like I do). Be sure to check out the <a title="Flash/iPhone Frequently Asked Questions" href="http://labs.adobe.com/wiki/index.php/Applications_for_iPhone:Developer_FAQ" target="_blank">FAQ</a> page for more info and also the <a title="Adobe's iPhone Forum" href="http://forums.adobe.com/community/labs/flashcs5/appsfor_iphone" target="_blank">Flash/iPhone forum</a>.</p>
<blockquote><address><span style="font-style: normal;"><a href="mailto:ben@bensilvis.com">Ben</a></span></address>
</blockquote>
]]></content:encoded>
			<wfw:commentRss>http://www.bensilvis.com/?feed=rss2&amp;p=294</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Adobe Flash Builder 4 beta 2 and Flash CS5</title>
		<link>http://www.bensilvis.com/?p=270</link>
		<comments>http://www.bensilvis.com/?p=270#comments</comments>
		<pubDate>Sat, 17 Oct 2009 20:15:50 +0000</pubDate>
		<dc:creator>Ben</dc:creator>
				<category><![CDATA[Flash]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[General]]></category>
		<category><![CDATA[CS5]]></category>
		<category><![CDATA[Flash Builder]]></category>

		<guid isPermaLink="false">http://www.bensilvis.com/?p=270</guid>
		<description><![CDATA[Flash Builder 4 I&#8217;ve been using Flash Builder 4 beta for a couple of months now and for the most part have been quite pleased. FB is the follow up to Flex 3. Adobe states that the name change is to help distinguish between the open-source Flex framework and their commercial IDE. Makes sense, but personally [...]]]></description>
			<content:encoded><![CDATA[<h5>Flash Builder 4</h5>
<p>I&#8217;ve been using Flash Builder 4 beta for a couple of months now and for the most part have been quite pleased. <strong>FB</strong> is the follow up to Flex 3. Adobe states that the name change is to help distinguish between the open-source Flex framework and their commercial IDE. Makes sense, but personally I like the name packaged with the initial beta release &#8211; &#8216;Gumbo&#8217;!</p>
<p>So anyways, the license on my copy of Flash Builder mysteriously stopped working yesterday. After a brief panic attack I decided to check out the <a href="http://labs.adobe.com/technologies/flashbuilder4/" target="_blank">Adobe Labs</a> website and noticed that the beta 2 release was out. Cool! As with beta 1, my Flex 3 license is supposed to provide me with a new working FB copy &#8211; however, licensing software is one of my least favorite things to do so I&#8217;ll get back to that after the weekend. The renewed 60 day trial will suffice for now.</p>
<h5>Flash CS5</h5>
<p>Since I was in this &#8216;update&#8217; state of mind I decided to peruse my usual avenues and came across some info concerning the CS5 Suite. My initial reaction was, &#8220;<em>Oh God, not another one already..</em>&#8220;. I still haven&#8217;t gotten used to the CS4 interface, and at my office I&#8217;m still using CS3. I&#8217;m also quite afraid of Adobe turning into another Autodesk. I have to admit though &#8211; as far as Flash goes, there is A LOT of cool stuff going on.</p>
<p>For starters, Flash CS5 &#8216;<em>Viper<span style="font-style: normal;">&#8216;:</span></em></p>
<ul>
<li><a title="Flash and the iPhone?" href="/?p=294" target="_self">natively compiles for iPhone apps and other touch interfaces</a> (update)</li>
<li>has code snippet libraries for designers</li>
<li>has a fancy new javascript powered brushes engine</li>
<li>finally employs code completion for custom classes</li>
<li>generates PHYSICS animation for designers &#8211; Awesome!</li>
</ul>
<p><span style="text-decoration: underline;">and my favorite</span>:</p>
<ul>
<li><strong>integrates between Flash Builder and Flash Professional</strong></li>
</ul>
<p>CS5 is set to release in April of 2010. I didn&#8217;t really spend a lot of time looking at the other aspects of the Suite, but one of the best resources for info I found is <a href="http://cs5.org/" target="_blank">cs5.org</a>. If there&#8217;s anything cool I missed be sure to let me know!</p>
<blockquote><address><span style="font-style: normal;"><a href="mailto:ben@bensilvis.com">Ben</a></span></address>
</blockquote>
]]></content:encoded>
			<wfw:commentRss>http://www.bensilvis.com/?feed=rss2&amp;p=270</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>AS3: Intro to Using Sprite Sheets</title>
		<link>http://www.bensilvis.com/?p=229</link>
		<comments>http://www.bensilvis.com/?p=229#comments</comments>
		<pubDate>Fri, 16 Oct 2009 05:10:12 +0000</pubDate>
		<dc:creator>Ben</dc:creator>
				<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[custom class]]></category>
		<category><![CDATA[SpriteSheet]]></category>

		<guid isPermaLink="false">http://www.bensilvis.com/?p=229</guid>
		<description><![CDATA[In this post I would like to introduce you to a custom class I wrote for working with sprite sheets. Now &#8220;what&#8217;s a sprite sheet?&#8221; you might ask. Well, while I&#8217;m not going to delve into much of the theory or the speed benefits from using them, you should know a few things: A sprite sheet [...]]]></description>
			<content:encoded><![CDATA[<p>In this post I would like to introduce you to a custom class I wrote for working with sprite sheets. Now &#8220;<em>what&#8217;s a sprite sheet?&#8221;</em> you might ask. Well, while I&#8217;m not going to delve into much of the theory or the speed benefits from using them, you should know a few things:</p>
<p>A sprite sheet is a collection of sprites arranged into a single image, where each sprite represents a frame of an animation, an asset, a part of an image, or something to that extent. The idea of a sprite sheet has been utilized for a long time, namely in early gaming systems like Atari and Nintendo. In recent times they are most often used for efficient bitmap animation and are extremely useful in tile based games for level creation.</p>
<p>When loading external assets, many times you will find that it is much more time and code efficient to load many in one state &#8211; Not to mention that when used effectively this can save memory. Also, once you start animating many objects in a scene you will see a real performance spike when compared to regular Flash keyframe animation. Props to <a href="http://www.8bitrocket.com/newsdisplay.aspx?newspage=13430" target="_blank">8bitrocket</a> for some useful info.</p>
<p><span id="more-229"></span></p>
<h5>SpriteSheet Class</h5>
<p>Enough explaining for now &#8211; In the following 3 tutorials I will describe how to utilize specific features of this class to achieve different results with your sprite sheets.</p>
<ol>
<li><a title="SpriteSheet Class - Extracting Sprites" href="/?p=317" target="_self">SpriteSheet Class &#8211; Extracting Sprites</a></li>
<li>SpriteSheet Class &#8211; Sprite Sheet Animation (coming soon)</li>
<li>SpriteSheet Class &#8211; Image Compositing and Level Creation (coming soon)</li>
</ol>
<h4>Actionscript:</h4>
<div class="codecolorer-container actionscript3 dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:700px;height:600px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br />47<br />48<br />49<br />50<br />51<br />52<br />53<br />54<br />55<br />56<br />57<br />58<br />59<br />60<br />61<br />62<br />63<br />64<br />65<br />66<br />67<br />68<br /></div></td><td><div class="actionscript3 codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #9900cc; font-weight: bold;">package</span> com<span style="color: #000066; font-weight: bold;">.</span>bensilvis<span style="color: #000066; font-weight: bold;">.</span>spriteSheet<br />
<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.display</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">Bitmap</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.display</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">BitmapData</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.display</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">Sprite</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.geom</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">Point</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.geom</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">Rectangle</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> SpriteSheet <span style="color: #0033ff; font-weight: bold;">extends</span> <span style="color: #004993;">Sprite</span> <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> tileSheetBitmapData<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">BitmapData</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> canvasBitmapData<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">BitmapData</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> tileWidth<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">int</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> tileHeight<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">int</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> rowLength<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">int</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> tileRectangle<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Rectangle</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> tilePoint<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Point</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> SpriteSheet<span style="color: #000000;">&#40;</span>tileSheetBitmap<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Bitmap</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #004993;">width</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Number</span> = <span style="color: #000000; font-weight:bold;">16</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #004993;">height</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Number</span> = <span style="color: #000000; font-weight:bold;">16</span><span style="color: #000000;">&#41;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span> &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tileSheetBitmapData = tileSheetBitmap<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">bitmapData</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tileWidth = <span style="color: #004993;">width</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tileHeight = <span style="color: #004993;">height</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rowLength = <span style="color: #004993;">int</span><span style="color: #000000;">&#40;</span>tileSheetBitmap<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">width</span> <span style="color: #000066; font-weight: bold;">/</span> <span style="color: #004993;">width</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tileRectangle = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Rectangle</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">,</span> tileWidth<span style="color: #000066; font-weight: bold;">,</span> tileHeight<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tilePoint = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Point</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; canvasBitmapData = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">BitmapData</span><span style="color: #000000;">&#40;</span>tileWidth<span style="color: #000066; font-weight: bold;">,</span> tileHeight<span style="color: #000066; font-weight: bold;">,</span> <span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #6699cc; font-weight: bold;">var</span> canvasBitmap<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Bitmap</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Bitmap</span><span style="color: #000000;">&#40;</span>canvasBitmapData<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span>canvasBitmap<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; drawTile<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> drawTile<span style="color: #000000;">&#40;</span>tileNumber<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">int</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">BitmapData</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tileRectangle<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span> = <span style="color: #004993;">int</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>tileNumber <span style="color: #000066; font-weight: bold;">%</span> rowLength<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">*</span> tileWidth<span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tileRectangle<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> = <span style="color: #004993;">int</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>tileNumber <span style="color: #000066; font-weight: bold;">/</span> rowLength<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">*</span> tileHeight<span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; canvasBitmapData<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">copyPixels</span><span style="color: #000000;">&#40;</span>tileSheetBitmapData<span style="color: #000066; font-weight: bold;">,</span> tileRectangle<span style="color: #000066; font-weight: bold;">,</span> tilePoint<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">return</span> canvasBitmapData<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">clone</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> tileBoard<span style="color: #000000;">&#40;</span>boardIndex<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Array</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">BitmapData</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #6699cc; font-weight: bold;">var</span> wide<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">int</span> = boardIndex<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">length</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #6699cc; font-weight: bold;">var</span> tall<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">int</span> = boardIndex<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">length</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; canvasBitmapData = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">BitmapData</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>tileWidth <span style="color: #000066; font-weight: bold;">*</span> wide<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000;">&#40;</span>tileHeight <span style="color: #000066; font-weight: bold;">*</span> tall<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #6699cc; font-weight: bold;">var</span> boardCanvas<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Bitmap</span> = <span style="color: #004993;">Bitmap</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">getChildAt</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; boardCanvas<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">bitmapData</span> = canvasBitmapData<span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tileRectangle = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Rectangle</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">,</span><span style="color: #000000;">&#40;</span>tileWidth <span style="color: #000066; font-weight: bold;">*</span> wide<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000;">&#40;</span>tileHeight <span style="color: #000066; font-weight: bold;">*</span> tall<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #6699cc; font-weight: bold;">var</span> i<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">int</span> = <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">;</span> i <span style="color: #000066; font-weight: bold;">&lt;</span> wide<span style="color: #000066; font-weight: bold;">;</span> i<span style="color: #000066; font-weight: bold;">++</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #6699cc; font-weight: bold;">var</span> j<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">int</span> = <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">;</span> j <span style="color: #000066; font-weight: bold;">&lt;</span> tall<span style="color: #000066; font-weight: bold;">;</span> j<span style="color: #000066; font-weight: bold;">++</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tilePoint = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Point</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>tileWidth <span style="color: #000066; font-weight: bold;">*</span> i<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000;">&#40;</span>tileHeight <span style="color: #000066; font-weight: bold;">*</span> j<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; drawTile<span style="color: #000000;">&#40;</span>boardIndex<span style="color: #000000;">&#91;</span>j<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">return</span> canvasBitmapData<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">clone</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
<span style="color: #000000;">&#125;</span></div></td></tr></tbody></table></div>
<blockquote><address><span style="font-style: normal;"><a href="mailto:ben@bensilvis.com">Ben</a></span></address>
</blockquote>
]]></content:encoded>
			<wfw:commentRss>http://www.bensilvis.com/?feed=rss2&amp;p=229</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>AS3: KeyboardEvents, KeyCodes, and CharCodes</title>
		<link>http://www.bensilvis.com/?p=146</link>
		<comments>http://www.bensilvis.com/?p=146#comments</comments>
		<pubDate>Thu, 08 Oct 2009 23:54:18 +0000</pubDate>
		<dc:creator>Ben</dc:creator>
				<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Games]]></category>
		<category><![CDATA[charCode]]></category>
		<category><![CDATA[KeyboardEvent]]></category>
		<category><![CDATA[keyCode]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.bensilvis.com/?p=146</guid>
		<description><![CDATA[Once you really get going into designing basically any kind of computer game &#8211; whether it be a 2-D platformer like Contra or even online video Poker &#8211; you are eventually going to need some kind of keyboard control besides general typing. Traditionally this could be the arrow keys or W,A,S,D for movement, spacebar for [...]]]></description>
			<content:encoded><![CDATA[<p>Once you really get going into designing basically any kind of computer game &#8211; whether it be a 2-D platformer like Contra or even online video Poker &#8211; you are eventually going to need some kind of keyboard control besides general typing. Traditionally this could be the <strong>arrow keys</strong> or <strong>W</strong>,<strong>A</strong>,<strong>S</strong>,<strong>D</strong> for movement, <strong>spacebar </strong>for jumping or plasma-ray firing, <strong>R</strong> for retry or <strong>Q</strong> for quit, but really the level of control is up to you.</p>
<p><span id="more-146"></span></p>
<p><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="500" height="300" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="menu" value="false" /><param name="src" value="tutorials/key_codes/key_codes.swf" /><embed type="application/x-shockwave-flash" width="500" height="300" src="tutorials/key_codes/key_codes.swf" menu="false"></embed></object></p>
<p>A<strong> keyCode </strong>is an integer representing a particular type of key on your keyboard. By differentiating between one keyCode and another in Flash, we can trigger separate events based on each individual keyboard key or combination of.</p>
<p>The example above displays the keyCode for each keyboard button.</p>
<p><em>Note: You will have to click the example to give your keyboard&#8217;s focus to the flash player.</em></p>
<h4>Actionscript:</h4>
<div class="codecolorer-container actionscript3 dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:700px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="actionscript3 codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.events</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">KeyboardEvent</span><span style="color: #000066; font-weight: bold;">;</span></div></td></tr></tbody></table></div>
<p>All you need to import is the <strong>KeyboardEvent</strong> class for our example.</p>
<h4>Actionscript:</h4>
<div class="codecolorer-container actionscript3 dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:700px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br /></div></td><td><div class="actionscript3 codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #004993;">stage</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">KeyboardEvent</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">KEY_UP</span><span style="color: #000066; font-weight: bold;">,</span> onKeyEvent<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<br />
<span style="color: #339966; font-weight: bold;">function</span> onKeyEvent<span style="color: #000000;">&#40;</span>e<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">KeyboardEvent</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span><br />
<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #6699cc; font-weight: bold;">var</span> character<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">String</span> = <span style="color: #004993;">String</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">fromCharCode</span><span style="color: #000000;">&#40;</span>e<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">charCode</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>e<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">keyCode</span> == <span style="color: #000000; font-weight:bold;">66</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span>character<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span> &nbsp; <span style="color: #009900; font-style: italic;">//'b' or 'B'</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">else</span> <span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>e<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">keyCode</span> == <span style="color: #000000; font-weight:bold;">83</span> <span style="color: #000066; font-weight: bold;">&amp;&amp;</span> e<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">shiftKey</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span>character<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span> &nbsp; <span style="color: #009900; font-style: italic;">//'S' only</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
<span style="color: #000000;">&#125;</span></div></td></tr></tbody></table></div>
<h5>What we do here is:</h5>
<ol>
<li>Attach a <strong>KeyboardEvent</strong> listener to the stage</li>
<li>Create a new String of the pressed keyboard character</li>
<li>Listen for a particular key press that triggers an action</li>
</ol>
<h4>Actionscript:</h4>
<div class="codecolorer-container actionscript3 dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:700px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="actionscript3 codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #004993;">stage</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">KeyboardEvent</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">KEY_UP</span><span style="color: #000066; font-weight: bold;">,</span> onKeyEvent<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span></div></td></tr></tbody></table></div>
<p>Here we create a new event listener and attach it to the stage. We define the listener to be of type <strong>KeyboardEvent</strong> and to listen for any <strong>KEY_UP</strong> events. Lastly we instruct the listener to call function <strong>onKeyEvent</strong> every time this event occurs. Roughly speaking, every time we release a keyboard key we do something.</p>
<h4>Actionscript:</h4>
<div class="codecolorer-container actionscript3 dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:700px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="actionscript3 codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #6699cc; font-weight: bold;">var</span> character<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">String</span> = <span style="color: #004993;">String</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">fromCharCode</span><span style="color: #000000;">&#40;</span>e<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">charCode</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span></div></td></tr></tbody></table></div>
<p>Once we are working within our event fired function, <strong>onKeyEvent</strong>, we have access to all of the information concerning that event through &#8216;<strong>e</strong>&#8216;. We can extract a lot from the event for just a simple button press, but for now we will just look at a particularly handy bit of info &#8211; <strong>charCode</strong>.</p>
<h5>CharCode</h5>
<p>A <strong>charCode</strong> is an ASCII code that represents a character on a keyboard.<em> (a,b,c,D,E,F,1,2,3,!,@,#..)</em> Each character has its own unique code &#8211; For instance: the charCode for &#8216;A&#8217; is 65, being different from &#8216;a&#8217; which is 97. Even a spacebar space has a charCode <em>(32)</em> because each space is a recognized character.</p>
<p><em>Note: Keyboard buttons like &#8216;Enter&#8217;, &#8216;Ctrl&#8217;, and &#8216;Num Lock&#8217; do not have charCodes. (They return a charCode of 0)</em></p>
<p>In the code snippet above we do something relatively complex to accomplish a task assumedly simple. We convert the typed keyboard character&#8217;s ASCII alter-ego into the character we understand intelligibly. Your OS does this constantly for you when in fact all your keyboard knows is which key is pressed. This is necessary for keyboards of multiple languages to function.</p>
<pre>fromCharCode(<a style="color: #0000cc; text-decoration: none;" href="http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/statements.html#..._(rest)_parameter">...</a> charCodes):String</pre>
<p>So, what we accomplish here is: <strong>fromCharCode &#8211; </strong>a method of the String class &#8211; takes a charCode, converts it and returns the respective character as a String which we then pass to a new variable <strong>character</strong>.</p>
<h4>Actionscript:</h4>
<div class="codecolorer-container actionscript3 dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:700px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br /></div></td><td><div class="actionscript3 codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>e<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">keyCode</span> == <span style="color: #000000; font-weight:bold;">66</span><span style="color: #000000;">&#41;</span><br />
<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span>character<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span> &nbsp; <span style="color: #009900; font-style: italic;">//'b' or 'B'</span><br />
<span style="color: #000000;">&#125;</span></div></td></tr></tbody></table></div>
<h5>KeyCode</h5>
<p>Now that we understand <strong>charCode</strong>, <strong>keyCode</strong> should be easy. Like briefly mentioned above, a keyCode is a number associated with a <span style="text-decoration: underline;">button </span>on your keyboard &#8211; contrary to a <span style="text-decoration: underline;">character</span> like that of a charCode. Working with a keyCode for keyboard driven events is more useful than charCodes because generally: for instance, when &#8216;X&#8217; is pressed you are not concerned with whether or not it is the capitalized form or not.</p>
<p>In the above &#8216;if statement&#8217;, we simply evaluate whether the <strong>keyCode </strong>input is equal to 66 <em>(The code for the &#8216;B&#8217; key)</em>. If this is true we print <strong>character</strong>,which we captured in the previous block, to the output screen. Since 66 in this case pertains only to the keyboard button for &#8216;B&#8217;, <strong>character </strong>could be either lowercase &#8216;b&#8217; or capital &#8216;B&#8217;.</p>
<p>You could replace this trace with anything! In the example at the top, <strong>character </strong>is placed inside of a text box and randomly attached to the stage.</p>
<h4>Actionscript:</h4>
<div class="codecolorer-container actionscript3 dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:700px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br /></div></td><td><div class="actionscript3 codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0033ff; font-weight: bold;">else</span> <span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>e<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">keyCode</span> == <span style="color: #000000; font-weight:bold;">83</span> <span style="color: #000066; font-weight: bold;">&amp;&amp;</span> e<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">shiftKey</span><span style="color: #000000;">&#41;</span><br />
<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span>character<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span> &nbsp; <span style="color: #009900; font-style: italic;">//'S' only</span><br />
<span style="color: #000000;">&#125;</span></div></td></tr></tbody></table></div>
<p>Here our code is a lot like the last block except this time, in addition to needing a particular keyCode, we are listening for the Shift key. The <strong>shiftKey</strong> Boolean is yet another <strong>KeyboardEvent </strong>attribute we have access to that tells us whether or not the Shift key is pressed. Ergo, if we press both the &#8216;S&#8217; key <em>(83) </em>and the Shift key we will satisfy the statement and trace <strong>character</strong>. In this case, since the Shift key is down, we can only expect a capital &#8216;S&#8217; <em>(Granted Caps Lock isn&#8217;t on <img src='http://www.bensilvis.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> )</em>.</p>
<p>Besides Shift, <strong>KeyboardEvent </strong>also lets us listen for Ctrl and Alt. Check out the <a title="Adobe live docs - KeyboardEvent" href="http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/events/KeyboardEvent.html" target="_blank">Adobe documentation</a> for all of the attributes.</p>
<h5>Concluding..</h5>
<p>By now you should hopefully have a better understanding of the <strong>KeyboardEvent </strong>class. Feel free to post or message me with any additional questions you might have!</p>
<p>The source to the example is available below</p>
<p><em>Note: When testing the example inside the Flash IDE, the internal shortcut keys tend to override your programmed events. To prevent this you will need to disable them.</em> <strong>Control > Disable Keyboard Shortcuts</strong></p>
<blockquote><address><span style="font-style: normal;"><a href="mailto:ben@bensilvis.com">Ben</a></span></address>
</blockquote>
<h5 style="text-align: center;"><a href="tutorials/key_codes/downloads/key_codes.zip"><img class="alignnone" style="margin-top: 20px; margin-bottom: -12px; margin-left: 4px; margin-right: 4px; border: 0px;" title="Download Source" src="/site_images/flash_icon_44.png" alt="" width="44" height="44" />Download Source</a></h5>
]]></content:encoded>
			<wfw:commentRss>http://www.bensilvis.com/?feed=rss2&amp;p=146</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>AS3: Applying a BlurFilter Effect</title>
		<link>http://www.bensilvis.com/?p=64</link>
		<comments>http://www.bensilvis.com/?p=64#comments</comments>
		<pubDate>Tue, 06 Oct 2009 17:01:24 +0000</pubDate>
		<dc:creator>Ben</dc:creator>
				<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[BlurFilter]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.bensilvis.com/?p=64</guid>
		<description><![CDATA[So I&#8217;m sitting here, having recently misplaced my glasses, and it&#8217;s become quite apparent to me that I can&#8217;t see. Somehow in the midst of the unfortunate circumstances I find the inspiration for my first tutorial: The BlurFilter applies a blur to visual objects in your scene. It is one of AS3&#8242;s various filter effects including: Glow, [...]]]></description>
			<content:encoded><![CDATA[<p>So I&#8217;m sitting here, having recently misplaced my glasses, and it&#8217;s become quite apparent to me that I can&#8217;t see. Somehow in the midst of the unfortunate circumstances I find the inspiration for my first tutorial:</p>
<p><span id="more-64"></span></p>
<p><object style="width: 500px; height: 300px;" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="500" height="300" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="menu" value="false" /><param name="src" value="tutorials/blurry_vision/blurry_vision.swf" /><embed style="width: 500px; height: 300px;" type="application/x-shockwave-flash" width="500" height="300" src="tutorials/blurry_vision/blurry_vision.swf" menu="false"></embed></object></p>
<p>The BlurFilter applies a blur to visual objects in your scene.<br />
It is one of AS3&#8242;s various filter effects including: <strong>Glow</strong>, <strong>Drop </strong><strong>Shadow</strong>, and <strong>Bevel<br />
<span style="font-weight: normal;">Play around with slider in the example above and you will see something similar to a Gaussian blur effect.</span> </strong></p>
<p>Let&#8217;s look at some code then, shall we?</p>
<h4>Actionscript:</h4>
<div class="codecolorer-container actionscript3 dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:700px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br /></div></td><td><div class="actionscript3 codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.display</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">MovieClip</span><span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.filters</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">BitmapFilter</span><span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.filters</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">BitmapFilterQuality</span><span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.filters</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">BlurFilter</span><span style="color: #000066; font-weight: bold;">;</span></div></td></tr></tbody></table></div>
<p>These are all the imports we should need in our package to achieve a blur effect.</p>
<h4>Actionscript:</h4>
<div class="codecolorer-container actionscript3 dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:700px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br /></div></td><td><div class="actionscript3 codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">filter</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">BitmapFilter</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">BlurFilter</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">10</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000; font-weight:bold;">10</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #004993;">BitmapFilterQuality</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">HIGH</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #6699cc; font-weight: bold;">var</span> myFilters<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Array</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Array</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
myFilters<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">push</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">filter</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<br />
blurredMC<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">filters</span> = myFilters<span style="color: #000066; font-weight: bold;">;</span></div></td></tr></tbody></table></div>
<h5>What we do here is:</h5>
<ol>
<li>Create a new <strong>BitmapFilter </strong>of type <strong>BlurFilter</strong></li>
<li>Create an array which we add our filter to</li>
<li>Apply our filter array to a specified object</li>
</ol>
<h4>Actionscript:</h4>
<div class="codecolorer-container actionscript3 dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:700px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="actionscript3 codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">filter</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">BitmapFilter</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">BlurFilter</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">10</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000; font-weight:bold;">10</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #004993;">BitmapFilterQuality</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">HIGH</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span></div></td></tr></tbody></table></div>
<p>The <strong>BlurFilter </strong>constructor takes 3 parameters:</p>
<pre>BlurFilter(blurX:Number = 4.0, blurY:Number = 4.0, quality:int = 1)</pre>
<ul>
<li><strong>blurX</strong>: the amount of blur to apply in the X direction<em> (0 &#8211; 255)</em></li>
<li><strong>blurY</strong>: the amount of blur to apply in the Y direction <em>(0 &#8211; 255)</em></li>
<li><strong>quality</strong>: the number of blur passes <em>(LOW, MEDIUM, HIGH)</em></li>
</ul>
<p>The higher the blur values, the more blur you will see applied to your object. In the code above we set both of these values to 10. You can just as easily though blur in only the X or Y direction for a clever motion blur effect. Remember that the higher the blur value the more cpu intensive the blur becomes.</p>
<p><em>Note: The way that the blur passes are processed it is more efficient to use powers of 2. (2, 4, 8, 16, etc.)</em></p>
<p>You may have been confused that my example code has quality set to <strong>BitmapFilterQuality.HIGH</strong>, while the default parameters directly above show quality as an integer of 1. The <strong>quality</strong> in itself is just a value telling Flash how many times to apply the blur. Generally the pre-made quality types are sufficient, but if you need you can replace BitmapFilterQuality with an integer (0 &#8211; 15).</p>
<h4>Actionscript:</h4>
<div class="codecolorer-container actionscript3 dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:700px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br /></div></td><td><div class="actionscript3 codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #6699cc; font-weight: bold;">var</span> myFilters<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Array</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Array</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
myFilters<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">push</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">filter</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span></div></td></tr></tbody></table></div>
<p>Here we create a new array <strong>myFilters</strong> and then push the BlurFilter we previously created into it.  If we were planning on applying multiple kinds of filters to our object, we would add each of them to this array.</p>
<h4>Actionscript:</h4>
<div class="codecolorer-container actionscript3 dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:700px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="actionscript3 codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">blurredMC<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">filters</span> = myFilters<span style="color: #000066; font-weight: bold;">;</span></div></td></tr></tbody></table></div>
<p>Here we apply all of our filters <em>(in this case the BlurFilter)</em> to DisplayObject <strong>blurredMC</strong>.</p>
<h5>In Flash</h5>
<p>I guess I should mention that if you don&#8217;t want to deal with all this code crap, you can still apply filters to your object via the Filter&#8217;s tab. (Flash 8 and higher). You can find this at <strong>Window &gt; Properties &gt; Filters</strong></p>
<p><img class=" alignnone" title="Blur Filter" src="/tutorials/blurry_vision/blur_filter.png" alt="" width="294" height="114" /></p>
<h5>Concluding..</h5>
<p>So wraps up my first ever Flash tutorial! I hope it was exceedingly beneficial to you. Well, even if it wasn&#8217;t I would love to hear any constructive criticism.</p>
<p>I&#8217;ve provided the source to the example above. Enjoy!</p>
<blockquote><address><span style="font-style: normal;"><a href="mailto:ben@bensilvis.com">Ben</a></span></address>
</blockquote>
<h5 style="text-align: center;"><a href="tutorials/blurry_vision/downloads/blurry_vision.zip"><img class="alignnone" style="margin-top: 20px; margin-bottom: -12px; margin-left: 4px; margin-right: 4px; border: 0px;" title="Download Source" src="/site_images/flash_icon_44.png" alt="" width="44" height="44" />Download Source</a></h5>
]]></content:encoded>
			<wfw:commentRss>http://www.bensilvis.com/?feed=rss2&amp;p=64</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>First Game Post</title>
		<link>http://www.bensilvis.com/?p=15</link>
		<comments>http://www.bensilvis.com/?p=15#comments</comments>
		<pubDate>Mon, 05 Oct 2009 04:53:00 +0000</pubDate>
		<dc:creator>Ben</dc:creator>
				<category><![CDATA[Flash]]></category>
		<category><![CDATA[Games]]></category>
		<category><![CDATA[game]]></category>

		<guid isPermaLink="false">http://www.bensilvis.com/?p=15</guid>
		<description><![CDATA[This is mainly just a test to see how well Flash works with my blog. So far it looks like there won&#8217;t be any problems. I have a LOT of silly little experiments collecting dust in the annexes of my many hard drives, so I thought I&#8217;d share one while I am tinkering - Here&#8217;s the [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: left;">This is mainly just a test to see how well Flash works with my blog. So far it looks like there won&#8217;t be any problems. <img src='http://www.bensilvis.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p style="text-align: left;">I have a LOT of silly little experiments collecting dust in the annexes of my many hard drives, so I thought I&#8217;d share one while I am tinkering -</p>
<p>Here&#8217;s the start of space shooter game I&#8217;d like to overhaul someday. It&#8217;s one of the first pseudo-real games I made. Theres a pretty long story behind it, but I won&#8217;t get into any of that right now..<br />
It still has a working title, but for now I call it:</p>
<p><span id="more-15"></span></p>
<h2 style="text-align: center;"><strong><strong>Super Space Ass Whoop!<br />
</strong></strong></h2>
<p style="text-align: center;"><object style="width: 500px; height: 500px;" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="500" height="500" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="quality" value="high" /><param name="scale" value="exactfit" /><param name="src" value="SSAW_loader.swf" /><embed style="width: 500px; height: 500px;" type="application/x-shockwave-flash" width="500" height="500" src="SSAW_loader.swf" scale="exactfit" quality="high"></embed></object></p>
<h6 style="text-align: center;">Controls:</h6>
<p style="text-align: center;">Arrow Keys &#8211; move<br />
Space Bar &#8211; shoots</p>
<p style="text-align: center;"><em>(By the way, it doesn&#8217;t really ever end)</em></p>
]]></content:encoded>
			<wfw:commentRss>http://www.bensilvis.com/?feed=rss2&amp;p=15</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>I&#8217;m a Blog Now</title>
		<link>http://www.bensilvis.com/?p=8</link>
		<comments>http://www.bensilvis.com/?p=8#comments</comments>
		<pubDate>Mon, 05 Oct 2009 03:24:18 +0000</pubDate>
		<dc:creator>Ben</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[ben silvis]]></category>

		<guid isPermaLink="false">http://www.bensilvis.com/?p=8</guid>
		<description><![CDATA[BenSilvis.com is a blog now! In the past this website has been a random collection of projects I&#8217;ve either spearheaded or been a part of. I now plan to present at least a semi-professional portfolio and attempt to give a little know-how back to the web community. Oh yeah I guess I forgot to say [...]]]></description>
			<content:encoded><![CDATA[<h3 style="font-size: 1.17em;">BenSilvis.com is a blog now!</h3>
<p>In the past this website has been a random collection of projects I&#8217;ve either spearheaded or been a part of. I now plan to present at least a semi-professional portfolio and attempt to give a little know-how back to the web community.</p>
<p>Oh yeah I guess I forgot to say that I&#8217;m a Flash Developer currently spreading my wings. I&#8217;m obsessed with game design and I have ties in 3D, game engines, web design, yada yada.. I&#8217;m not too savvy with the whole blogging thing, but this seems like a great place to host  new projects, ideas, tutorials, and other things that tickle my fancy.</p>
<p>Well.. I&#8217;ll flesh this things out over the next little while. Enjoy!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.bensilvis.com/?feed=rss2&amp;p=8</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
