<?xml version="1.0" encoding="UTF-8"?>
<!-- generator="wordpress/2.3.3" -->
<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/"
	>

<channel>
	<title>{ P I X E L W E L D E R S } &#187; Best Practices</title>
	<link>http://pixelwelders.com/blog</link>
	<description>Flash + Flex + Game Dev + Grammar?</description>
	<pubDate>Thu, 13 Jan 2011 13:26:17 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.3.3</generator>
	<language>en</language>
			<item>
		<title>Flash/Flex Integration: SWFLibrary v1.5</title>
		<link>http://pixelwelders.com/blog/best-practices/2008/flashflex-integration-swflibrary-v15/</link>
		<comments>http://pixelwelders.com/blog/best-practices/2008/flashflex-integration-swflibrary-v15/#comments</comments>
		<pubDate>Wed, 20 Aug 2008 01:28:48 +0000</pubDate>
		<dc:creator>Zack Jordan</dc:creator>
		
		<category><![CDATA[ActionScript 3.0]]></category>

		<category><![CDATA[Best Practices]]></category>

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

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

		<category><![CDATA[Flash/Flex Integration]]></category>

		<guid isPermaLink="false">http://pixelwelders.com/blog/best-practices/2008/flashflex-integration-swflibrary-v15/</guid>
		<description><![CDATA[
Usually I like to post glamorous stuff, like 3D swirly things and experiments.  Occasionally, however, I feel the need to release a little code in the interest of helping a few people out.  This code is a review of sorts, since I already posted a similar utility back in June.  However, since [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://pixelwelders.com/blog/wp-content/themes/tma/images/latest/SWFLibrary_470x175.jpg"/ style="float:none"></p>
<p>Usually I like to post glamorous stuff, like 3D swirly things and experiments.  Occasionally, however, I feel the need to release a little code in the interest of helping a few people out.  This code is a review of sorts, since I already posted a <a href="http://pixelwelders.com/blog/best-practices/2008/runtime-libraries-in-as3/" >similar utility</a> back in June.  However, since then I have been expanding my code library and I thought I&#8217;d share the latest and much improved version.  It&#8217;s really a simple concept, but having this in my toolbox has lightened my workload as an ActionScripter considerably.</p>
<h3>Advantages</h3>
<p>This utility allows you to publish any symbol from Flash and access it at runtime from an ActionScript-only project, such as in Flex Builder.  This has a few advantages:</p>
<ul>
<li>Easy storage of many assets in one file</li>
<li>Ability to change graphics without recompiling the main application</li>
<li>Faster load times with only one HTTP request (as opposed to many separate images and sounds)</i>
<li>Ability to import vector graphics and animations at runtime</li>
<li>Smaller main SWF size</li>
</ul>
<p>The one disadvantage that I can think of is that you&#8217;ll need to recompile your library SWF every time you make a change.  However, I think the advantages above outweigh this.</p>
<h3>Differences in v1.5</h3>
<p>Here are the main differences from the last version:</p>
<ul>
<li>The class is no longer static.  This allows for more than one instance of SWFLibrary at a time.</li>
<li>The class can now return instances of Sprites, MovieClips, and Sounds from a library SWF.</li>
</ul>
<h3>Usage</h3>
<p>1 - In Flash, select &#8220;Export for ActionScript&#8221; in the symbol&#8217;s Linkage settings in the Library (ex. &#8216;Character&#8217;).<br />
2 - Publish the Flash file as a SWF.<br />
3 - Load the SWF into an instance of SWFLibrary<br />
4 - Access the symbol through SWFLibrary, using the class name chosen in Step 1.</p>
<p>A SWFLibrary can be used with four lines of code.  Here&#8217;s what you&#8217;ll need:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript"><span style="color: #808080; font-style: italic;">// creates an instance of SWFLibrary, adds a listener, and loads a SWF</span>
<span style="color: #000000; font-weight: bold;">var</span> gameAssets:SWFLibrary = <span style="color: #000000; font-weight: bold;">new</span> SWFLibrary
gameAssets.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span> Event.<span style="color: #006600;">COMPLETE</span>, handleAssetsLoaded <span style="color: #66cc66;">&#41;</span>;
gameAssets.<span style="color: #0066CC;">load</span><span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">&quot;myCustomGameAssets.swf&quot;</span> <span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">// accesses the asset (assumes there is a symbol exported as 'Character' in the loaded SWF, as seen in step 1 above)</span>
<span style="color: #808080; font-style: italic;">// this code should run after the Event above has fired</span>
<span style="color: #000000; font-weight: bold;">var</span> mySprite:Sprite = gameAssets.<span style="color: #006600;">getSprite</span><span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">&quot;Character&quot;</span> <span style="color: #66cc66;">&#41;</span>;
addChild<span style="color: #66cc66;">&#40;</span> mySprite <span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">// gets the asset as a MovieClip (assuming, of course, that it actually is a movieclip)</span>
<span style="color: #000000; font-weight: bold;">var</span> myMC:<span style="color: #0066CC;">MovieClip</span> = gameAssets.<span style="color: #006600;">getSprite</span><span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">&quot;Character&quot;</span> <span style="color: #66cc66;">&#41;</span>;
addChild<span style="color: #66cc66;">&#40;</span> myMC <span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">// getting a sound</span>
<span style="color: #000000; font-weight: bold;">var</span> mySound:<span style="color: #0066CC;">Sound</span> = gameAssets.<span style="color: #006600;">getSound</span><span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">&quot;SoundLinkageNameHere&quot;</span> <span style="color: #66cc66;">&#41;</span>;
mySound.<span style="color: #0066CC;">play</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #808080; font-style: italic;">// === ===</span></pre></div></div>

<p>After a symbol has been retrieved from SWFLibrary, it may be treated like any other object.</p>
<h3>Download</h3>
<p><a href="http://pixelwelders.com/open_source/SWFLibrary/SWFLibrary.zip" onclick="javascript:pageTracker._trackPageview('/downloads/open_source/SWFLibrary/SWFLibrary.zip');">DOWNLOAD SOURCE</a></p>
<p>So have fun with this.  Hopefully it saves you as much time as it saves me.</p>
]]></content:encoded>
			<wfw:commentRss>http://pixelwelders.com/blog/best-practices/2008/flashflex-integration-swflibrary-v15/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Presenting: Tutorio.us!</title>
		<link>http://pixelwelders.com/blog/best-practices/2008/presenting-tutorious/</link>
		<comments>http://pixelwelders.com/blog/best-practices/2008/presenting-tutorious/#comments</comments>
		<pubDate>Tue, 22 Jul 2008 18:20:18 +0000</pubDate>
		<dc:creator>Zack Jordan</dc:creator>
		
		<category><![CDATA[Best Practices]]></category>

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

		<guid isPermaLink="false">http://pixelwelders.com/blog/best-practices/2008/presenting-tutorious/</guid>
		<description><![CDATA[After a couple of months working on this project, I am ready to release my labor of love.  Presenting: Tutorio.us!]]></description>
			<content:encoded><![CDATA[<p>After a couple of months working on this project, I am ready to release my labor of love.  Presenting: Tutorio.us!</p>
<p><a href="http://tutorio.us"><br />
<img style="float:none;" src='http://pixelwelders.com/blog/wp-content/uploads/2008/07/logo2_500.jpg' alt='Tutorious Logo' /><br />
</a></p>
<p>To explain what this is, and instead of writing something new, I&#8217;ll just quote directly directly from the website:</p>
<div style="background:#eee; padding:15px;">
<h3>The Scenario</h3>
<p>So you want to learn Flash. Great! A noble endeavor. And you want to do it on your own, harnessing the vast resources of the internet. Also a good idea, with only one problem: which resources?</p>
<p>The Internet is a vast place. According to <a href="http://adamant.typepad.com/seitz/2007/04/weighing_the_we.html" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://adamant.typepad.com/seitz/2007/04/weighing_the_we.html');">some estimates</a>, it contains nearly two ounces of information (which is more than you think when you’re counting electrons). If that doesn’t awe you, consider <a href="http://blog.managednetworks.co.uk/it-support/googles-20-petabytes/" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://blog.managednetworks.co.uk/it-support/googles-20-petabytes/');">this article</a>, which tells us that if a byte is a grain of rice, Google processes enough rice daily for every person on earth to have sixteen bowls full. <em>Daily</em>.</p>
<h3>The Problem</h3>
<p>The main point is this: there is a lot of information out there. There is more than enough to teach you everything you want to know about Flash. If you’ve spent any time searching, you’ve probably seen hundreds of tutorials already- maybe dozens today alone. However, the problem is this: a lot of them kinda suck. You’ve probably noticed this too. Incorrect information, bad (even crippling) practices, ancient Flash versions… the list goes on.</p>
<p>The second problem is this: If you’re new to ActionScript, what do you learn first? XML? Text formatting? Class structure? And where do you go after that? What are the pre-requisites of each tutorial? All this knowledge is out there (somewhere in that two ounces of electrons) but the majority of it is alone, without context, floating in the void.</p>
<h3>The Solution</h3>
<p>So here’s our solution. Tutorio.us is our attempt to index the best Flash instruction on the Internet, in a context that makes sense. We are dedicated to finding the clearest, best-written content for the most cutting-edge technology and putting it together in ways that everyone can understand.
</p></div>
<p><br/><br />
So there you have it.  Go take a look and let me know what you think!</p>
]]></content:encoded>
			<wfw:commentRss>http://pixelwelders.com/blog/best-practices/2008/presenting-tutorious/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Speed Tests: Objects vs. Arrays vs. Dictionaries</title>
		<link>http://pixelwelders.com/blog/best-practices/2008/speed-tests-objects-vs-arrays-vs-dictionaries/</link>
		<comments>http://pixelwelders.com/blog/best-practices/2008/speed-tests-objects-vs-arrays-vs-dictionaries/#comments</comments>
		<pubDate>Tue, 08 Jul 2008 09:27:12 +0000</pubDate>
		<dc:creator>Zack Jordan</dc:creator>
		
		<category><![CDATA[ActionScript 3.0]]></category>

		<category><![CDATA[Best Practices]]></category>

		<guid isPermaLink="false">http://pixelwelders.com/blog/best-practices/2008/speed-tests-objects-vs-arrays-vs-dictionaries/</guid>
		<description><![CDATA[	I’ve been taking buses around Europe for a couple weeks now, and sometimes those trips get extremely long.  Yesterday I took a bus from Venice to Seefeld (Austria), a trip that would have taken about five hours if not for the hurricane-like downpour they were having in Italy.  Seriously, it was ridiculous- all traffic was on the side of the highway, and all the motorcyclists were huddled under the overpasses.  But on the bright side, it did give me a lot of time to try out some things that I normally wouldn’t take the time to do, such as this experiment.]]></description>
			<content:encoded><![CDATA[<p>	I’ve been taking buses around Europe for a couple weeks now, and sometimes those trips get extremely long.  Yesterday I took a bus from Venice to Seefeld (Austria), a trip that would have taken about five hours if not for the hurricane-like downpour they were having in Italy.  Seriously, it was ridiculous- all traffic was on the side of the highway, and all the motorcyclists were huddled under the overpasses.  But on the bright side, it did give me a lot of time to try out some things that I normally wouldn’t take the time to do, such as this experiment.</p>
<p>	I make a lot of decisions daily based on things I think I know, but really have just always assumed.  And so, on this bus ride, I decided to get to the bottom of a few of them.  The first experiment I tried was with the read and write speeds of Objects, Arrays, and Dictionaries.  In the past, I have always used Arrays when I need all the nifty sorting and organization methods, Dictionaries when I need fast access, object keys, or weak references, and Objects pretty much never (because they’re messy and slow).  So yesterday I decided to see if my assumptions were actually valid, or if I had been doing things wrong this whole time.</p>
<p>	So I set up a quick class to test.  I created a Dictionary, an Object, and an Array, and then I put 100,000 integers in each one for the write test, and read them back out for the read test.  This is what I got (all times are average and in milliseconds).</p>
<table>
<tr>
<th/>
<th>Object</th>
<th>Array</th>
<th>Dictionary</th>
</tr>
<tr>
<th>Read (integer key)</th
<td>868</td>
<td>19</td>
<td>573</td>
</tr>
<tr>
<th>Write (integer key)</th>
<td>24</td>
<td>17</td>
<td>22</td>
</tr>
<tr>
<th>Read (string key)</th>
<td>586</td>
<td>N/A</td>
<td>573</td>
</tr>
<tr>
<th>Write (string key)</th>
<td>329</td>
<td>N/A</td>
<td>333</td>
</tr>
</table>
<p>	Obviously there are many more benchmarks that I could have run, but I also had a pretty good book on that bus.  I think I’ve done enough experimentation to prove that, once again, my assumptions are invalid.</p>
]]></content:encoded>
			<wfw:commentRss>http://pixelwelders.com/blog/best-practices/2008/speed-tests-objects-vs-arrays-vs-dictionaries/feed/</wfw:commentRss>
		</item>
		<item>
		<title>AS3 Broadcaster Class</title>
		<link>http://pixelwelders.com/blog/best-practices/2008/as3-broadcaster-class/</link>
		<comments>http://pixelwelders.com/blog/best-practices/2008/as3-broadcaster-class/#comments</comments>
		<pubDate>Mon, 23 Jun 2008 14:52:31 +0000</pubDate>
		<dc:creator>Zack Jordan</dc:creator>
		
		<category><![CDATA[ActionScript 3.0]]></category>

		<category><![CDATA[Best Practices]]></category>

		<category><![CDATA[AS2 vs AS3]]></category>

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

		<guid isPermaLink="false">http://pixelwelders.com/blog/best-practices/2008/as3-broadcaster-class/</guid>
		<description><![CDATA[This is something simple enough that I hesitate to post it, but maybe someone else can benefit.  Plus, I always try to have something new on Mondays.  Anyway, this a technique I once used back in the dark ages of ActionScript development (meaning AS2).  Last week, however, I suddenly had need of it again.]]></description>
			<content:encoded><![CDATA[<p><img src="http://pixelwelders.com/blog/wp-content/themes/tma/images/latest/satellite_470x175.jpg" style="float:none"/></p>
<p>This is something simple enough that I hesitate to post it, but maybe someone else can benefit.  Plus, I always try to have something new on Mondays.  Anyway, this a technique I once used back in the dark ages of ActionScript development (meaning AS2).  Last week, however, I suddenly had need of it again.</p>
<h3>The Scenario</h3>
<p>The AS3 event model is a pretty slick deal- much better than the six different ways we used to do it back in AS2.  However, I ran into a spot last week in which it failed me.  I had several different components in completely different parts of an application, and I wanted them all to listen to each other.  That in itself is no problem.  The problem was that I also wanted them to be completely <em>unaware</em> of each other, in hopes of keeping the code a bit more encapsulated.  So how does one listen for an event that comes from a source whose very existence is unknown?</p>
<h3>Option #1</h3>
<p>Option #1 was to make all my events bubble, and have each component add event listeners to some display list parent that all the components shared.  This would ensure that everyone got their events, but it makes everyone dependent on the current setup.  What if I wanted to move a component elsewhere and now it didn&#8217;t share that parent?  Plus, it&#8217;s not a solution that is guaranteed to work in a different app.  <em>Plus</em>, it involves children referencing their parent, which I am against on a fundamental, perhaps even a <em>spiritual</em> level.  So, scratch Option #1.</p>
<h3>Option #2</h3>
<p>Then I thought maybe each component should store a reference to all the other components.  But once again, you can throw the whole “loosely-coupled” idea out the window.  Something about this solution just didn’t feel right.</p>
<h3>Option #3</h3>
<p>Option #3 was the old AS2 method of creating a central static Broadcaster class and telling everyone to listen to <em>it</em>.  Now the only dependency each component had was to one static communication class.  Everything else could change as long as they were listening to the Broadcaster.  And so, as my great-grandmother has told me, sometimes the old ways are best.</p>
<h3>Source</h3>
<p>There’s really not much to explain with this class.  The only challenge was making a static class into an EventDispatcher, but that turned out to be pretty easy (thanks to <a href="http://gskinner.com/" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://gskinner.com/');">G. Skinner</a>, who originally gave me the idea for a static EventDispatcher).  Behold:</p>
<pre lang=”Actionscript”>
/**
 * A central event broadcaster
 *
 * @author	Zack Jordan
 * 			{ P I X E L W E L D E R S . C O M }
 */
package com.pixelwelders.events
{
	import flash.events.Event;
	import flash.events.EventDispatcher;

	public class Broadcaster
	{

		private static var eventDispatcher			: EventDispatcher;

		/**
		 * Broadcasts an event to all listeners
		 * To listen to any and all events, use Broadcaster.addEventListener()
		 *
		 * @param	event		The Event to broadcast
		 * @return				nothing
		 */
		public static function broadcast( event:Event ): void
		{
			dispatchEvent( event );
		}

		// === S T A T I C   E V E N T   D I S P A T C H E R ===
		public static function addEventListener( type:String, listener:Function, useCapture:Boolean=false,
			priority:int=0, useWeakReference:Boolean=true ):void
		{
			if ( !eventDispatcher ) eventDispatcher = new EventDispatcher();
			eventDispatcher.addEventListener( type, listener, useCapture, priority, useWeakReference );
		}

		public static function removeEventListener( type:String, listener:Function, useCapture:Boolean=false ):void
		{
			if ( eventDispatcher ) eventDispatcher.removeEventListener( type, listener, useCapture );
  	    	}

		public static function dispatchEvent( p_event:Event ):void
		{
			if ( eventDispatcher ) eventDispatcher.dispatchEvent( p_event );
		}

	}
}
</pre>
<p>As you can see, this is just a static wrapper for an instance of EventDispatcher.  I just route all the methods of the EventDispatcher to static methods, and voilá.  As always, if you can think of a better way, just drop it in the comments.</p>
]]></content:encoded>
			<wfw:commentRss>http://pixelwelders.com/blog/best-practices/2008/as3-broadcaster-class/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Runtime Libraries in Flex ActionScript Projects</title>
		<link>http://pixelwelders.com/blog/best-practices/2008/runtime-libraries-in-as3/</link>
		<comments>http://pixelwelders.com/blog/best-practices/2008/runtime-libraries-in-as3/#comments</comments>
		<pubDate>Mon, 16 Jun 2008 09:15:33 +0000</pubDate>
		<dc:creator>Zack Jordan</dc:creator>
		
		<category><![CDATA[ActionScript 3.0]]></category>

		<category><![CDATA[Best Practices]]></category>

		<guid isPermaLink="false">http://pixelwelders.com/blog/best-practices/2008/runtime-libraries-in-as3/</guid>
		<description><![CDATA[When working with Flash, there is really only one vector format worth worrying about, and that is SWF.  And furthermore, it turns out that there is a kinda slick way to import all the assets contained in a SWF into a Flex ActionScript project.  In the following class, I've just smoothed the process a little bit.]]></description>
			<content:encoded><![CDATA[<p><img src="http://pixelwelders.com/blog/wp-content/themes/tma/images/latest/library_470x175.jpg" alt="Runtime library" style="float:none"/></p>
<p>This is something I&#8217;ve begun using recently, and I thought I would post about it to see if I can help anyone else who&#8217;s been thinking about this.  I&#8217;ve seen some other articles on this, but they&#8217;ve had some shortcomings that I think I have improved on.  Of course, there&#8217;s room for tons of improvement on my class as well, so feel free to extend this to your little collective hearts&#8217; content.  The source is downloadable at the bottom of the page, or you can continue to the second page of this article to view it all without downloading.  Anyway: onward!</p>
<h3>The Need</h3>
<p>Okay, so: as a Flex developer who does a lot of ActionScript-only projects, I often run into situations in which I need to import some artwork.  This artwork may be in any number of formats, and they all require different solutions.  However, for a project that I&#8217;m working on now (which I would expound upon if it wasn&#8217;t [apparently] top-secret), I needed to import hundreds of little images in a vector format.  Furthermore, the client hinted that it would be nice, if not mandatory, to be able to swap out images later without sending it back to me for a recompile.  So, I had to do a bit of thinking.</p>
<p>However, it turns out that the solution is really not that hard.  When working with Flash, there is really only one vector format worth worrying about, and that is SWF.  And furthermore, it turns out that there is a kinda slick way to import all the assets contained in a SWF into a Flex ActionScript project.  In the following class, I&#8217;ve just smoothed the process a little bit.</p>
<h3>The Theory</h3>
<p>1- Create a MovieClip in Flash CS3 and check Export for ActionScript in its linkage properties.  Remember the class name that you give it, because you&#8217;re going to need that to pull this asset back out of its mother SWF.</p>
<p>2- Publish your SWF.  Make a note of where it&#8217;s going so you can load it in Flex.</p>
<p>3- In Flex, set up a Loader with the typical listeners (see my source below for details) and load the SWF.</p>
<p>4- Once the Loader has successfully loaded the SWF, the part you are interested in is the Loader&#8217;s contentLoaderInfo property (which is an instance of the LoaderInfo class, if you&#8217;d like to check the Flash help files for more information).  This property now contains the data from your Flash CS3 SWF.  Interestingly enough, a LoaderInfo class contains an instance of the ApplicationDomain class, which in turns contains all class definitions that are contained in that SWF.  Since you checked Export for ActionScript on your MovieClip, that means there is a definition for it in the ApplicationDomain of this LoaderInfo.  You just need to pull it out and instantiate it.</p>
<p>5- You have to do a little dancing around with types to get your MovieClip back out, since you will be pulling it out as an instance of Class, not as an instance of MovieClip.  However, with a couple lines of code, it&#8217;s easy enough to actually create an instance of your original Flash CS3-authored MovieClip.</p>
<h3>The Code</h3>
<p>Here are the vital parts of the code, as seen below in the source download link at the bottom of the page.  These are contained in a wrapper class that I wrote that surrounds Loader.contentLoaderInfo.</p>
<p>This is the handler that is called when the Loader successfully loads the SWF.  Notice that we save the LoaderInfo (called &#8220;contentLoaderInfo&#8221;) for later use.  This is the package of all the assets we have just imported.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript"><span style="color: #0066CC;">private</span> <span style="color: #0066CC;">static</span> <span style="color: #000000; font-weight: bold;">function</span> handleLoadComplete<span style="color: #66cc66;">&#40;</span> event:Event <span style="color: #66cc66;">&#41;</span>: <span style="color: #0066CC;">void</span>
<span style="color: #66cc66;">&#123;</span>
	loaderInfo = loader.<span style="color: #006600;">contentLoaderInfo</span>;
	dispatchEvent<span style="color: #66cc66;">&#40;</span> <span style="color: #000000; font-weight: bold;">new</span> Event<span style="color: #66cc66;">&#40;</span> Event.<span style="color: #006600;">COMPLETE</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>And here is where the real magic happens, and it must be called after the above handler.  Notice how we get a real live Sprite instance from the definition in our LoaderInfo instance.  Plus, it looks just like the asset you created in Flash at the beginning of this article.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript"><span style="color: #0066CC;">public</span> <span style="color: #0066CC;">static</span> <span style="color: #000000; font-weight: bold;">function</span> getAsset<span style="color: #66cc66;">&#40;</span> id:<span style="color: #0066CC;">String</span> <span style="color: #66cc66;">&#41;</span>: Sprite
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">var</span> Asset:<span style="color: #000000; font-weight: bold;">Class</span> = loaderInfo.<span style="color: #006600;">applicationDomain</span>.<span style="color: #006600;">getDefinition</span><span style="color: #66cc66;">&#40;</span> id <span style="color: #66cc66;">&#41;</span> as <span style="color: #000000; font-weight: bold;">Class</span>;			
	<span style="color: #b1b100;">return</span> Sprite<span style="color: #66cc66;">&#40;</span> <span style="color: #000000; font-weight: bold;">new</span> Asset<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<h3>Summary</h3>
<p>This is as basic as it gets.  My source below extends this enough for me to use it in the aforementioned project (as well as adding some basic error handling), but this could be extended for all types of applications.  For instance, my class only returns instances of Sprites because that is what I am interested in.  However, you could also return the class itself (un-instantiated), or anything that you might find in LoaderInfo.applicationDomain.  The possibilities are&#8230; well, not limitless.  But more than you might think.</p>
<h3>Source</h3>
<p>The zip contains a RuntimeLibrary class (shown below), an example using it, and some sample assets in a SWF (with the matching FLA so you can see that they are nothing special- just Exported for ActionScript).  As you can see, I&#8217;m doing some basic error handling, but you may want to do something a little more robust; or, you may want the error.  Feel free to change whatever you want- that&#8217;s the beauty of open-source!</p>
<p><a href="http://pixelwelders.com/downloads/RuntimeLibrary/Pixelwelders_RuntimeLibrary.zip" onclick="javascript:pageTracker._trackPageview('/downloads/downloads/RuntimeLibrary/Pixelwelders_RuntimeLibrary.zip');">DOWNLOAD SOURCE</a></p>

<div class="wp_syntax"><div class="code"><pre class="actionscript"><span style="color: #808080; font-style: italic;">/**
 * A utility to load a SWF at runtime.
 * This makes all definitions in the loaded SWF available.
 * You can use this to load graphics elements created in Flash CS3 into Flex ActionScript projects
 * 
 * @author	Zack Jordan
 * 			{ P I X E L W E L D E R S }
 * 			pixelwelders.com/blog
 */</span>
package com.<span style="color: #006600;">pixelwelders</span>.<span style="color: #006600;">graphics</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Loader</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">LoaderInfo</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">ErrorEvent</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">Event</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">EventDispatcher</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">IOErrorEvent</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">net</span>.<span style="color: #006600;">URLRequest</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> RuntimeLibrary <span style="color: #0066CC;">extends</span> EventDispatcher
	<span style="color: #66cc66;">&#123;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> request				: URLRequest;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> loader				: Loader;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> loaderInfo			: LoaderInfo;
&nbsp;
		<span style="color: #808080; font-style: italic;">/**
		 * Constructor- creates a new RuntimeLibrary
		 * 
		 * @param			nothing
		 * @return			The newly-created RuntimeLibrary 
		 */</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> RuntimeLibrary<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			request = <span style="color: #000000; font-weight: bold;">new</span> URLRequest<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			loader = <span style="color: #000000; font-weight: bold;">new</span> Loader<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			loader.<span style="color: #006600;">contentLoaderInfo</span>.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span> Event.<span style="color: #006600;">INIT</span>, handleLoadComplete <span style="color: #66cc66;">&#41;</span>;
			loader.<span style="color: #006600;">contentLoaderInfo</span>.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span> IOErrorEvent.<span style="color: #006600;">IO_ERROR</span>, handleIOError <span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">// === A P I ===</span>
		<span style="color: #808080; font-style: italic;">/**
		 * Loads a swf into this RuntimeLibrary
		 * 
		 * @param	url		the URL of the SWF to load
		 * @return			nothing
		 */</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">load</span><span style="color: #66cc66;">&#40;</span> <span style="color: #0066CC;">url</span>:<span style="color: #0066CC;">String</span> <span style="color: #66cc66;">&#41;</span>: <span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			request.<span style="color: #0066CC;">url</span> = <span style="color: #0066CC;">url</span>;
			loader.<span style="color: #0066CC;">load</span><span style="color: #66cc66;">&#40;</span> request <span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">/**
		 * Creates and returns an instance of the specified class from the loaded SWF
		 * If the specified class does not exist, dispatches an ErrorEvent and returns an empty Sprite
		 * 
		 * @param	id		Name of the class in the loaded SWF
		 * @return			An instance of the specified class, or an empty Sprite if the class is not found
		 */</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getAsset<span style="color: #66cc66;">&#40;</span> id:<span style="color: #0066CC;">String</span> <span style="color: #66cc66;">&#41;</span>: Sprite
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">try</span>
			<span style="color: #66cc66;">&#123;</span>
				<span style="color: #000000; font-weight: bold;">var</span> Asset:<span style="color: #000000; font-weight: bold;">Class</span> = loaderInfo.<span style="color: #006600;">applicationDomain</span>.<span style="color: #006600;">getDefinition</span><span style="color: #66cc66;">&#40;</span> id <span style="color: #66cc66;">&#41;</span> as <span style="color: #000000; font-weight: bold;">Class</span>;
			<span style="color: #66cc66;">&#125;</span> <span style="color: #0066CC;">catch</span> <span style="color: #66cc66;">&#40;</span> <span style="color: #0066CC;">error</span>:ReferenceError <span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">&quot;Asset not found.&quot;</span> <span style="color: #66cc66;">&#41;</span>;
				dispatchEvent<span style="color: #66cc66;">&#40;</span> <span style="color: #000000; font-weight: bold;">new</span> ErrorEvent<span style="color: #66cc66;">&#40;</span> ErrorEvent.<span style="color: #0066CC;">ERROR</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
&nbsp;
			<span style="color: #b1b100;">return</span> Asset ? Sprite<span style="color: #66cc66;">&#40;</span> <span style="color: #000000; font-weight: bold;">new</span> Asset<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span> : <span style="color: #000000; font-weight: bold;">new</span> Sprite<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">// === E V E N T   H A N D L E R S ===</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> handleLoadComplete<span style="color: #66cc66;">&#40;</span> event:Event <span style="color: #66cc66;">&#41;</span>: <span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			loaderInfo = loader.<span style="color: #006600;">contentLoaderInfo</span>;
			dispatchEvent<span style="color: #66cc66;">&#40;</span> <span style="color: #000000; font-weight: bold;">new</span> Event<span style="color: #66cc66;">&#40;</span> Event.<span style="color: #006600;">COMPLETE</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> handleIOError<span style="color: #66cc66;">&#40;</span> event:IOErrorEvent <span style="color: #66cc66;">&#41;</span>: <span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">&quot;load error: &quot;</span> + event.<span style="color: #0066CC;">text</span> <span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://pixelwelders.com/blog/best-practices/2008/runtime-libraries-in-as3/feed/</wfw:commentRss>
		</item>
		<item>
		<title>The Art and Beauty of Singletons</title>
		<link>http://pixelwelders.com/blog/best-practices/2008/art-and-beauty-of-singletons-in-as3/</link>
		<comments>http://pixelwelders.com/blog/best-practices/2008/art-and-beauty-of-singletons-in-as3/#comments</comments>
		<pubDate>Mon, 14 Apr 2008 09:02:27 +0000</pubDate>
		<dc:creator>Zack Jordan</dc:creator>
		
		<category><![CDATA[ActionScript 3.0]]></category>

		<category><![CDATA[Best Practices]]></category>

		<category><![CDATA[Design Patterns]]></category>

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

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

		<guid isPermaLink="false">http://pixelwelders.com/blog/best-practices/2008/art-and-beauty-of-singletons-in-as3/</guid>
		<description><![CDATA[A) In poker, a card that is the only one of its rank.
B) In animal husbandry, the sole surviving offspring of a litter.
C) In astrology, a single planet alone in a hemisphere (or some crap like that).
D) In mathematics, a set with only one member.
E) In England, a small village about 7 miles north of Chichester in West Sussex.

What do all these things have in common?]]></description>
			<content:encoded><![CDATA[<h3>What is a Singleton?</h3>
<p>A) In poker, a card that is the only one of its rank.<br />
B) In animal husbandry, the sole surviving offspring of a litter.<br />
C) In astrology, a single planet alone in a hemisphere (or some crap like that).<br />
D) In mathematics, a set with only one member.<br />
E) In England, a small village about 7 miles north of Chichester in West Sussex.</p>
<p><img src='http://pixelwelders.com/blog/wp-content/uploads/2008/04/singleton_cat_350.jpg' alt='A cute yet tragic Singleton' style="float:left; margin:0.5em 1.5em 1.5em 0em;" /></p>
<p>All of these things share a trait, and that trait is that each is the only one of its kind.  If two kittens survive from a litter, neither one is a singleton.  If a set has more than one number, it is not a singleton.  In works the same way in object-oriented programming; a Singleton is a class that allows only one instance of itself; if there are more than one, that class cannot be a Singleton.</p>
<p>So why would one want a class that only allows one instance of itself?  I can think of many applications.  A ScoreKeeper class for your game, perhaps, or a Countdown class that keeps track of how much time you have to complete a level.  In <a href=”http://os-wars.com” target=”_blank”>OS Wars</a> I use a Singleton to keep track of all building resources currently in the game.  There’s another one that keeps track of all the units on the battlefield.  In fact, the Battlefield itself is a Singleton, because there’s no reason I would ever need more than one.</p>
<p>Another great thing about a Singleton is that, because there is only one, it’s very easy to get to.  With a normal object, you have to worry about passing it around to the various classes that might need it.  With a Singleton, that’s not necessary.  All you need to do is import the class and voilà- there’s your Singleton.</p>
<p><img src='http://pixelwelders.com/blog/wp-content/uploads/2008/04/singleton_village.jpg' alt='The Village of Singleton' style='float:right; margin:.5em 0em 1.5em 1.5em;padding:0;'/></p>
<p>It works this way because the class itself keeps a static reference to the only instance of itself.  So instead of calling the constructor to get a reference to a new instance, you call the <code>SingletonClassName.instance</code> method to get a reference to the <em>already-created</em> instance.  If that doesn&#8217;t make sense, here&#8217;s an example.  This first piece of code is what a normal ScoreKeeper class would look like.  With this structure, you can use that syntax that everyone knows and loves, <code>new ScoreKeeper()</code>, which returns a brand new ScoreKeeper every time it&#8217;s called.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ScoreKeeper
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> ScoreKeeper<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">&quot;new ScoreKeeper&quot;</span> <span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>But suppose you wanted to ensure that there was only one ScoreKeeper, and you wanted to be able to easily access it from any location in your program.  That means two things: A) the constructor can only be called once, and B) you need to keep track of the new ScoreKeeper that one time the constructor <em>does</em> get called.  What good is having one instance of a class if you can&#8217;t get to it?  So here&#8217;s the Singleton version of the above class:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ScoreKeeper
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #0066CC;">static</span> <span style="color: #000000; font-weight: bold;">var</span> _instance:ScoreKeeper;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #0066CC;">static</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">get</span> instance<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span> <span style="color: #66cc66;">!</span>_instance <span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				_instance = <span style="color: #000000; font-weight: bold;">new</span> ScoreKeeper<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
&nbsp;
			<span style="color: #b1b100;">return</span> _instance;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> ScoreKeeper<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">&quot;new ScoreKeeper!&quot;</span> <span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>	
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>There are two differences here: the static variable <code>_instance</code> and the static method <code>get instance()</code>.  The <code>_instance</code> variable is where the actual instance of this class is stored.  And the <code>get instance()</code> method, of course, enables you to access this instance.  So to access this Singleton, you would use:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript">ScoreKeeper.<span style="color: #006600;">instance</span>;</pre></div></div>

