<?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>Shide and Prame &#187; Flash and ActionScript</title>
	<atom:link href="http://aidan.rfm.co.nz/blog/tag/flash/feed/" rel="self" type="application/rss+xml" />
	<link>http://aidan.rfm.co.nz/blog</link>
	<description>Aidan&#039;s digital life</description>
	<lastBuildDate>Fri, 21 May 2010 01:45:51 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>onReleaseOutside for AS3</title>
		<link>http://aidan.rfm.co.nz/blog/2010/03/onreleaseoutside-for-as3/</link>
		<comments>http://aidan.rfm.co.nz/blog/2010/03/onreleaseoutside-for-as3/#comments</comments>
		<pubDate>Tue, 23 Mar 2010 22:32:08 +0000</pubDate>
		<dc:creator>aidan</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Flash and ActionScript]]></category>
		<category><![CDATA[Rant]]></category>
		<category><![CDATA[Shortcuts and Timesavers]]></category>

		<guid isPermaLink="false">http://aidan.rfm.co.nz/blog/?p=119</guid>
		<description><![CDATA[Recently I was horrified to learn that there is no AS3 equivalent for the AS2 event &#8220;onReleaseOutside&#8221;. I consider to be an extremely useful and important event. Although it can be emulated faithfully with the provided mouse events and a complicated bunch of calls to add/removeEventListener, it&#8217;s something you have to implement again and again. [...]]]></description>
			<content:encoded><![CDATA[<p>Recently I was horrified to learn that there is no AS3 equivalent for the AS2 event &#8220;onReleaseOutside&#8221;. I consider to be an extremely useful and important event. Although it can be emulated faithfully with the provided mouse events and a complicated bunch of calls to add/removeEventListener, it&#8217;s something you have to implement again and again. Because I was too lazy, I&#8217;ve been grumbling, sighing, and copy-pasting code without much more thought.</p>
<p>But as it turns out, I don&#8217;t have to anymore because Tyler of xtyler.com has released a super-groovy as3 library for <a title="AS3 Button Behavior Made Easy" href="http://www.xtyler.com/code/112/">making any InteractiveObject behave as a button</a>.</p>
<p>Siic! [sic]</p>
]]></content:encoded>
			<wfw:commentRss>http://aidan.rfm.co.nz/blog/2010/03/onreleaseoutside-for-as3/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>addEventListener() is the new malloc()</title>
		<link>http://aidan.rfm.co.nz/blog/2010/02/addeventlistener-is-the-new-malloc/</link>
		<comments>http://aidan.rfm.co.nz/blog/2010/02/addeventlistener-is-the-new-malloc/#comments</comments>
		<pubDate>Wed, 10 Feb 2010 01:15:17 +0000</pubDate>
		<dc:creator>aidan</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Flash and ActionScript]]></category>

		<guid isPermaLink="false">http://aidan.rfm.co.nz/blog/?p=11</guid>
		<description><![CDATA[If you repeatedly (or naïvely) add event listeners but forget to remove them, even a small, simple flash application can eventually end up hogging a very large amount of memory. Sound familiar?]]></description>
			<content:encoded><![CDATA[<p>Remember back when you learned C? Way, way back in time? Well I do. When I recall my experience with C, I remember it being intoxicatingly fast and powerful. Kinda felt like mainlining into an artery of the machine: so much potential for incredible speed, but also horrendously dangerous and unforgiving.</p>
<p><span id="more-11"></span></p>
<p>For a coding noob, C&#8217;s requirement for manual memory management is perhaps the most time- and brain-consuming &#8216;feature&#8217;. Writing anything but the most basic of programs (i.e. using only &#8220;stack&#8221; memory) required consistent focus on the task of correctly allocating, managing, and deallocating memory. Accidentally accessing data from a part of memory that you hadn&#8217;t allocated resulted in that familiar spartan sting: <code>Segmentation fault</code>. No fancy stack trace, elaboration, or even an apology. The machine just smugly states how  <code>you fucked up</code> and dumps you back to the shell. I remember an amusing graffiti on the computer lab&#8217;s whiteboard: &#8220;it&#8217;s all seg&#8217;s fault!&#8221;</p>
<p>Since I&#8217;d just come from a Java background the previous year, I found this &#8220;memory stuff&#8221; initially very frustrating and intimidating. Though I didn&#8217;t write off C as &#8220;just a stupid or difficult language&#8221; because I firmly believed that this overhead must be a tradeoff for some other benefits which I had yet to discover or fully appreciate.</p>
<p>Although I learned later that there was a debugger (gdb) that took away a lot of the pain of seg faults, it didn&#8217;t make the task of managing memory any easier: it was just helpful when things went wrong. The debugger doesn&#8217;t help, for example, with memory leaks. If you repeatedly allocated memory but forgot to deallocate it, even a small, simple program could eventually end up hogging a very large amount of memory.</p>
<p>As time went on, I got comfortable with managing memory, but I never forgot about my assumption that there were something(s) so amazing about the language that balanced up the overhead for manual memory management.</p>
<p>And to be fair, C is an inately quick language, which is fairly obvious from the start. This is mostly because it&#8217;s pretty much the CPU&#8217;s roommate: CPUs and the C language have evolved together over the last few decades, and they&#8217;ve formed a pretty formidable partnership. But also, some features of C (e.g. memory pointers) open up algorithmic possibilities that don&#8217;t really have a comparable equivalent in other managed languages, like Java.</p>
<h3>Memory Management as a Focus Burglar</h3>
<p>Having said this though, I remember the day that I decided &#8220;this isn&#8217;t worth it&#8221;. I had grown to really enjoy aspects of memory management and the associated benefits, but for me, it just wasn&#8217;t worth the significant overhead of time and <em>focus</em>. The tasks of manual memory <em>steal focus</em> from the programmer, and make it more difficult to concentrate on the actual problem at hand.</p>
<p>So since then, I&#8217;ve hardly touched any unmanaged languages. I&#8217;ve had many arguments about whether or not this is a bad thing, but in the end I&#8217;m pretty happy with it. <em>So there</em>.</p>
<p>I&#8217;ve relatively recently taken an interest in Flash, mostly because of its relatively new, strongly-typed, compiled, managed language: ActionScript 3. It&#8217;s a super groovy language, with a decent community and lots of sweet libraries (some of which have been ironically ported from C and C++). Although its previous version was a lot like JavaScript, AS3 is more akin to Java than any other language. AS3 makes conventional, structured application development possible.</p>
<p>And, since the world seemed to be moving inexorably toward managed languages, I thought that I had left the world of memory leaks in the archaic past, and that it would henceforth be dredged up only in drunken thought experiments with old-school hackers in dingy bars at 4am.</p>
<h3>Not &#8217;til the Fat Lady sings</h3>
<p>But I was wrong. Because <code>addEventListener()</code> is the new <code>malloc()</code>.</p>
<p>If you repeatedly (or naïvely) add event listeners but forget to remove them, even a small, simple flash application can eventually end up hogging a very large amount of memory. Sound familiar?</p>
<p>Typically this happens when an developer creates load listeners, or mouse listeners, then removes assets from the display list without removing the listeners. Those assets still remain in memory as long as they are &#8220;listened to&#8221;. This is because the listener (e.g. the stage) retains a list of all of its dispatchers, so the listener can&#8217;t be garbage collected until it is removed from this list. Using &#8220;weak links&#8221; when creating adding listeners can help, but I have historically found this too buggy to rely on.</p>
<p>In ActionScript 2, although this mechanic was also possible, it manifested itself less often, because Flash assets were removed from all dispatcher lists (and from memory) when they were removed from the display list using <code>unloadAndStop()</code> or similar. In AS3, however, assets can exist before or after they&#8217;ve been removed from the display list, which is less forgiving for sloppy coding.</p>
<p>This of course, isn&#8217;t specific to ActionScript: just <em>any</em> managed language which includes or supports event dispatchers and listeners  (C#, Java, etc.). Extra care needs to be taken when using this model. Everytime you add a &#8220;transient&#8221; listener, there should be code somewhere else to remove that listener, just like malloc/dealloc.</p>
<p>Here&#8217;s some psuedo-as code to demonstrate:</p>
<pre>loader.addEventListener(COMPLETE, onLoadComplete);
function onLoadComplete() {
  loader.removeEventListener(COMPLETE, onLoadComplete);
  // ... (Loader handling code)
}
</pre>
<p>So that&#8217;s pretty much it.</p>
]]></content:encoded>
			<wfw:commentRss>http://aidan.rfm.co.nz/blog/2010/02/addeventlistener-is-the-new-malloc/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Strange Flash framerate issue</title>
		<link>http://aidan.rfm.co.nz/blog/2010/02/strange-flash-framerate-issue/</link>
		<comments>http://aidan.rfm.co.nz/blog/2010/02/strange-flash-framerate-issue/#comments</comments>
		<pubDate>Wed, 10 Feb 2010 00:41:09 +0000</pubDate>
		<dc:creator>aidan</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Flash and ActionScript]]></category>

		<guid isPermaLink="false">http://aidan.rfm.co.nz/blog/2010/02/strange-flash-framerate-issue/</guid>
		<description><![CDATA[I'm using Flash CS4, and the fps is set to 30. However, when I run it normally (within flash using "Test Movie") I get a frame rate of 19.95 fps or so, and it's quite steady. When I run it externally, with the flash player or in a browser, I get pretty much exactly 30 fps. What gives?]]></description>
			<content:encoded><![CDATA[<p>I noticed today that the Flash game I&#8217;m working on has some odd framerate quirks, and I was wondering if anyone else has noticed similar behaviour. I know that Flash&#8217;s framerates can be quite unsteady at the best of times, but this is not the issue I&#8217;m having (at the moment).</p>
<p>I&#8217;m using Flash CS4, and the fps is set to 30. However, when I run it normally (within flash using &#8220;Test Movie&#8221;) I get a frame rate of 19.95 fps or so, and it&#8217;s quite steady. When I run it externally, with the flash player or in a browser, I get pretty much exactly 30 fps.</p>
<p>The frame rate in Flash remains at about 19.95 even if I bump up the fps to 60, even though this is faster fps is reflected correctly when it&#8217;s run externally. The game is a simple tile-based puzzle game, and is certainly not CPU bound. When running inside Flash, it&#8217;s using about 13% of the CPU.</p>
<p>When I set the framerate to, say, 10 fps, it runs at the correct speed everywhere.</p>
<p>Anyone have similar experiences with Flash? Any advice?</p>
]]></content:encoded>
			<wfw:commentRss>http://aidan.rfm.co.nz/blog/2010/02/strange-flash-framerate-issue/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Saving time when dynamically constructing BitmapData subclasses created in Flash</title>
		<link>http://aidan.rfm.co.nz/blog/2010/02/saving-time-when-dynamically-constructing-bitmapdata-subclasses-created-in-flash/</link>
		<comments>http://aidan.rfm.co.nz/blog/2010/02/saving-time-when-dynamically-constructing-bitmapdata-subclasses-created-in-flash/#comments</comments>
		<pubDate>Wed, 10 Feb 2010 00:24:22 +0000</pubDate>
		<dc:creator>aidan</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Flash and ActionScript]]></category>
		<category><![CDATA[Rant]]></category>

		<guid isPermaLink="false">http://aidan.rfm.co.nz/blog/2010/02/saving-time-when-dynamically-constructing-bitmapdata-subclasses-created-in-flash/</guid>
		<description><![CDATA[When dynamically constructing embedded bitmaps which have been defined in the Flash Studio, it turns out that you don't actually need to pass the constructor the correct width and height, which can save heaps of time.]]></description>
			<content:encoded><![CDATA[<p>When constructing custom <code>BitmapData</code> subclasses which have been defined in the Flash Studio (such as <code>MyFancyBitmapData</code>), it turns out that you don&#8217;t actually need to pass the correct width and height of the bitmap. The <code>MyFancyBitmapData</code> class completely ignores its two (required) constructor parameters, and uses hard coded ones.</p>
<p>So don&#8217;t bother meticulously violating <acronym title="Don't Repeat Yourself">DRY</acronym> principles when creating these things dynamically. You can pass any integers you like, including zero or negative one, but I particularly like using <code>NaN</code> because it makes it clear that you&#8217;re not even trying to supply a number that could possibly be interpreted in any meaningful way (even if that&#8217;s a tad passive aggressive&#8230;)</p>
<pre>// e.g.
var myBitmapData:BitmapData = new MyFancyBitmap(NaN,NaN);
trace(myBitmapData.width + ", " + myBitmapData.height); // outputs: 45, 45</pre>
<p>It seems a bit ugly to me that the generated <code>BitmapData</code> subclasses require their parameters. I really do think they should be optional at the very least, and ideally, illegal.</p>
<p>Hope this saves someone some time.</p>
]]></content:encoded>
			<wfw:commentRss>http://aidan.rfm.co.nz/blog/2010/02/saving-time-when-dynamically-constructing-bitmapdata-subclasses-created-in-flash/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Catmull-Rom curve</title>
		<link>http://aidan.rfm.co.nz/blog/2009/08/catmull-rom-curve/</link>
		<comments>http://aidan.rfm.co.nz/blog/2009/08/catmull-rom-curve/#comments</comments>
		<pubDate>Sun, 09 Aug 2009 05:54:39 +0000</pubDate>
		<dc:creator>aidan</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Computer Programming]]></category>
		<category><![CDATA[Flash and ActionScript]]></category>

		<guid isPermaLink="false">http://aidan.rfm.co.nz/blog/?p=49</guid>
		<description><![CDATA[Ever wanted to draw a curve directly through any number of points with the Flash Graphics class?]]></description>
			<content:encoded><![CDATA[<p>Ever wanted to draw a curve directly through any number of points with the Flash Graphics class?</p>
<p>Here&#8217;s a class that will do that for you. It uses a &#8220;<a title="Catmull-Rom wikipedia article" href="http://en.wikipedia.org/wiki/Catmull-Rom_spline#Catmull.E2.80.93Rom_spline" target="_blank">Catmull-Rom</a>&#8221; spline (named after its inventors), which draws directly through any number of points.</p>
<p>It&#8217;s not a spectacular or pretty looking curve. It&#8217;s very &#8220;dowdy&#8221;, and somewhat ugly, but it doesn&#8217;t make wild swinging curves like many other equations. This humble quality makes it really good for situations where it&#8217;s important that the curve stays pretty close to the control points.</p>
<p><a href="http://aidan.rfm.co.nz/blog/wp-content/catmullromexample.swf">Example SWF</a> (<a href="http://aidan.rfm.co.nz/blog/wp-content/catmullromexample.as">actionscript code</a>)</p>
<p><a href="http://aidan.rfm.co.nz/blog/wp-content/catmullrom.as">CatmullRom curve actionscript class</a></p>
]]></content:encoded>
			<wfw:commentRss>http://aidan.rfm.co.nz/blog/2009/08/catmull-rom-curve/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
