<?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()™ &#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>Mon, 23 Apr 2012 19:07:06 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Snippet Saturday: quick random choice</title>
		<link>http://dispatchevent.org/mims/snippet-saturday-quick-random-choice/</link>
		<comments>http://dispatchevent.org/mims/snippet-saturday-quick-random-choice/#comments</comments>
		<pubDate>Sun, 20 Mar 2011 20:05:22 +0000</pubDate>
		<dc:creator>Mims H Wright</dc:creator>
				<category><![CDATA[AS3]]></category>
		<category><![CDATA[Snippets]]></category>
		<category><![CDATA[Tips, Tricks, and Hacks]]></category>
		<category><![CDATA[snippet]]></category>

		<guid isPermaLink="false">http://dispatchevent.org/?p=877</guid>
		<description><![CDATA[Today&#8217;s Snippet Saturday (actually, Sunday) is a quick shortcut for choosing one of several strings, objects, etc. randomly. Now, I wouldn&#8217;t really recommend using this code in a project. There are ways to do the same thing that are much more readable and less error prone. Instead, I thought it was an interesting experiment to [...]]]></description>
			<content:encoded><![CDATA[<p>Today&#8217;s Snippet Saturday (actually, Sunday) is a quick shortcut for choosing one of several strings, objects, etc. randomly. </p>
<p><script src="https://gist.github.com/878605.js?file=randomChoice.as"></script></p>
<p>Now, I wouldn&#8217;t really recommend using this code in a project. There are ways to do the same thing that are much more readable and less error prone. Instead, I thought it was an interesting experiment to show off some of how AS3&#8242;s syntax works for those of you who may not have seen something like this. </p>
<p>What&#8217;s happening here? Let&#8217;s break it down.</p>
<ul>
<li><code>["Alpha", "Bravo", "Charlie"]</code> &#8211; Here we are instantiating a new array and populating it with three strings. This is essentially the same as:
<pre>
var a:Array = new Array();
a[0] = "Alpha";
a[1] = "Bravo";
a[2] = "Charlie";
</pre>
</li>
<li><code>[...]</code> &#8211; next is another pair of square brackets. This is an <a href="http://www.adobe.com/livedocs/flash/9.0/ActionScriptLangRefV3/operators.html#array_access">array access operator</a>. In other words, everything between the two brackets will be evaluated as the index of the array to retrieve.</li>
<li><code>int(..</code>.) &#8211; This is an <a href="http://www.adobe.com/livedocs/flash/9.0/main/wwhelp/wwhimpl/common/html/wwhelp.htm?context=LiveDocs_Parts&#038;file=00000048.html">explicit type-cast to an int</a>. That means that everything inside those parentheses is evaluated and flash attempts to convert from whatever data type it is to an integer. In the case of decimal numbers, they are rounded down so this is similar to using <code>Math.floor()</code>.</li>
<li><code>Math.random() * 3</code> &#8211; random() of course produces a random floating point (decimal) number between 0.0 and (almost but not quite) 1.0. Multiplying that number by 3 (the length of the array) produces a number between 0.0 and 3.0 (technically, between 0 and 2.99999999etc).</li>
</ul>
<p>The result, an array is created with three strings, a random number between 0.0 and (almost) 3.0 is generated, it is rounded down to an int between 0 and 2, that number is used as the index of the array to look up. The result will be randomly one of the three strings. </p>
<p>I hope you found this interesting!</p>
]]></content:encoded>
			<wfw:commentRss>http://dispatchevent.org/mims/snippet-saturday-quick-random-choice/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Snippet Saturday: isRoughlyEqual()  PLUS: as3-utils</title>
		<link>http://dispatchevent.org/mims/snippet-saturday-isroughlyequal-plus-as3-utils/</link>
		<comments>http://dispatchevent.org/mims/snippet-saturday-isroughlyequal-plus-as3-utils/#comments</comments>
		<pubDate>Sat, 12 Mar 2011 14:11:19 +0000</pubDate>
		<dc:creator>Mims H Wright</dc:creator>
				<category><![CDATA[AS3]]></category>
		<category><![CDATA[Snippets]]></category>
		<category><![CDATA[fuzzy]]></category>
		<category><![CDATA[isRoughlyEqual]]></category>
		<category><![CDATA[math]]></category>

		<guid isPermaLink="false">http://dispatchevent.org/?p=879</guid>
		<description><![CDATA[Last week, I started a (hopefully) weekly post where I throw out a (hopefully) useful piece of code for people to (hopefully) use in their projects. I&#8217;m calling this #SnippetSaturday. John Lindquist commented almost immediately that there is a project on github designed to collect these sorts of useful little snippets of code. It&#8217;s called [...]]]></description>
			<content:encoded><![CDATA[<p>Last week, I started a (hopefully) weekly post where I throw out a (hopefully) useful piece of code for people to (hopefully) use in their projects. I&#8217;m calling this #SnippetSaturday. </p>
<p><a href="http://johnlindquist.com/">John Lindquist</a> commented almost immediately that there is a project on github designed to collect these sorts of useful little snippets of code. It&#8217;s called <a href="https://github.com/as3/as3-utils">as3-utils</a>. Check it out!</p>
<p>This week&#8217;s snippet, isRoughlyEqual(), provides a simple way to find out if two numbers are in the ballpark of each other.</p>
<p><script src="https://gist.github.com/857563.js?file=isRoughlyEqual.as"></script></p>
<p>I&#8217;ve added it to the as3-utils project too.<br />
<a href="http://github.com/mimshwright/as3-utils/blob/master/src/utils/number/isRoughlyEqual.as">http://github.com/mimshwright/as3-utils/blob/master/src/utils/number/isRoughlyEqual.as</a></p>
]]></content:encoded>
			<wfw:commentRss>http://dispatchevent.org/mims/snippet-saturday-isroughlyequal-plus-as3-utils/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Snippet Saturday: limit() function</title>
		<link>http://dispatchevent.org/mims/snippet-saturday-limit-function/</link>
		<comments>http://dispatchevent.org/mims/snippet-saturday-limit-function/#comments</comments>
		<pubDate>Sun, 06 Mar 2011 02:14:59 +0000</pubDate>
		<dc:creator>Mims H Wright</dc:creator>
				<category><![CDATA[AS3]]></category>
		<category><![CDATA[Snippets]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[gist]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[snippet-saturday]]></category>

		<guid isPermaLink="false">http://dispatchevent.org/?p=878</guid>
		<description><![CDATA[So I&#8217;m sitting on a bunch of pretty useful code and I&#8217;m not sure how to share it with people. I could create a massive library that combines all the miscellaneous bits into a single, poorly documented library, but after looking at the Flash Game Dojo&#8217;s wiki, I&#8217;m starting to believe more strongly in the [...]]]></description>
			<content:encoded><![CDATA[<p>So I&#8217;m sitting on a bunch of pretty useful code and I&#8217;m not sure how to share it with people. I could create a massive library that combines all the miscellaneous bits into a <a href="http://code.google.com/p/as3lib/">single, poorly documented library</a>, but after looking at the <a href="http://flashgamedojo.com/wiki/index.php?title=Category:Snippets">Flash Game Dojo&#8217;s wiki</a>, I&#8217;m starting to believe more strongly in the idea of releasing code in smaller pieces that achieve a particular function. So, I&#8217;m going to try to kick off this idea of <a href="http://twitter.com/#!/search/%23SnippetSaturday">#SnippetSaturday</a>. I&#8217;m sure I won&#8217;t manage to post something every week but I&#8217;ll do my best. If you&#8217;ve got a blog or just twitter, I&#8217;d encourage you to join in too!</p>
<p>Anyway, here&#8217;s my first contribution. It&#8217;s a simple little function that limits a number between an upper and lower limit.</p>
<p><script src="https://gist.github.com/856948.js?file=limit.as"></script></p>
<p>BTW, I used <a href="https://gist.github.com/">gist.github.com</a> for this but <a href="http://snipplr.com">snipplr</a> is good too. If you know of others, leave a comment!</p>
]]></content:encoded>
			<wfw:commentRss>http://dispatchevent.org/mims/snippet-saturday-limit-function/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Matryoshka Functions</title>
		<link>http://dispatchevent.org/mims/matryoshka-functions/</link>
		<comments>http://dispatchevent.org/mims/matryoshka-functions/#comments</comments>
		<pubDate>Fri, 07 Jan 2011 19:22:40 +0000</pubDate>
		<dc:creator>Mims H Wright</dc:creator>
				<category><![CDATA[AS3]]></category>
		<category><![CDATA[Objective-C]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tips, Tricks, and Hacks]]></category>

		<guid isPermaLink="false">http://dispatchevent.org/?p=869</guid>
		<description><![CDATA[The other day, I was working with the Flex function BindingUtil.bindProperty(). I always have a hard time remembering which pair of arguments comes first, the &#8216;site&#8217; or the &#8216;host&#8217; and to be honest, the names don&#8217;t really make much sense to me. &#8220;Oh, if only this were an Objective-C project I would have to laboriously [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://dispatchevent.org/wp-content/uploads/2011/01/First-matryoshka-museum-doll-open.jpeg"><img src="http://dispatchevent.org/wp-content/uploads/2011/01/First-matryoshka-museum-doll-open-300x211.jpg" alt="" width="300" height="211" class="alignnone size-medium wp-image-870" /></a><br />
The other day, I was working with the Flex function BindingUtil.bindProperty(). I always have a hard time remembering which pair of arguments comes first, the &#8216;site&#8217; or the &#8216;host&#8217; and to be honest, the names don&#8217;t really make much sense to me. &#8220;Oh, if only this were an Objective-C project I would have to laboriously type out each name of each parameter.&#8221; I thought, &#8220;It would be so much more obvious if it were writen like <code>Bind.property("foo").fromObject(bar).toTheValueOfProperty("foo").ofObject(baz);</code>.&#8221; </p>
<p>What followed was the discovery/invention of just such a thing, a nested, super-verbose function. The function achieves this by returning an instance of an internal class with nothing in it except for the next step in the chain.</p>
<pre lang="actionscript3">
package
{
	public class Bind
	{
		public static function updateProperty(object:Object, property:String):BindSource {
			return new BindSource(object, property);
		}
	}
}
import mx.binding.utils.BindingUtils;
class BindSource {

	private var targetObject:Object;
	private var targetProperty:String;

	public function BindSource(targetObject:Object, targetProperty:String):void {
		this.targetObject = targetObject;
		this.targetProperty = targetProperty;
	} 

	public function whenSourcePropertyChanges(object:Object, property:String):void {
		BindingUtils.bindProperty(targetObject, targetProperty, object, property);
	}
}
</pre>
<p>To call this function you would use:</p>
<pre lang="actionscript3">Bind.updateProperty(foo, "bar").whenSourcePropertyChanges(baz, "bar");</pre>
<p>Kinda cool! I soon realized that this technique has some characteristics that Objective-C messages don&#8217;t have. For example, each step can use more than one parameter (plus they each get their own code hinting). You can also provide multiple choices for what to do next providing a sort of branching behavior.</p>
<pre lang="actionscript3">
AngleFinder.getAngleOfVector(4, 4).inDegrees() 	// 45Â°
AngleFinder.getAngleOfVector(4, 4).inRadians() 	// Ï€/4
</pre>
<p>You can even do free-form stacking of functions like this.</p>
<pre lang="actionscript3">
new Calculator	(5)                    // 5
                        .plus(3)            // 8
                        .times(2)          // 16
                        .dividedBy(4)   // 4
                        .minus(1)         // 3
                        .plus(2)            // 5
                        .equals();
</pre>
<p>I proudly named these &#8220;Matryoshka Functions&#8221; after the Russian stacking dolls even though I&#8217;m sure I&#8217;m not the first to try something like this. As it turns out, they&#8217;re about 10% practical and 90% clever hack. Even though there could be some potential use for this (especially with complicated, repetitive tasks), this technique conflicts too much with the way we normally code in ActionScript. Still, I thought it was cool enough to share with everyone.</p>
<p>Pros:</p>
<ul>
<li>Very descriptive</li>
<li>Branching and stacking effect is interesting and potentially useful</li>
<li>Code hinting shows the next step in the function as well as the parameter names</li>
<li>Looks cool!</li>
</ul>
<p>Cons:</p>
<ul>
<li>Abominable code style. Too weird to be useful.</li>
<li>Difficult to know when to stop calling functions</li>
<li>Not completing the function will produce unexpected results with no compile-time errors</li>
<li>Each step requires you to pass the parameters from the previous step</li>
</ul>
<p>Can you find any use of these? Can you think of a more efficient way to write the functions? Any other thoughts?</p>
<p>Check out the source with multiple examples (FlashBuilder project).<br />
<a href='http://dispatchevent.org/wp-content/uploads/2011/01/MatroshkaFunctionDemo.zip'>MatroshkaFunctionDemo</a></p>
]]></content:encoded>
			<wfw:commentRss>http://dispatchevent.org/mims/matryoshka-functions/feed/</wfw:commentRss>
		<slash:comments>3</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 where objects landed on the stage, but after landing, they appeared really muddy, which was [...]]]></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>AS3 Bible translated to Chinese</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[IRL]]></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 read chinese and you&#8217;d like a copy, let me know and I&#8217;ll send you 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[Architecture]]></category>
		<category><![CDATA[AS3]]></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 in my projects and I find that by keeping things flexible, it saves more time [...]]]></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>7</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>1</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 and more streamlined. The entire system for greater simplicity and practical functionality. This version will [...]]]></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 way, down the line it&#8217;s a very easy change when you want to localize the [...]]]></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>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 mine recently and I&#8217;ve decided that a concise explanation is something I should write. This [...]]]></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 course, simply tracing a number, even if it&#8217;s in the format 0xFF, will result in [...]]]></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[Architecture]]></category>
		<category><![CDATA[AS3]]></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 useful, more of a teaching thing really. However there&#8217;s a bit of code that says: [...]]]></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. Before I begin, I must stress to you, if there is any way you can [...]]]></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 ones that compare Vectors to Arrays. Looping Vectors appears to be about 60x faster than [...]]]></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>5</slash:comments>
		</item>
	</channel>
</rss>