<p>And to call any methods or properties of the Singleton, you would use this syntax:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript">ScoreKeeper.<span style="color: #006600;">instance</span>.<span style="color: #006600;">addScore</span><span style="color: #66cc66;">&#40;</span> <span style="color: #cc66cc;">50</span> <span style="color: #66cc66;">&#41;</span>;
<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span> ScoreKeeper.<span style="color: #006600;">instance</span>.<span style="color: #006600;">score</span> <span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>This of course assumes that your class has a public method called <code>addScore()</code> and a public property called <code>score</code>.  You can get to any public methods or properties this way.</p>
<p>This also adds one more layer, called <em>lazy initialization</em>.  Look in the <code>get instance()</code> method, and notice that before it returns a reference to the Singleton, it checks to see if that reference exists.  If so, it simply returns it.  If not, it creates it first and <em>then</em> returns it.  Either way, you&#8217;re guaranteed to get the one existing instance.</p>
<p>There is one problem remaining- if you were so inclined, you could still call the constructor without going through <code>get instance()</code>, which would of course result in <em>two</em> Singletons.  And as we discussed above, making two of something makes it not a Singleton anymore.  So how can we prevent this?  There are a few ways I&#8217;ve seen floating around, but the simplest is this:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ScoreKeeper
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #0066CC;">static</span> <span style="color: #000000; font-weight: bold;">var</span> _instance:ScoreKeeper;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #0066CC;">static</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">get</span> instance<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span> <span style="color: #66cc66;">!</span>_instance <span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				_instance = <span style="color: #000000; font-weight: bold;">new</span> ScoreKeeper<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
&nbsp;
			<span style="color: #b1b100;">return</span> _instance;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> ScoreKeeper<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span> _instance <span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				<span style="color: #0066CC;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Error</span><span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">&quot;Singleton already exists- use get instance() to access&quot;</span> <span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #66cc66;">&#123;</span>
				<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">&quot;new ScoreKeeper!&quot;</span> <span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
		<span style="color: #66cc66;">&#125;</span>	
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>The new lines are in the constructor.  The <code>get instance()</code> method still only calls the constructor once, but if you forget it&#8217;s a Singleton and call the constructor later, you get a runtime error.</p>
<p>There are also a couple more complicated ways of ensuring that your Singleton is in fact a Singleton, but I haven&#8217;t lost too much sleep over over it.  I usually even leave out the check in the constructor.  Just remember that if it has <code>_instance</code> and <code>get instance()</code>, and a big comment up at the top that says &#8220;SINGLETON!&#8221;, it&#8217;s probably a Singleton.</p>
]]></content:encoded>
			<wfw:commentRss>http://pixelwelders.com/blog/best-practices/2008/art-and-beauty-of-singletons-in-as3/feed/</wfw:commentRss>
		</item>
		<item>
		<title>A Flash Horror Story</title>
		<link>http://pixelwelders.com/blog/best-practices/2008/a-flash-horror-story/</link>
		<comments>http://pixelwelders.com/blog/best-practices/2008/a-flash-horror-story/#comments</comments>
		<pubDate>Mon, 17 Mar 2008 18:29:21 +0000</pubDate>
		<dc:creator>Zack Jordan</dc:creator>
		
		<category><![CDATA[Best Practices]]></category>

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

		<category><![CDATA[horror story]]></category>

		<guid isPermaLink="false">http://pixelwelders.com/blog/?p=21</guid>
		<description><![CDATA[<img src='http://pixelwelders.com/blog/wp-content/uploads/2008/03/moon.jpg' alt='A Terrifying Celestial Body' />Sometimes deadlines make you do things... terrible things.  Things that you wouldn't want associated with your name because they could single-handedly ruin your career.  Systems and classes that you hope against hope will never ever have a problem or need an update.  But the whole time, something in you is telling you, "This is wrong.  This will come back to haunt me."  Sometimes you're lucky.  But usually you're not, and it does come back to haunt you- with a vengeance.]]></description>
			<content:encoded><![CDATA[<p><img src='http://pixelwelders.com/blog/wp-content/uploads/2008/03/moon.jpg' style="margin-right:10px;" alt='A Terrifying Celestial Body' />Sometimes deadlines make you do things&#8230; terrible things.  Things that you wouldn&#8217;t want associated with your name because they could single-handedly ruin your career.  Systems and classes that you hope against hope will never ever have a problem or need an update.  But the whole time, something in you is telling you <em>this is wrong.  This will come back to haunt me.</em>  Sometimes you&#8217;re lucky.  But usually you&#8217;re not, and it does come back to haunt you- with a vengeance.</p>
<p>I want to tell you a story about a man- a <em>desperate</em> man.  We&#8217;ll call him Ted.  Ted was a graphic designer who was just getting into Flash, and so he took a freelance job with a local Flash development house to put his new skills to use.  This company tasked him with creating a fun little app that allowed the user to customize a car image.  The user, they said, should be able to add logos, spoilers, &#8220;dubs,&#8221; etc. and then download his/her fantastic creation as a wallpaper.  In fact, they continued, we&#8217;ve already finished the layout and the car images- here&#8217;s an FLA and a zip of all 170 possible combinations.</p>
<p>At this point, many developers are already putting this app together in their own minds.  Twenty lines of code, maybe a splash of XML, a big image directory- call it a day.  That is, in fact, what the development company expected.  However, the days turned to weeks and Ted had not turned in his project.  Phone calls became less frequent and more harried.  Finally, hours before deadline, Ted emailed a link to the project.  It loaded a bit more slowly than anticipated, but it was assumed that it was just because of the size of the graphics.  Which, as we were to find, it was.</p>
<p>However, the client soon decided that a small change was needed.  Because of the rather lengthy turnaround time with Ted (who was now slogging through another project), the development company gave it to an in-house guy this time, who was confident that he could make the change in a half hour.  How complicated could an app like that possibly be?  He downloaded Ted&#8217;s source from the development server and opened it&#8230; and that part of his soul that every human is born with, that sings because the Earth is beautiful and its people are good- that part died within him.</p>
<p><img src='http://pixelwelders.com/blog/wp-content/uploads/2008/03/flash_nightmare.jpg' alt='A Flash timeline nightmare' style="float:right;margin:.5em 0em 1.5em 1.5em;padding:0;" /></p>
<p>The source files bore the unmistakable mark of a madman, drunk with desperation.  The project contained one hundred seventy keyframes, each containing one of the car images.  To view one image, in other words, required downloading its 169 siblings as well.  Furthermore, each keyframe held five buttons for adding the various options to the car.  Five completely new instances per keyframe, each with a unique instance name (redSpoiler155_btn, redSpoiler156_btn, etc.).  <em>Furthermore</em>, each of the five buttons had a variant of the following code attached directly to it:</p>
<p><code>on(release){gotoAndStop(51)}</code></p>
<p>170 x 5 = 850 identical lines of code.  In 170 unlabeled keyframes.  Attached to 850 separate and unique button instances.  In a project with 170 wallpaper-sized images that, when compiled, produced a 20MB SWF.  The reason neither the development house or the client noticed the download time was because they were operating on A) industrial-strength Internet connections and B) the assumption that the freelancer hired was not a complete sociopath.</p>
<p>To be fair, we all know what happened.  The guy had gained a couple skills and went with what he knew, rather than doing a ten-minute Google search, without ever thinking <em>seriously, there has to be a better way.</em>  Nonetheless, the second his &#8220;techniques&#8221; came to light, the development house called him to request that he invoice them immediately and go work for someone else.</p>
<p>They say on certain nights, when the moon is full, you can still hear the feverish and half-insane clicking of a man attaching code to onstage objects.</p>
]]></content:encoded>
			<wfw:commentRss>http://pixelwelders.com/blog/best-practices/2008/a-flash-horror-story/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>

