<?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>dispatchEvent() Blog™ &#187; AS3</title>
	<atom:link href="http://dispatchevent.org/category/as3/feed/" rel="self" type="application/rss+xml" />
	<link>http://dispatchevent.org</link>
	<description>Collective thoughts on the Flash Platform, iOS, Unity, and any other technology we use.</description>
	<lastBuildDate>Wed, 14 Jul 2010 15:06:06 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>ActionScript 3.0 Bible Series at FlashCoders NY</title>
		<link>http://dispatchevent.org/roger/actionscript-3-0-bible-series-at-flashcoders-ny/</link>
		<comments>http://dispatchevent.org/roger/actionscript-3-0-bible-series-at-flashcoders-ny/#comments</comments>
		<pubDate>Wed, 14 Jul 2010 15:06:06 +0000</pubDate>
		<dc:creator>Roger Braunstein</dc:creator>
				<category><![CDATA[AS3]]></category>
		<category><![CDATA[Conferences and User Groups]]></category>
		<category><![CDATA[News]]></category>

		<guid isPermaLink="false">http://dispatchevent.org/?p=859</guid>
		<description><![CDATA[Hey readers! I&#8217;ve been doing a series of dynamic, engaging, interactive, educational and disturbingly sexy workshops on topics from the new AS3 Bible. These workshops are hosted by the fearless FlashCoders NY user group. If you&#8217;re in NYC and want &#8230; <a href="http://dispatchevent.org/roger/actionscript-3-0-bible-series-at-flashcoders-ny/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Hey readers!</p>
<p>I&#8217;ve been doing a series of dynamic, engaging, interactive, educational and disturbingly sexy workshops on topics from the new <a href="http://actionscriptbible.com/">AS3 Bible</a>. These workshops are hosted by the fearless <a href="http://www.flashcodersny.com/">FlashCoders NY</a> user group. If you&#8217;re in NYC and want to drop by, check the meeting announcements. Having a copy of the book isn&#8217;t required! (But I sure recommend it, duh!)</p>
<p>I give the workshops every other Wednesday, give or take. Tonight&#8217;s is on <b>Pixel Bender</b>! Be sure to stop by!</p>
<p>As an aside, you may be curious what I&#8217;m up to. Actually, you aren&#8217;t, but I&#8217;ll tell you anyway because I&#8217;m amped. I&#8217;m juggling several iOS projects on iPhone 4 and iPad, I&#8217;m days away from launching an amazing collaboration with artists/directors Radical Friend, the creators of openFrameworks, and the talented folks at The Science Project &#8212; an experimental art piece driven by Unity and oF. And what the hell, I&#8217;m trying to learn 3D modeling (again) (finally). I heard that I might be giving a series of iOS dev video training episodes&#8230; stay tuned if you want to hear more!</p>
]]></content:encoded>
			<wfw:commentRss>http://dispatchevent.org/roger/actionscript-3-0-bible-series-at-flashcoders-ny/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Killing 3D Transforms</title>
		<link>http://dispatchevent.org/roger/killing-3d-transforms/</link>
		<comments>http://dispatchevent.org/roger/killing-3d-transforms/#comments</comments>
		<pubDate>Sat, 03 Apr 2010 07:33:31 +0000</pubDate>
		<dc:creator>Roger Braunstein</dc:creator>
				<category><![CDATA[AS3]]></category>
		<category><![CDATA[Tips, Tricks, and Hacks]]></category>

		<guid isPermaLink="false">http://dispatchevent.org/?p=848</guid>
		<description><![CDATA[Here&#8217;s a quick tip. If you&#8217;ve ever dealt with 3D in Flash Player 10+ you&#8217;ll know that DisplayObjects in 3D in Flash Player are bitmap-cached; only the bitmap representation is transformed in 3D. Recently I was working on a site &#8230; <a href="http://dispatchevent.org/roger/killing-3d-transforms/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s a quick tip. If you&#8217;ve ever dealt with 3D in Flash Player 10+ you&#8217;ll know that DisplayObjects in 3D in Flash Player are bitmap-cached; only the bitmap representation is transformed in 3D. Recently I was working on a site where objects landed on the stage, but after landing, they appeared really muddy, which was obvious when you looked at the text they contained. Even though they were completely flat, they were still using bitmap proxies! Not great.</p>
<p>Turning a DisplayObject into a 3D DisplayObject is as simple as assigning a value to its rotationX, rotationY, rotationZ, or z properties. But how do you turn it back? I tried giving all these properties a value of 0, but after their animations completed they were already 0. An object at z=0 still renders in 3D, using bitmap caching. Setting these values to NaN was no better.</p>
<p>The solution I found is to null out the 3D transformation matrix and assign a 2D transformation matrix. This can be accomplished like so for a DisplayObject named <code>sprite</code>:</p>
<p><code>var matrix:Matrix = new Matrix();<br />
matrix.translate(sprite.x, sprite.y);<br />
matrix.rotate(sprite.rotationZ);<br />
sprite.transform.matrix3D = null;<br />
sprite.transform.matrix = matrix;</code></p>
<p>In this code I simply drop the transformations that are inapplicable to 2D space. This is fine when your object is already coplanar with the stage.</p>
<p>Anyway, quick tip if this ever had you scratching your head too.</p>
]]></content:encoded>
			<wfw:commentRss>http://dispatchevent.org/roger/killing-3d-transforms/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Side-by-side comparison shows blitting isn&#8217;t a huge speed bump</title>
		<link>http://dispatchevent.org/mims/side-by-side-comparison-shows-blitting-isnt-a-huge-speed-bump/</link>
		<comments>http://dispatchevent.org/mims/side-by-side-comparison-shows-blitting-isnt-a-huge-speed-bump/#comments</comments>
		<pubDate>Sun, 21 Mar 2010 18:43:55 +0000</pubDate>
		<dc:creator>Mims H Wright</dc:creator>
				<category><![CDATA[AS3]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tips, Tricks, and Hacks]]></category>
		<category><![CDATA[Videogames]]></category>
		<category><![CDATA[animation]]></category>
		<category><![CDATA[bit]]></category>
		<category><![CDATA[blit]]></category>
		<category><![CDATA[oldschool]]></category>
		<category><![CDATA[sprite]]></category>

		<guid isPermaLink="false">http://dispatchevent.org/?p=847</guid>
		<description><![CDATA[This isn&#8217;t a new article, but it touches on a topic I&#8217;ve heard a lot about recently. That is, pixel &#8216;blitting&#8216;. Blitting is an old school process used in sprite-based games of essentially erasing a spot in a bitmap image &#8230; <a href="http://dispatchevent.org/mims/side-by-side-comparison-shows-blitting-isnt-a-huge-speed-bump/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>This isn&#8217;t a new article, but it touches on a topic I&#8217;ve heard a lot about recently. That is, pixel &#8216;<a href="http://en.wikipedia.org/wiki/Bit_blit">blitting</a>&#8216;. Blitting is an old school process used in sprite-based games of essentially erasing a spot in a bitmap image based on a sprite&#8217;s alpha and using a bitwise operator to draw the sprite into that bitmap image. Most graphics systems do this transparently to the user in some fashion (pardon the pun). <a href="http://www.8bitrocket.com/">8-Bit Rocket</a> has a <a href="http://www.8bitrocket.com/newsdisplay.aspx?newspage=13430">good introduction</a> to these concepts with regards to Flash, but read on for the important part of this post.</p>
<p><a href="http://www.photonstorm.com/archives/160/is-pixel-blitting-in-as3-really-worth-the-effort"><img class="alignnone" title="blitTest" src="http://www.photonstorm.com/wp-content/uploads/2008/09/sshot-2008-09-19-1.png" alt="" width="300" /></a></p>
<p><a href="http://www.photonstorm.com/archives/160/is-pixel-blitting-in-as3-really-worth-the-effort">Read the article on Photon Storm</a></p>
<p>The article shows that Flash Player is already fairly optimized for graphical composition operations when compared to blitting. Both showed similar framerates. However, the cacheAsBitmap operation caused an exponential increase in memory consumption when the number of sprites on stage increased. Ironically, the comparison was run by the creator of the <a href="http://code.google.com/p/pixelblitz/">Pixel Blitz</a> library who, I&#8217;m sure, had a difficult time presenting the results.</p>
<p>There are some good details in the article so if you&#8217;re interested, read the whole thing before you decide if you agree.</p>
<p>Have you done any work with bit blitting or sprite sheet animation? Have you found it to be beneficial? Tell us your thoguhts.</p>
]]></content:encoded>
			<wfw:commentRss>http://dispatchevent.org/mims/side-by-side-comparison-shows-blitting-isnt-a-huge-speed-bump/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>AS3 Bible translated to 繁體中文</title>
		<link>http://dispatchevent.org/mims/as3-bible-translated-to-chinese/</link>
		<comments>http://dispatchevent.org/mims/as3-bible-translated-to-chinese/#comments</comments>
		<pubDate>Thu, 18 Mar 2010 22:08:11 +0000</pubDate>
		<dc:creator>Mims H Wright</dc:creator>
				<category><![CDATA[AS3]]></category>
		<category><![CDATA[In Real Life]]></category>
		<category><![CDATA[as3bible]]></category>
		<category><![CDATA[book]]></category>
		<category><![CDATA[chinese]]></category>
		<category><![CDATA[free as in beer]]></category>

		<guid isPermaLink="false">http://dispatchevent.org/?p=843</guid>
		<description><![CDATA[Yesterday, completely unannounced, a package arrived at my doorstep containing two copies of the ActionScript 3.0 Bible Traditional Chinese Edition! Well, that&#8217;s pretty Zang if you ask me! I&#8217;m going to keep one as a trophy but if you can &#8230; <a href="http://dispatchevent.org/mims/as3-bible-translated-to-chinese/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Yesterday, completely unannounced, a package arrived at my doorstep containing two copies of the <a href="http://www.amazon.com/ActionScript-3-0-Bible-Roger-Braunstein/dp/0470135603">ActionScript 3.0 Bible</a> <em>Traditional Chinese Edition! </em>Well, that&#8217;s pretty Zang if you ask me!</p>
<p><a href="http://dispatchevent.org/wp-content/uploads/2010/03/chinese-bible.jpg"><img class="alignnone size-medium wp-image-844" title="chinese-bible" src="http://dispatchevent.org/wp-content/uploads/2010/03/chinese-bible-300x168.jpg" alt="" width="300" height="168" /></a></p>
<p>I&#8217;m going to keep one as a trophy but if you can read chinese and you&#8217;d like a copy, let me know and I&#8217;ll send you a present*!</p>
<p><small>*(If you&#8217;re outside the US, i&#8217;ll ask you to pay for shipping)</small></p>
]]></content:encoded>
			<wfw:commentRss>http://dispatchevent.org/mims/as3-bible-translated-to-chinese/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>IDisplayObject? &#8211; Getting around the lack of an interface for the DisplayObject in Flash</title>
		<link>http://dispatchevent.org/mims/idisplayobject/</link>
		<comments>http://dispatchevent.org/mims/idisplayobject/#comments</comments>
		<pubDate>Mon, 15 Mar 2010 02:27:22 +0000</pubDate>
		<dc:creator>Mims H Wright</dc:creator>
				<category><![CDATA[AS3]]></category>
		<category><![CDATA[Architecture]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tips, Tricks, and Hacks]]></category>
		<category><![CDATA[displayobject]]></category>
		<category><![CDATA[hack]]></category>
		<category><![CDATA[idisplayobject]]></category>
		<category><![CDATA[interface]]></category>
		<category><![CDATA[workaround]]></category>

		<guid isPermaLink="false">http://dispatchevent.org/?p=838</guid>
		<description><![CDATA[Interfaces are amazing things. When I was young and green, I didn&#8217;t understand their purpose, but after working with them for a while, I will defend their use to the end. I try to create lots of interfaces early on &#8230; <a href="http://dispatchevent.org/mims/idisplayobject/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><a href="http://dispatchevent.org/wp-content/uploads/2010/03/DisplayObject_subclasses.png"><img class="alignnone size-medium wp-image-840" title="DisplayObject_subclasses" src="http://dispatchevent.org/wp-content/uploads/2010/03/DisplayObject_subclasses-300x156.png" alt="" width="300" height="156" /></a></p>
<p>Interfaces are amazing things.  When I was young and green, I didn&#8217;t understand their purpose, but after working with them for a while, I will defend their use to the end. I try to create lots of interfaces early on in my projects and I find that by keeping things flexible, it saves more time despite the extra typing, pardon the double entendre.</p>
<p>But this is not an article about why interfaces are so great. No, this is an article about the mysterious gap in the Flash Player API with regards to an interface for DisplayObjects. If you have a class typed as something like <code>IView</code>, there&#8217;s no way to enure that <code>IView</code> can be a parameter of display list functions like <code> addChild()</code>.</p>
<p><span id="more-838"></span>Why can&#8217;t we guarantee that something is a DisplayObject through interfaces? There is a practical answer. The inner workings of the Flash Player are slowing unfolding to us with each version. We now have low level access to sound, byte streams, and text rendering. However, the display list still relies exclusively on one of the only truly abstract classes in ActionScript 3.0, the DisplayObject. Because a low-level drawing engine is not exposed to AS3 programmers, we don&#8217;t know how it works and we cannot guarantee through an interface that an arbitrary class can be drawn.</p>
<p>Fortunately, there are some workarounds that can help, each with their own strengths and weaknesses. Personally, I think #2 is the way to go. It&#8217;s practically dripping with polymorphic goodness.</p>
<ol>
<li>
<h3>Make your interface inherit from IBitmapDrawable and IEventDispatcher</h3>
<p>DisplayObject is the only class that uses both of these two interfaces so by combining them in your interface, you can sort of be sure you&#8217;re working with a DisplayObject. Still, this is my least favorite solution</p>
<h4>Pros:</h4>
<ul>
<li>Uses interfaces exclusively.</li>
<li>Only a DisplayObject would implement both of these.</li>
</ul>
<h4>Cons:</h4>
<ul>
<li>There&#8217;s nothing to stop you from using this to create a class that isn&#8217;t a DisplayObject and that could cause problems.</li>
<li>You would still have to type cast to DisplayObject to use this as an argument for <code>addChild()</code>.</li>
</ul>
</li>
<li>
<h3>Create an interface that exposes a method to return the object as a DisplayObject</h3>
<p>My personal favorite solution. It not only solves the problem of guaranteeing that a class can go in the display list, it doesn&#8217;t even require that the class be a DisplayObject.</p>
<p>Here&#8217;s an example of what this would look like. <a href="http://gist.github.com/332408">IDisplayObject.as</a></p>
<h4>Pros:</h4>
<ul>
<li>Flexible solution that conforms to good OOD principles.</li>
<li>Uses interfaces exclusively.</li>
<li>Easy to apply. You can usually implement it with a single line of code: <code>return this;</code></li>
</ul>
<h4>Cons:</h4>
<ul>
<li>Requires you to use <code>asDisplayObject()</code> every time you want to use the object as a DisplayObject.</li>
<li>Calling <code>foo.asDisplayObject()</code> would throw an exception if <code>foo</code> was null so additional checks may be necessary.</li>
</ul>
</li>
<li>
<h3>Use an <a href="http://en.wikipedia.org/wiki/Abstract_type">abstract class</a> that inherits from DisplayObject</h3>
<p>Not an ideal solution primarily because Flash doesn&#8217;t have true abstract classes (although <a href="http://dispatchevent.org/mims/abstract-classes-in-as3/">there are workarounds</a>) but still totally valid.</p>
<h4>Pros:</h4>
<ul>
<li>Guarantees that the object is a DisplayObject. No type casting required.</li>
<li>Abstract classes can be treated virtually the same as an interface for practical use.</li>
</ul>
<h4>Cons:</h4>
<ul>
<li>No true abstract classes in Flash.</li>
<li>Not as flexible as an interface-based solution since it forces you to inherit from DisplayObject.</li>
</ul>
</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://dispatchevent.org/mims/idisplayobject/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Copy text to clipboard in AS3</title>
		<link>http://dispatchevent.org/mims/as3-clipboard/</link>
		<comments>http://dispatchevent.org/mims/as3-clipboard/#comments</comments>
		<pubDate>Tue, 02 Mar 2010 06:27:34 +0000</pubDate>
		<dc:creator>Mims H Wright</dc:creator>
				<category><![CDATA[AS3]]></category>
		<category><![CDATA[Tips, Tricks, and Hacks]]></category>
		<category><![CDATA[clipboard]]></category>
		<category><![CDATA[copypasta]]></category>

		<guid isPermaLink="false">http://dispatchevent.org/?p=825</guid>
		<description><![CDATA[Here&#8217;s how you do it! http://mandarin.no/as3/as3-snippet-1-copy-text-to-clipboard/]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s how you do it!<br />
<a href="http://mandarin.no/as3/as3-snippet-1-copy-text-to-clipboard/">http://mandarin.no/as3/as3-snippet-1-copy-text-to-clipboard/</a></p>
]]></content:encoded>
			<wfw:commentRss>http://dispatchevent.org/mims/as3-clipboard/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>KitchenSync Version 2.0 is now available!</title>
		<link>http://dispatchevent.org/mims/kitchensync-v2-released/</link>
		<comments>http://dispatchevent.org/mims/kitchensync-v2-released/#comments</comments>
		<pubDate>Tue, 09 Feb 2010 03:14:49 +0000</pubDate>
		<dc:creator>Mims H Wright</dc:creator>
				<category><![CDATA[AS3]]></category>
		<category><![CDATA[News]]></category>
		<category><![CDATA[physics & motion]]></category>
		<category><![CDATA[animation]]></category>
		<category><![CDATA[KitchenSync]]></category>
		<category><![CDATA[launch]]></category>
		<category><![CDATA[tween]]></category>
		<category><![CDATA[version2.0]]></category>

		<guid isPermaLink="false">http://dispatchevent.org/?p=809</guid>
		<description><![CDATA[I&#8217;m very proud to announce that the second version of KitchenSync has arrived! What started as a labour of love about 3 years ago has finally gotten its first much needed upgrade. Just about everything in this version is newer &#8230; <a href="http://dispatchevent.org/mims/kitchensync-v2-released/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m very proud to announce that the second version of KitchenSync has arrived! What started as a labour of love about 3 years ago has finally gotten its first much needed upgrade. Just about everything in this version is newer and more streamlined. The entire system for greater simplicity and practical functionality.</p>
<p>This version will not be backwards compatible, however, there&#8217;s finally some <a href="http://kitchensync.as3lib.org/manual">Decent Documentation</a>!!! There&#8217;s also a <a href="http://kitchensync.as3lib.org">new blog</a> devoted to KS.</p>
<p><a href="http://kitchensynclib.googlecode.com">GO GET IT NOW!</a></p>
<p>Here&#8217;s some of the new features. You can view all of them by checking out the <a href="http://kitchensynclib.googlecode.com/svn/trunk/docs/changelog.txt">change log</a>.</p>
<ul>
<li>Usability
<ul>
<li>REFACTORED all classes with usability and stability in mind.</li>
<li>UPDATED all ASDocs</li>
<li>ADDED several new methods in the TweenFactory for easy tween creation.</li>
</ul>
</li>
<li>Synchronizer and Time
<ul>
<li>REFACTORED The way time is handled throughout.</li>
<li>ADDED ISynchronizerCore and example core classes. This allows advanced users to switch between different methods of timing (e.g. enterframe or timer based)</li>
<li>ADDED FrameRateUtil for getting instantaneous and averaged framerates of the system. The FrameRateView now makes use of this class and has more options.</li>
</ul>
</li>
<li>Action Architecture
<ul>
<li>REFACTORED action classes and extracted several interfaces from them.</li>
<li>ADDED jumpToTime and jumpByTime functionality to actions.</li>
<li>ADDED Looping via a new group called KSLooper.</li>
<li>REMOVED complicated and rarely used action triggers from AbstractAction.</li>
<li>ADDED togglePause() method and progress variable to AbstractAction (for getting percentage complete of an action).</li>
</ul>
</li>
<li>Tweens
<ul>
<li>CHANGED constructors in KSTween. Emphasis is now on using TweenFactory.</li>
<li>IMPROVED Object parser in TweenFactory</li>
</ul>
</li>
<li>Action Groups
<ul>
<li>ADDED Syntactic sugar to the group constructors. using an array in the constructor for a parallel group adds a sequence and vice versa.</li>
<li>ADDED totalDuration to groups to show the duration of all child actions in a group.</li>
<li>ADDED KSRandomGroup.</li>
<li>REMOVED KSSteppedSequenceGroup because it seemed kinda useless.</li>
</ul>
</li>
<li>Loading Actions
<ul>
<li>REFACTORED Loading actions. Now they&#8217;re much easier to use and more powerful.</li>
<li>ADDED KSLoadQueue for quickly creating a class to load files from the network in a batch.</li>
<li>ADDED the resultList property to the ILoaderAction interface so that you can quickly access the loaded files in a batch.</li>
</ul>
</li>
<li>Misc
<ul>
<li>RENAMED several classes and reworked package structure</li>
<li>ADDED KSAsynchronousIteration for running processor-intensive for loops spaced out over a period of time so that they are essentially asynchronous.</li>
<li>FIXED several minor and major bugs.</li>
</ul>
</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://dispatchevent.org/mims/kitchensync-v2-released/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Using %tokens% with Flex resource bundles</title>
		<link>http://dispatchevent.org/mims/using-tokens-with-flex-resource-bundles/</link>
		<comments>http://dispatchevent.org/mims/using-tokens-with-flex-resource-bundles/#comments</comments>
		<pubDate>Tue, 08 Dec 2009 19:26:08 +0000</pubDate>
		<dc:creator>Mims H Wright</dc:creator>
				<category><![CDATA[AS3]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[Tips, Tricks, and Hacks]]></category>
		<category><![CDATA[resource bundles]]></category>
		<category><![CDATA[strings]]></category>
		<category><![CDATA[token]]></category>

		<guid isPermaLink="false">http://dispatchevent.org/?p=796</guid>
		<description><![CDATA[When I use Flex, I make extensive use of Resource Bundles for all but the most basic applications. In short, they allow you to keep all of your static string values (and non-string values) separated out of your code. That &#8230; <a href="http://dispatchevent.org/mims/using-tokens-with-flex-resource-bundles/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>When I use Flex, I make extensive use of <a href="http://help.adobe.com/en_US/Flex/4.0/UsingSDK/WS2db454920e96a9e51e63e3d11c0bf69084-7fcf.html">Resource Bundles</a> for all but the most basic applications. In short, they allow you to keep all of your static string values (and non-string values) separated out of your code. That way, down the line it&#8217;s a very easy change when you want to localize the site in another language, rebrand it for another audience, or when a client asks you to change a button label or error message. Granted, this level of detail isn&#8217;t always necessary but I think it&#8217;s a good habit to get into even if it&#8217;s overkill for some projects.</p>
<p>But resource bundles can&#8217;t help you in every scenario. I often find myself needing to construct a sentence with live data inserted. For this, the resource strings are less helpful. For example, how would you write:</p>
<p><code>"At " + time + ", you received a message from " + userName + "."</code></p>
<p>With resource strings, you would have to do something like</p>
<pre>at=At
message=, you received a message from
period=.</pre>
<p>Which completely negates the whole point of using resource bundles. What to do?</p>
<h3>Token replacement</h3>
<p><img class="size-full wp-image-799 alignright" title="tokens" src="http://dispatchevent.org/wp-content/uploads/2009/12/tokens.jpg" alt="tokens" width="155" height="125" /><br />
What I really wanted was a way to inject data into the string. I&#8217;ve seen this done by use of tokens in other languages like <a href="http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/Strings/Articles/FormatStrings.html#//apple_ref/doc/uid/20000943">Objective-C</a> so I thought I&#8217;d give it a try. The result was surprisingly lightweight.</p>
<p>I created this utility that lets you use tokens (delimited by % signs) that can be replaced by live data when you retrieve the resource. Using this class, the example above would look more like this:</p>
<pre>receivedMessage=At %time%, you received a message from %userName%.

// then in your code
trace(ResourceStringUtil.getResoureceStringWithTokens("receivedMessage", {time: "11:49", userName: "Mims"}));

// displays
At 11:49, you received a message from Mims.</pre>
<p>Here&#8217;s the source code Feel free to use it and modify it for your purposes.</p>
<p><script src="http://gist.github.com/251904.js?file=ResourceStringUtil.as"></script></p>
]]></content:encoded>
			<wfw:commentRss>http://dispatchevent.org/mims/using-tokens-with-flex-resource-bundles/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>The Deepening &#8211; Select Your Own Adventure Edition</title>
		<link>http://dispatchevent.org/mims/the-deepening-select-your-own-adventure-edition/</link>
		<comments>http://dispatchevent.org/mims/the-deepening-select-your-own-adventure-edition/#comments</comments>
		<pubDate>Tue, 22 Sep 2009 19:13:39 +0000</pubDate>
		<dc:creator>Mims H Wright</dc:creator>
				<category><![CDATA[AS3]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[News]]></category>
		<category><![CDATA[Videogames]]></category>
		<category><![CDATA[atom]]></category>
		<category><![CDATA[funny]]></category>
		<category><![CDATA[video]]></category>
		<category><![CDATA[work]]></category>

		<guid isPermaLink="false">http://dispatchevent.org/?p=735</guid>
		<description><![CDATA[I&#8217;m very pleased to announce that one of my latest projects has just gone live. The Deepening &#8211; Select Your Own Adventure Edition has just gone up on Atom Films. Go give it a look, it&#8217;s hilarious. The video is &#8230; <a href="http://dispatchevent.org/mims/the-deepening-select-your-own-adventure-edition/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><a href="http://deepening.atom.com"><img class="alignnone size-full wp-image-737" title="Screen shot 2009-09-22 at 15.26.51 PM" src="http://dispatchevent.org/wp-content/uploads/2009/09/Screen-shot-2009-09-22-at-15.26.51-PM.png" alt="Screen shot 2009-09-22 at 15.26.51 PM" /></a></p>
<p>I&#8217;m very pleased to announce that one of my latest projects has just gone live. <a href="http://www.deepening.atom.com">The Deepening &#8211; Select Your Own Adventure Edition</a> has just gone up on Atom Films. Go give it a look, it&#8217;s hilarious.</p>
<p>The video is a Good Cop / Bad Cop action show where you get to choose what happens next. It was created by the brilliant <a href="http://theduncanbrothers.com/">Duncan Brothers</a> who asked me to help build the Flash portion of it. Enjoy!</p>
<p><a href="http://www.deepening.atom.com">deepening.atom.com</a></p>
]]></content:encoded>
			<wfw:commentRss>http://dispatchevent.org/mims/the-deepening-select-your-own-adventure-edition/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Fast Intro to Flash</title>
		<link>http://dispatchevent.org/calebjohnston/fast-intro-to-flash/</link>
		<comments>http://dispatchevent.org/calebjohnston/fast-intro-to-flash/#comments</comments>
		<pubDate>Wed, 08 Apr 2009 04:07:42 +0000</pubDate>
		<dc:creator>Caleb Johnston</dc:creator>
				<category><![CDATA[AIR]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[Meta]]></category>

		<guid isPermaLink="false">http://dispatchevent.org/?p=620</guid>
		<description><![CDATA[There is plenty of documentation, tutorials and explanation of Flash on the internet, blogs and books. However, it&#8217;s hard for me to find a good, concise article that covers Flash well. I&#8217;ve been exposing Flash 9 to a friend of &#8230; <a href="http://dispatchevent.org/calebjohnston/fast-intro-to-flash/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><span style="font-family: Arial;">There is plenty of documentation, tutorials and explanation of Flash on the internet, blogs and books. However, it&#8217;s hard for me to find a good, concise article that covers Flash well. I&#8217;ve been exposing Flash 9 to a friend of mine recently and I&#8217;ve decided that a concise explanation is something I should write. This post will be brief, high level and will cover Flash as a platform and not the Flash IDE. This article is also intended for those with previous exposure to technical concepts such as virtual machines and compilers.<br style="font-family: Arial;" /> </span><span id="more-620"></span><br style="font-family: Arial;" /><strong><span style="font-family: Arial;">Overview</span></strong><br />
<span style="font-family: Arial;">Flash is a platform for building and deploying interactive multimedia on the internet. The term Flash is ambiguous because it can be used to describe the runtime environment (Flash Player) or one of two IDEs targeting the player (Adobe Flash and Adobe Flex Builder). Flash currently uses an ECMA-based scripting language known as Actionscript for authoring. This same language is used across all of Adobe&#8217;s platform -which includes Adobe AIR, Adobe Flex, and Adobe Flash. Actionscript uses a <em>strong, static, safe</em> type system which makes it&#8217;s syntax more similar to Java than Javascript.</span></p>
<p><strong><span style="font-family: Arial;">Runtime Environment</span></strong></p>
<p><span style="font-family: Arial;"><strong>SWF Files</strong></span><br style="font-family: Arial;" /><span style="font-family: Arial;">Swf files are compressed, multipurpose </span><span style="font-family: Arial;">packages designed to contain the following data: raster and vector graphics, video, audio, text, fonts, animation, and actionscript code. Swfs can be loaded at runtime whenever necessary and can use both static and dynamic linking based upon the compiler settings. The runtime environment uses security sandboxes for loaded swf files. The security settings primarily limit the extent of filesystem access and cross-domain access that swf files can use. Adobe has <a id="jj8r" title="provided the swf file format spec" href="http://www.adobe.com/devnet/swf/">provided the swf file format spec</a> and has recently provided the <a id="k0w6" title="FLV video format spec" href="http://www.adobe.com/devnet/flv/">FLV video format spec</a> and <a id="drt9" title="has declared that they will provide the RTMP specs" href="http://www.adobe.com/aboutadobe/pressroom/pressreleases/200901/012009RTMP.html">has declared that they will provide the RTMP spec</a> as well.</span><br />
<span style="font-family: Arial;"><strong><br />
Flash Player &amp; AVM2</strong><br />
The Flash Player runtime is a very light-weight, virtual machine and handles system calls. The Flash Player includes both AVM1 (AS1, AS2, for Flash versions 1-8) and AVM2  (AS3 for Flash 9,10+) which processes compiled <a id="obh5" title="Adobe Byte Code" href="http://www.anotherbigidea.com/javaswf/avm2/AVM2Instructions.html">Adobe Bytecode</a> (ABC). The term AVM stands for &#8220;ActionScript Virtual Machine&#8221;<strong>*</strong>. </span><span style="font-family: Arial;">The Flash Player uses a Deferred Reference Counting (DRC) mechanism combined with a conservative mark/sweep garbage collector.</span><span style="font-family: Arial;"> The Adobe VM uses both JIT compiling and code interpretation and may benefit from compile-time bytecode optimizations based upon compiler input parameters. AS3 computational performance is generally closer to Java than Javascript [<a id="zbpr" title="AS3 performance vs Java vs Javascript" href="http://www.oddhammer.com/actionscriptperformance/set4/">source</a>]. The player also maintains a variable framerate that reflects the time consumed for each frame update [<a id="w455" title="source" href="http://www.onflex.org/ted/2005/07/flash-player-mental-model-elastic.php">source</a>]. Each frame update consists of multiple stages that include internal player processing, rendering, and author script processing. The program framerate is the principle metric reflecting application performance.<br />
<span style="font-family: Arial;"><br style="font-family: Arial;" /></span><span style="font-size: small;"><strong><span style="font-family: Arial;">Development Environment</span></strong></span></span></p>
<p><strong><span style="font-family: Arial;">Actionscript API</span></strong><br style="font-family: Arial;" /> <span style="font-family: Arial;">The Actionscript API is divided up amongst the Adobe AIR environment, the Flex environment, and the Flash environment and the Flash Player</span><span style="font-family: Arial;">.</span><span style="font-family: Arial;"> The source for all the packages are divided up amongst <a id="lc9l" title="swc files" href="http://livedocs.adobe.com/flex/3/html/building_overview_5.html">swc files</a>. Swc files are pre-compiled catalogs of classes and assets. They are similar to swf files but they are used for compile-time dependency resolution. Furthermore, the native swc&#8217;s predominantly contain code and not visual assets. An overview of the Actionscript packages is kept up to date in the <a id="y1w4" title="Adobe Actionscript 3 package summary" href="http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/package-summary.html">Adobe Live Docs</a>.</span></p>
<p><span style="font-family: Arial;"><strong>The Flash IDE</strong><br />
The Flash IDE was designed for designers, artists and animators. So, it is equiped with a layer system which reflects the stage display list and a hierarchical timeline which reflects the temporal nature of the runtime. The objects placed on the stage are represented by the timeline and can be &#8220;tweened&#8221; (using runtime interpolated 2D geometric transformations or custom shape morphs). The same timeline is also used for object creation and destruction. All stage instances are confined to a small set of native data types including: MovieClips, Sprites, Shapes, <span style="font-family: Arial;">TextFields, </span><span style="font-family: Arial;">Videos, and Bitmaps. The IDE also has a library that contains source objects for all stage instances and even instances that aren&#8217;t used on the stage. Flash projects often have several build targets (compiled swf files) that use parts of the project codebase. After Macromedia was purchased by Adobe, the Flash authoring environment has been updated to provide much better interoperability with the Adobe Creative Suite.</span></span></p>
<p><span style="font-family: Arial;"><strong>Adobe Flex Builder</strong><br />
Flex Builder is an Eclipse-based Actionscript IDE for writing and compiling swf files. Flex Builder is primarily designed to build &#8220;Flex&#8221; projects which can be written in an interface tagging language known as MXML. MXML is a declarative XML langauge used to define interface elements in a project. MXML files provide the &#8220;view&#8221; in the MVC software design pattern. Flex Builder also provides plenty of useful features like code completion, syntax formatting, active debugging, memory profiling, and project building that expose errors and warnings during development time. Because the Flex framework is not bundled with the Flash Player, Flex projects must statically link to all swc assets specific to Flex which amounts to greater than 500kb. This is very unnatractive for most users, thus Adobe has provided <a id="ilsr" title="means by which the download can be significantly decreased" href="http://onflash.org/ted/2008/01/flex-3-framework-caching.php">means by which the download can be significantly decreased</a>. Flex Builder is also used to deploy applications for the <a id="kwwz" title="Adobe AIR platform" href="http://en.wikipedia.org/wiki/Adobe_Integrated_Runtime">Adobe AIR platform</a>. Adobe AIR is a desktop runtime that combines <a id="ffsj" title="webkit" href="http://webkit.org/">webkit</a> with the AVM2 portion of the Flash Player. AIR can be likened to a closed source <a id="k10e" title="Java SE" href="http://en.wikipedia.org/wiki/Java_SE">Java SE</a> runtime but provides a smaller learning curve and greater accessibility than what comes with the Java ecosystem. </span></p>
<p><span style="font-family: Arial;"><strong>Compiler</strong><br />
Since Adobe has launched the Flex platform they have provided free access to their Flex and Actionscript compiler and SDK. They have also provided open source projects under the <a id="mz0t" title="Mozilla public license" href="http://en.wikipedia.org/wiki/Mozilla_Public_License">Mozilla public license</a>. The compiler (<a id="n5hv" title="Adobe Flex 3 Help - Compilers" href="http://livedocs.adobe.com/flex/3/html/help.html?content=compilers_13.html">mxmlc</a>) provides an impressive amount of flexibility and customization for swf compiling. It can be configured with command line options or a <a id="xwm0" title="configuration XML file" href="http://livedocs.adobe.com/flex/3/html/compilers_11.html#134938">configuration XML file</a>.</span></p>
<p><span style="font-family: Arial;"><em><strong>* </strong>correction 09-12-2009, I had previously stated that the term AVM meant &#8220;Adobe Virtual Machine&#8221;, this is a mixup. It actually means &#8220;ActionScript Virtual Machine&#8221;. Thanks dbam.</em></span></p>
]]></content:encoded>
			<wfw:commentRss>http://dispatchevent.org/calebjohnston/fast-intro-to-flash/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>AS3 number to hex converter</title>
		<link>http://dispatchevent.org/mims/as3-number-to-hex-converter/</link>
		<comments>http://dispatchevent.org/mims/as3-number-to-hex-converter/#comments</comments>
		<pubDate>Fri, 30 Jan 2009 01:38:44 +0000</pubDate>
		<dc:creator>Mims H Wright</dc:creator>
				<category><![CDATA[AS3]]></category>
		<category><![CDATA[Lab]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[as3lib]]></category>
		<category><![CDATA[bitwise]]></category>
		<category><![CDATA[converter]]></category>
		<category><![CDATA[hex]]></category>
		<category><![CDATA[hexadecimal]]></category>
		<category><![CDATA[hexit]]></category>
		<category><![CDATA[math]]></category>

		<guid isPermaLink="false">http://dispatchevent.org/?p=618</guid>
		<description><![CDATA[I&#8217;ve been working on some code for converting colors lately and getting quite into bitwise operations. In one of the demos I was making, I needed a quick and easy way to see the results of a color calculation. Of &#8230; <a href="http://dispatchevent.org/mims/as3-number-to-hex-converter/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been working on some code for converting colors lately and getting quite into bitwise operations. In one of the demos I was making, I needed a quick and easy way to see the results of a color calculation. Of course, simply tracing a number, even if it&#8217;s in the format 0xFF, will result in a digital number string &#8220;255&#8243;. I&#8217;ve always been a little stumped about how to do this calculation but I realized that it&#8217;s actually very easy to do with the <a href="http://livedocs.adobe.com/flex/3/langref/operators.html#bitwise_AND">&amp;</a> and <a href="http://livedocs.adobe.com/flex/3/langref/operators.html#bitwise_unsigned_right_shift">&gt;&gt;&gt;</a> bitwise operators.</p>
<p><code><br />
getNumberAsHexString(255); // 0xFF<br />
getNumberAsHexString(0xABCDEF); // 0xABCDEF<br />
getNumberAsHexString(0x00FFCC); // 0xFFCC<br />
getNumberAsHexString(0x00FFCC, 6); // 0x00FFCC - Uses 6 as a minimum number of hexits</code></p>
<p>I&#8217;ve added the code to the <a href="http://as3lib.org">AS3lib</a> as  as a global function. I may move it to a utility class later on. You can <a href="http://code.google.com/p/as3lib/source/browse/trunk/src/org/as3lib/math/getNumberAsHexString.as">view the source</a> on Google Code.</p>
<p>Incidentally, in a moment of bug-fixing frustration, I found <a href="http://kinderas.blogspot.com/2008/01/quicktip-actionscript-3-and-number.html">this article on O.C.A.S.</a> which describes how the same functionality is already built into the <code>toString()</code> method! However, the work isn&#8217;t completely wasted since my version includes the option to show leading zeroes and adds &#8220;<code>0x</code>&#8221; at the front of the result.</p>
]]></content:encoded>
			<wfw:commentRss>http://dispatchevent.org/mims/as3-number-to-hex-converter/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Q&amp;A &#8211; is, as, and type conversion</title>
		<link>http://dispatchevent.org/mims/is-and-as/</link>
		<comments>http://dispatchevent.org/mims/is-and-as/#comments</comments>
		<pubDate>Tue, 27 Jan 2009 07:41:08 +0000</pubDate>
		<dc:creator>Mims H Wright</dc:creator>
				<category><![CDATA[AS3]]></category>
		<category><![CDATA[Architecture]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tips, Tricks, and Hacks]]></category>
		<category><![CDATA[as]]></category>
		<category><![CDATA[coercion]]></category>
		<category><![CDATA[cs]]></category>
		<category><![CDATA[downcasting]]></category>
		<category><![CDATA[is]]></category>
		<category><![CDATA[lesson]]></category>
		<category><![CDATA[oop]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[types]]></category>

		<guid isPermaLink="false">http://dispatchevent.org/?p=591</guid>
		<description><![CDATA[A question from a reader gave me an excuse to write a huge rant about type conversions: I&#8217;m doing a little exercise in a book that makes a textfield in which each letter can only be entered once. Not very &#8230; <a href="http://dispatchevent.org/mims/is-and-as/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>A question from a reader gave me an excuse to write a huge rant about type conversions:</p>
<blockquote><p>I&#8217;m doing a little exercise in a book that makes a textfield in which each letter can only be entered once. Not very useful, more of a teaching thing really. However there&#8217;s a bit of code that says:</p>
<p><code>var tf:TextField = event.target as TextField;</code></p>
<p>I don&#8217;t understand this at all! What the hell is <code>event.target as TextField</code>? Anyway here&#8217;s the full code for context&#8217;s sake:</p>
<pre lang="actionscript3">
package com.FoundationAS3.ch6 {

	import flash.display.Sprite;
	import flash.text.TextField;
	import flash.text.TextFieldType;
	import flash.events.TextEvent;

	public class PreventDefaultTest extends Sprite {

		public function PreventDefaultTest() {
			var tf:TextField = new TextField();
			addChild(tf);

			tf.width = stage.stageWidth;
			tf.height = stage.stageHeight;
			tf.type = TextFieldType.INPUT;
			tf.wordWrap = true;

			tf.addEventListener(TextEvent.TEXT_INPUT, onTextFieldTextInput);
		}

		private function onTextFieldTextInput(event:TextEvent):void {
			var tf:TextField = event.target as TextField;
			if (tf.text.indexOf(event.text) &gt; -1) {
				event.preventDefault();
			}
		}
	}
}
</pre>
<p>Thanks,<br />
-Neal
</p></blockquote>
<p>My response after the jump.<br />
<span id="more-591"></span><br />
Neal,</p>
<p>First of all, let me say that preventing the default event handler, e.g. for a <code>TextField</code>, is rarely used and isn&#8217;t worth spending a lot of time discussing.</p>
<p>On the other hand, the <code>as</code> operator (along with the <code>is</code> operator and the type cast) are totally useful and you should learn all about them. First, let me cover <code>is</code>:</p>
<h3>is</h3>
<p><code><a href="http://livedocs.adobe.com/flex/2/langref/operators.html#is">is</a></code> is an operator that evaluates whether some object <em>is</em> of a certain type. The syntax is <code>[object] is [type]</code>. The term &#8216;type&#8217; is similar but not interchangeable with &#8216;class&#8217;. A type can refer to not only the class of an object, but also the super-classes it extends and interfaces it implements. An example:</p>
<pre lang="actionscript3">
var sprite:Sprite = new Sprite();
trace(sprite is Sprite); // true, because the sprite object is an instance of Sprite
trace(sprite is DisplayObject); // true, Sprite is a subclass of DisplayObject
trace(sprite is MovieClip); // false, MovieClip is a subclass of Sprite.
trace(sprite is IEventDispatcher); // true, DisplayObject extends EventDispatcher which implements IEventDispatcher.
</pre>
<p>Note that sprite is not a MovieClip but a MovieClip is a Sprite. Because MovieClip extends Sprite and contains all the same methods and properties as Sprite but not the other way around. </p>
<p>Note: the <code>instanceof</code> operator commonly used in AS1/2 still exists in AS3 but is for all practical purposes deprecated by <code>is</code> (although still useful if you&#8217;re manipulating prototypes). <code>typeof</code> is also related but shouldn&#8217;t be used in place of <code>is</code> because it only returns strings for primitive data types like Object and String. In order to get the actual name of the class that an object is an instance of, you can use the <code>flash.utils.getQualifiedClassName()</code> global function.</p>
<h3>as</h3>
<p><code><a href="http://livedocs.adobe.com/flex/2/langref/operators.html#as">as</a></code> functions similarly to is except that it actually will attempt to coerce the object into the data type that you ask for. <a href="http://en.wikipedia.org/wiki/Type_conversion">Coercion</a>, AKA type conversion or casting, is the act of changing the data type of an object. </p>
<p>With the case from your book, an <code>Event</code> object has a <code>target</code> property which refers back to the object that originally dispatched the event. Since the <code>Event</code> object is designed to be sent out from all different types of objects, the target&#8217;s data type is <code>Object</code> (the mother of all the other classes). So, if the variable you&#8217;re defining is a TextField, you&#8217;re expecting an object with a <code>text</code> property. <code>Object</code>, doesn&#8217;t have a <code>text</code> property so you have a dilemma. The solution is to cast the object into the type you want it to be, basically forcing it to be more specific.</p>
<pre lang="actionscript3">
var textField:TextField = event.target; // Throws compile error because target is an Object not a TextField

var textField:TextField = event.target as TextField; // OK! as long as the event target actually is a text field
</pre>
<p>Going from a more generic type to a more specific type is called a <em>downcast</em>. Going from a more specific type to a more generic type is called an <em>upcast</em>. Upcasting in ActionScript, as with most other languages, is done automatically and is always safe. e.g. a Sprite can be used as a parameter for a function that requires a DisplayObject. Downcasting on the other hand is unsafe meaning that you can&#8217;t always guarantee that it will work. A DisplayObject is not necessarily a Sprite. </p>
<p>When an attempt to downcast with <code>as</code> fails, the value returned will be <code>null</code>:</p>
<pre lang="actionscript3">
var string:String = new String();
var textField:TextField = string as TextField // string can't be converted. textField will be set to null
</pre>
<p>If you&#8217;re not sure whether your object is the right type (and you should never assume it is) check to see if the value is null before proceeding:</p>
<pre lang="actionscript3">
var typedObject:TypedObject = mysteryObject as TypedObject;
if (typedObject != null) {
	// conversion was successful! do something with typedObject
} else {
	// conversion failed
}
</pre>
<h3>Type casts</h3>
<p>Another way to do this with slightly different results is by type casting. Type casting works with the syntax <code>Type(object)</code> and is functionally similar to <code>object as Type</code>. The only difference is with a type cast, you get a runtime error if the coercion fails.</p>
<p><code>var textField:TextField = TextField(event.target); // if event.target is a TextField, you're good. If not, you get an error.<br />
</code></p>
<p>you can also shortcut like this:</p>
<p><code>TextField(event.target).text = "foo";</code></p>
<h3>Conclusion</h3>
<p>Why are objects like <code>event.target</code> given such generic data types? The idea is that you want to minimize the requirements of a property, parameter, or function to maximize the number of objects that will qualify thus making your program more flexible. In other words, strive to use the most broadly defined data types that you can get away with. </p>
<p>For example, say you&#8217;re creating a simulator program for getting a person from their home to their job. You may be tempted to define your <code>modeOfTransportation</code> variable with a type of <code>Car</code>. But down the road the company you&#8217;re working for wants to create a more green image for themselves and suggests using alternative modes of transportation. If you&#8217;re code is based on Car, it&#8217;s limited in what you can do with it. However, if you had used a <code>Vehicle</code> type (includes Car, Bike, Motorcycle, Donkey) instead, you may not have had a problem. Using interfaces like <code>IConveyance</code> makes your program even more flexible because you can include non-vehicluar objects like MolecularTransporter, Feet, and CrowdSurfing as long as they satisfy the interface. In fact, I would suggest as an experiment that the next time you start a project, try coding all the interfaces first before you move on to coding specific classes. You&#8217;ll be surprised at how flexible it is and how it forces you to consider the roles of your classes from a higher level. </p>
<p>Though you should strive for generic types, sometimes you will need to get specific. If your program has a gas station simulator in it, the <code>fillTankOfVehicle()</code> method might require a <code>MotorVehicle</code> class (includes Car, Motorcycle, Plane) instead of simply <code>Vehicle</code>. Still, you should only get very specific when your program requires it. If you are making a racing program, use <code>F1RaceCar</code> only when a simple <code>Car</code> won&#8217;t cut it.</p>
<p>I hope this helps! This is definitely a difficult topic even for experienced programmers to grasp!</p>
]]></content:encoded>
			<wfw:commentRss>http://dispatchevent.org/mims/is-and-as/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Downsampling Flash &#8211; Converting AS3 to AS2</title>
		<link>http://dispatchevent.org/mims/converting-as3-to-as2/</link>
		<comments>http://dispatchevent.org/mims/converting-as3-to-as2/#comments</comments>
		<pubDate>Fri, 09 Jan 2009 01:03:32 +0000</pubDate>
		<dc:creator>Mims H Wright</dc:creator>
				<category><![CDATA[AS3]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tutorial]]></category>

		<guid isPermaLink="false">http://dispatchevent.org/?p=565</guid>
		<description><![CDATA[I recently came across a situation where I was forced to convert a working project in Flash 9 AS3 to Flash 8 AS2. I wanted to share some of my experiences to help other weary programmers in the same predicament. &#8230; <a href="http://dispatchevent.org/mims/converting-as3-to-as2/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I recently came across a situation where I was forced to convert a working project in Flash 9 AS3 to Flash 8 AS2. I wanted to share some of my experiences to help other weary programmers in the same predicament.</p>
<p><span id="more-565"></span></p>
<p>Before I begin, I must stress to you, <strong>if there is any way you can avoid downgrading your code, you should avoid it!</strong> If it&#8217;s a matter of making two systems communicate, there are countless reasons that it would be better to upgrade your code from AS2 to AS3. There are even ways to host your AS2 code in an AS3 wrapper. In my case, an important requirement (that the deliverable absolutely must be AS2) was left out of the original scope of work due to a misunderstanding but anything that doesn&#8217;t cause project stoppage should be considered carefully.</p>
<p>I must also preface this article by reminding the reader that there is no 1-to-1 conversion from AS3 to AS2 (nor the other way around). There are loads of features added to Flash Player 9 that simply don&#8217;t exist in Player 8. Some things you&#8217;ll be able to convert with a simple find and replace. Some things you&#8217;ll need to re-create in AS2 or re-think completely. Some things you just won&#8217;t be able to do. This article only covers the stuff I ran across while converting my files and is by no means comprehensive. However, I hope it gives you a good starting point to work from.</p>
<p><strong>Converting the document class </strong></p>
<p>Since you&#8217;re a smart, cool AS3 programmer with a good sense of humour, I know you use a document class in your FLA files or a main application class for your FlexBuilder project. Right? Good. Of course, AS2 does not support document classes and all code must be run from the timeline. You have a couple of options here. You could move your document class to the timeline and remove the class specific portions of the code. Perhaps a quicker and easier solution is to modify your document class to take a reference to the root. So you have something like this on the first frame&#8230;</p>
<pre>var documentClass:MyDocumentClass = new MyDocumentClass(this);</pre>
<p>Just keep in mind that your document class will need to store a reference to the root that&#8217;s passed in as well as make references to things that were on the stage. For example&#8230;</p>
<pre>public function MyDocumentClass(mc:MovieClip) {
   this._mc = mc;
   this.thingOnStage = _mc.thingOnStage;
}</pre>
<p><strong>Cleaning up syntax errors</strong></p>
<p>The first step to take once you can compile your code is to run it and fix each compile time error one at a time. To save you the trouble of some of the more common ones I found, I created this list of common syntax problems.</p>
<p><strong>Search and replace:</strong></p>
<ul>
<li>void -&gt; Void</li>
<li>int and uint -&gt; Number</li>
<li>DisplayObject, DisplayObjectContainer, Sprite -&gt; MovieClip</li>
<li>public class -&gt; class</li>
<li>internal -&gt; public</li>
<li>protected -&gt; private</li>
<li>override -&gt; (nothing)</li>
<li>mouseEnabled -&gt; enabled</li>
<li>[obj] as [type] -&gt; [type]([obj])</li>
<li>[obj] is [type] -&gt; [obj] instanceof [type]</li>
<li>const -&gt; var (or optionally, to implicit getter)</li>
<li>Add underscores before MovieClip variables. e.g.: x, scaleY, alpha -&gt; _x, _yscale, _alpha including:
<ul>
<li>alpha -&gt; _alpha</li>
<li>x,y -&gt; _x, _y</li>
<li>scaleX,Y -&gt; _xscale, _yscale</li>
<li>mouseX,Y -&gt; _xmouse, _ymouse</li>
<li>visible -&gt; _visible</li>
<li>currentFrame -&gt; _currentFrame</li>
<li>totalFrames -&gt; _totalframes</li>
</ul>
</li>
</ul>
<p><strong>Add:</strong></p>
<ul>
<li>import statements for classes within the same package (not required in AS3)</li>
</ul>
<p><strong>Remove:</strong></p>
<ul>
<li>import statements for AS3 packages (e.g. flash.display.* )</li>
</ul>
<p><strong>Convert:</strong></p>
<ul>
<li> _xscale, _yscale, _alpha &#8211; Convert from decimal to integer (0.2 becomes 20).</li>
<li> Convert Timer to setInterval</li>
<li> Convert Loader to MovieClipLoader</li>
<li> Remove package declaration but copy the package name and put it before the class name. Also remove closing } from the end of the file.<br />
e.g. :</p>
<pre>package com.mysite.package {
	public class Foo {
	 	//...
	}
}</pre>
<p>becomes</p>
<pre>class com.mysite.package.Foo {

	//...

}</pre>
</li>
</ul>
<p><strong>Other tips</strong></p>
<p>Events are a huge part of AS3 development and Player 9 supports a fully event-driven development style. Unfortunately, AS2 is heavily lacking in this department and while events can be recreated in AS2, it lacks method closures and bubbling that make AS3 so nice. To downgrade, I did two things. First, I recreated the event dispatcher in as2 and second, I made heavy use of event callbacks and the Delegate class.</p>
<p><strong>com.mimswright.events</strong></p>
<p>In order to change as little of the event code as possible, I looked back to previous AS2 projects where I had used <a href="http://dannypatterson.com">Danny Patterson</a>&#8216;s EventBroadcaster classes. These classes worked roughly the same as the EventDispatcher class in AS3. I took the liberty of brushing up the code and making it look as much like the AS3 EventDispatcher as I could. I&#8217;m making it freely available for use.</p>
<p><a href="http://dispatchevent.org/wp-content/uploads/2009/01/as2eventdispatcher.zip">as2eventdispatcher</a></p>
<p><strong>event callbacks</strong></p>
<p>Most of the events you&#8217;ll probably be using must be triggered by callbacks in AS2. That is, you define a function that gets called when an event takes place like</p>
<pre>onMouseMove = function ():Void {
   //do something
}</pre>
<p>Keep in mind that in AS2 there are no method closures as there are in AS3. That is, a function isn&#8217;t necessarily executed in the scope that it exists in. In even simpler terms, a function doesn&#8217;t know what object it&#8217;s associated with. Because of this, code such as <code>button.onRelease = object.handleClick;</code> executes the <code>handleClick</code> function from the <code>button</code> context and my not work properly. Fortunately, AS2 code can make use of the <code>mx.utils.Delegate</code> class &#8211; specifically, the <code>Delegate.create()</code> method. This artificially maintains the context in which the function was supposed to be called. So your code should look like <code>button.onRelease = mx.utils.Delegate.create(object, object.handleClick);</code></p>
<p>I hope this has been a helpful start for you poor souls who are faced with the challenge of down-converting code. Good luck to you and feel free to add your experiences to the comments.</p>
]]></content:encoded>
			<wfw:commentRss>http://dispatchevent.org/mims/converting-as3-to-as2/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
		<item>
		<title>AS3 Performance comparison tool</title>
		<link>http://dispatchevent.org/mims/as3-performance-comparison-tool/</link>
		<comments>http://dispatchevent.org/mims/as3-performance-comparison-tool/#comments</comments>
		<pubDate>Wed, 17 Dec 2008 18:42:55 +0000</pubDate>
		<dc:creator>Mims H Wright</dc:creator>
				<category><![CDATA[AS3]]></category>
		<category><![CDATA[Site-seeing]]></category>

		<guid isPermaLink="false">http://dispatchevent.org/?p=523</guid>
		<description><![CDATA[I ran across an interesting performance comparison tool at businessintelligence.me This could be very useful for code optimization and for a general understanding of what&#8217;s going on behind the scenes. Perhaps the most striking comparisons in this tool are the &#8230; <a href="http://dispatchevent.org/mims/as3-performance-comparison-tool/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I ran across <a href="http://businessintelligence.me/projects/performance_tester/performanceTester.html">an interesting performance comparison tool at businessintelligence.me</a></p>
<p>This could be very useful for code optimization and for a general understanding of what&#8217;s going on behind the scenes.</p>
<p><a href="http://businessintelligence.me/projects/performance_tester/performanceTester.html"><img src="http://dispatchevent.org/wp-content/uploads/2008/12/performacetool.png" alt="performace comparison tool" title="performace comparison tool" class="alignnone size-full wp-image-524" /></a></p>
<p>Perhaps the most striking comparisons in this tool are the ones that compare <a href="http://livedocs.adobe.com/flex/gumbo/langref/Vector.html">Vectors</a> to Arrays. Looping Vectors appears to be about 60x faster than looping through Arrays. Someone recently asked me when you would want to use Vectors and my response was whenever you possibly can. And if you think about it, the times which you need an untyped Array are (should be) almost never.</p>
]]></content:encoded>
			<wfw:commentRss>http://dispatchevent.org/mims/as3-performance-comparison-tool/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Tip: Adding version checking to your external code library</title>
		<link>http://dispatchevent.org/mims/version-checking/</link>
		<comments>http://dispatchevent.org/mims/version-checking/#comments</comments>
		<pubDate>Fri, 24 Oct 2008 18:14:33 +0000</pubDate>
		<dc:creator>Mims H Wright</dc:creator>
				<category><![CDATA[AS3]]></category>
		<category><![CDATA[Architecture]]></category>
		<category><![CDATA[Tips, Tricks, and Hacks]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[KitchenSync]]></category>
		<category><![CDATA[library]]></category>

		<guid isPermaLink="false">http://dispatchevent.org/?p=446</guid>
		<description><![CDATA[Since version 1.0 of a code library that I&#8217;m sure you&#8217;re tired of me talking about came out, I have been making steady updates, some of which break legacy code. I was also having trouble keeping track of which version &#8230; <a href="http://dispatchevent.org/mims/version-checking/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Since version 1.0 of <a href="http://kitchensynclib.googlecode.com" target="_blank">a code library that I&#8217;m sure you&#8217;re tired of me talking about</a> came out, I have been making steady updates, some of which break legacy code. I was also having trouble keeping track of which version of the library a particular demo was written for. In order to make sure that the new code library doesn&#8217;t cause unpredictable results for the old implementations I added a version check to the main class.</p>
<p>This version check is very simple. It checks a version number in the client code against a version number of the library (or external classes) when the main class of the library is initialized. It is also completely optional (so average users don&#8217;t need to mark up their code with version numbers).</p>
<p>Here&#8217;s an example:</p>
<p>Say you normally initialize your library using:</p>
<pre lang="actionscript3">MyLibrary.initialize();</pre>
<p>Which calls the initialize method:</p>
<pre lang="actionscript3">public static function initialize():void {
   // initialize library here
}</pre>
<p>If the external code changes unexpectedly (say through an SVN update) this could cause problems that are difficult to trace. The solution is to modify the initializer to match the client code version with the initializer version.</p>
<pre lang="actionscript3">public static const VERSION:String = "1.6";
public static function initialize(versionCheck:String = VERSION):void {
   if (versionCheck != VERSION) {
       throw new Error ("The version check failed! This library is version " + VERSION + ". Update your code or GTFO!");
   }
   // initialize library here
}</pre>
<p>And when you run the initializer, use the version number you&#8217;re expecting.</p>
<pre lang="actionscript3">MyLibrary.initialize("1.6");</pre>
<p>Some things to notice with this approach:</p>
<ul>
<li>I used a string instead of a number for the version number so that it would allow for sub-sub version numbers and other markers, e.g. &#8220;1.6.24 beta r545&#8243;</li>
<li>Because there is a default value for <code>versionCheck</code> in the initializer, providing a version number is optional. Using <code>MyLibrary.initialize()</code> will still work and throw no errors.</li>
<li>Unfortunately, there is not much you can do if the version numbers don&#8217;t match except to warn the client that the external code has changed. Still, I&#8217;ve found this to be very useful.</li>
</ul>
<p>The code for my initializer in its entirety after the jump&#8230;<br />
<span id="more-446"></span><br />
From <a href="http://code.google.com/p/kitchensynclib/source/browse/trunk/src/org/as3lib/kitchensync/KitchenSync.as">KitchenSync.as</a></p>
<pre lang="actionscript3">/**
* The current version of the library. Use this to verify that the library is the
* version that your software expects.
*/
public static const VERSION:String = "1.6";

/**
* Flag noting whether the engine has been initialized.
*/
private static var _initialized:Boolean = false;

/**
* Initializes the timing core for KitchenSync. Must be called before using any actions.
*
* @param frameRateSeed must be a DisplayObject that is added to the display list.
* @param versionCheck a string for the version you think you're using. e.g. 1.2 This is recommended
*                                         but not required. It will throw an error if you're using the wrong version of KS.
*/
public static function initialize(frameRateSeed:DisplayObject, versionCheck:String = VERSION):void
{       

	if (_initialized) {
		throw new IllegalOperationError("KitchenSync has already been initialized.");
	}
	if (versionCheck != VERSION) {
		throw new Error ("Version check failed. Please update to the correct version or to continue using this version (at your own risk) put the initialize() method inside a try{} block.");
	}

	// Initialization code omitted

	_initialized = true;
}</pre>
]]></content:encoded>
			<wfw:commentRss>http://dispatchevent.org/mims/version-checking/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
