<?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>alecmce.com &#187; gotcha</title>
	<atom:link href="http://alecmce.com/category/gotcha/feed" rel="self" type="application/rss+xml" />
	<link>http://alecmce.com</link>
	<description></description>
	<lastBuildDate>Thu, 10 Nov 2011 21:56:45 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Gotcha &#8211; BitmapData.draw(stage) and FullScreen Mode</title>
		<link>http://alecmce.com/as3/drawfullscreengotcha</link>
		<comments>http://alecmce.com/as3/drawfullscreengotcha#comments</comments>
		<pubDate>Thu, 14 Oct 2010 03:26:02 +0000</pubDate>
		<dc:creator>alecmce</dc:creator>
				<category><![CDATA[as3]]></category>
		<category><![CDATA[gotcha]]></category>

		<guid isPermaLink="false">http://alecmce.com/?p=1038</guid>
		<description><![CDATA[When you set stage.displayState to StageDisplayState.FULL_SCREEN a display overlays your application: Where does it come from? Clearly it&#8217;s built into the Flash Player, but what may be surprising is that when it displays, it is actually added to the stage. It doesn&#8217;t float above the stage, it is added to the stage. This is no [...]]]></description>
			<content:encoded><![CDATA[<p>When you set <code>stage.displayState</code> to <code>StageDisplayState.FULL_SCREEN</code> a display overlays your application:</p>
<p><img src="http://alecmce.com/wp-content/uploads/2010/10/PressEscToExitFullScreenMode.png" alt="" title="PressEscToExitFullScreenMode" width="608" height="70" class="aligncenter size-full wp-image-1042" /></p>
<p>Where does it come from? Clearly it&#8217;s built into the Flash Player, but what may be surprising is that when it displays, it is actually added to the stage. It doesn&#8217;t float above the stage, it is added to the stage. This is no big deal&#8230; usually.</p>
<p>To consider when it might be a problem, try hitting the fullscreen button then hitting the draw button in the demo below. (Depending on the size of your screen, mouse skills will probably be required!)</p>

    <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" id="swfobj_0" width="640" height="480" align="center">
      <param name="movie" value="http://alecmce.com/wp-content/uploads/2010/10/Main6.swf" />
      <param name="align" value="center" />
      <param name="allowfullscreen" value="true" />
      <!--[if !IE]>-->
      <object type="application/x-shockwave-flash" data="http://alecmce.com/wp-content/uploads/2010/10/Main6.swf" width="640" height="480" align="center" allowfullscreen="true">
      <!--<![endif]-->
        
      <!--[if !IE]>-->
      </object>
      <!--<![endif]-->
    </object>

<p>One of the problems with adding the display to the stage is that it sits in a different security domain to your application. If your application attempts to reference the stage, for example to draw the stage into a <code>BitmapData</code>, then you get hit with a <code> Security sandbox violation</code>. Of course, if your mouse skills weren&#8217;t up to it and the element had removed itself from the stage, the error would not occur.</p>
<p>Now, there is probably no good reasons why you should ever need to take draw a copy of the stage, and in fact I found this error because I had (or maybe a colleague had?) done it when I wanted to draw a copy of a particular element. However, since screen-wipe transitions and other effects could easily be implemented in such a way (and since their implementation usually precedes implementing a fullscreen button), the potential for this error does exist.</p>
<p>It doesn&#8217;t look like anyone has noticed this error before. I searched for <code>https://www.macromedia.com/support/flashplayer/sys/</code> in JIRA and Google and didn&#8217;t find anything useful. I present it here in the hope that at least one other person won&#8217;t be skewered on this AS3 Gotcha like me!</p>
</p>
<p>ps. I took some liberties with the code as displayed in the demo above. For full disclosure, <a href="http://gist.github.com/625470">view the real source code on GitHub</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://alecmce.com/as3/drawfullscreengotcha/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Error #2078 on Flash IDE publish &#8211; Gotcha</title>
		<link>http://alecmce.com/as3/error-2078-on-flash-ide-publish-gotcha</link>
		<comments>http://alecmce.com/as3/error-2078-on-flash-ide-publish-gotcha#comments</comments>
		<pubDate>Mon, 24 May 2010 23:11:13 +0000</pubDate>
		<dc:creator>alecmce</dc:creator>
				<category><![CDATA[as3]]></category>
		<category><![CDATA[gotcha]]></category>

		<guid isPermaLink="false">http://alecmce.com/?p=907</guid>
		<description><![CDATA[I just spent a couple of hours creating an asset in the Flash IDE, and when I went to publish this FLA with absolutely no code except one stop(); in it, I got the following error: The culprit? I had named a MovieClip instance within the IDE &#8220;name&#8221;, which the Flash player doesn&#8217;t like at [...]]]></description>
			<content:encoded><![CDATA[<p>I just spent a couple of hours creating an asset in the Flash IDE, and when I went to publish this FLA with absolutely no code except one <code>stop();</code> in it, I got the following error:</p>
<pre class="brush: plain; title: ; notranslate">
Error: Error #2078: The name property of a Timeline-placed object cannot be modified.
	at flash.display::DisplayObject/set name()
	at flash.display::Sprite/constructChildren()
	at flash.display::Sprite()
	at flash.display::MovieClip()
</pre>
<p>The culprit? I had named a MovieClip instance within the IDE &#8220;name&#8221;, which the Flash player doesn&#8217;t like at all. It would have been good of the IDE to warn me of this problem (ie, scan the named MovieClips in an FLA and check for reserved words), or give me a more graceful error message, but sadly not. Instead I had to find it the hard way &#8211; changing stuff and publishing, changing stuff and publishing, changing stuff and publishing&#8230; Hopefully this will save someone a few minutes!</p>
]]></content:encoded>
			<wfw:commentRss>http://alecmce.com/as3/error-2078-on-flash-ide-publish-gotcha/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>[Embed(source=&quot;asset.swf&quot;)] Gotcha (and Workaround)</title>
		<link>http://alecmce.com/as3/embed-asset-gotcha</link>
		<comments>http://alecmce.com/as3/embed-asset-gotcha#comments</comments>
		<pubDate>Fri, 21 May 2010 07:02:58 +0000</pubDate>
		<dc:creator>alecmce</dc:creator>
				<category><![CDATA[as3]]></category>
		<category><![CDATA[gotcha]]></category>
		<category><![CDATA[library]]></category>

		<guid isPermaLink="false">http://alecmce.com/?p=894</guid>
		<description><![CDATA[It&#8217;s been a long-time without a post. Part of the reason for that has been starting work at a new job (which involves a commute), and part has been that as part of my new job, I was actually encouraged to spend time playing World of Warcraft. To me, that&#8217;s like taking a crowbar to [...]]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s been a long-time without a post. Part of the reason for that has been starting work at a new job (which involves a commute), and part has been that as part of my new job, I was actually encouraged to spend time playing World of Warcraft. To me, that&#8217;s like taking a crowbar to Pandora&#8217;s Box and having a peek inside. I learned quite a lot, but have also played just a few too many Warsong Gulches.</p>
<p>Back to work, and I was doing a little prototyping this evening, when I came across a familiar problem: In <code>AS3</code> we can use</p>
<pre class="brush: as3; title: ; notranslate">
[Embed(source=&quot;asset.swf&quot;, symbol=&quot;symbol&quot;)]
private var symbolClass:Class;

var symbol:MovieClip = new symbolClass();
</pre>
<p>to embed a symbol from an art <code>SWF</code> in what is probably a code-built <code>SWF</code>. That&#8217;s great, but what if you want to embed an entire <code>SWF</code>?</p>
<pre class="brush: as3; title: ; notranslate">
[Embed(source=&quot;asset.swf&quot;)]
private var assetClass:Class;

var asset:MovieClip = new assetClass();
</pre>
<p>looks like it should do the trick, but you can&#8217;t access any of the information within the asset. That&#8217;s a real pain, the reason for which is pretty convoluted. I remembered working around this problem in the past, and happily managed to unearth a long-forgotten treasure in my codebase, which I thought I&#8217;d share (having rapidly refactored it to use as3-signals, naturally).</p>
<pre class="brush: as3; title: ; notranslate">
package com.alecmce.util
{
	import org.osflash.signals.Signal;

	import mx.core.MovieClipAsset;

	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.display.MovieClip;
	import flash.events.Event;

	public class UnpackEmbed
	{
		private var _ready:Signal;

		private var _asset:MovieClipAsset;
		private var _content:MovieClip;

		public function UnpackEmbed(assetClass:Class)
		{
			_asset = new assetClass();
			_ready = new Signal(UnpackEmbed);

			var loader:Loader = Loader(_asset.getChildAt(0));
			var info:LoaderInfo = loader.contentLoaderInfo;
			info.addEventListener(Event.COMPLETE, onLoadComplete);
		}

		private function onLoadComplete(event:Event):void
		{
			var info:LoaderInfo = LoaderInfo(event.target);
			info.removeEventListener(Event.COMPLETE, onLoadComplete);

			_content = MovieClip(info.loader.content);
			_ready.dispatch(this);
		}

		public function get content():MovieClip
		{
			return _content;
		}

		public function get ready():Signal
		{
			return _ready;
		}

		public function get asset():MovieClipAsset
		{
			return _asset;
		}
	}
}
</pre>
<p>When you embed a SWF in this way then instantiate it, Flash somehow conspires to create a <code>MovieClipAsset</code> with a <code>Loader</code> inside, which will be &#8216;loading&#8217; the already-embedded content. The content is not available immediately (it may be sometimes, I have encountered cases where it was not), so you have to wait for an <code>Event.COMPLETE</code> to be fired before you can access it. This class exposes a signal that informs you when the content is ready. It could probably be more rigorous, such as including an <code>isComplete</code> flag, but it serves my purposes, when used in the following manner:</p>
<pre class="brush: as3; title: ; notranslate">
[Embed(source=&quot;asset.swf&quot;)]
private var assetClass:Class;

asset = new UnpackEmbed(assetClass);
asset.ready.addOnce(onAssetReady);

private function onAssetReady(asset:UnpackEmbed):void
{
	// now we can access the asset.content!
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://alecmce.com/as3/embed-asset-gotcha/feed</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>Gotcha &#8211; transform.matrix and scaleX/Y</title>
		<link>http://alecmce.com/as3/gotcha-transform-scale</link>
		<comments>http://alecmce.com/as3/gotcha-transform-scale#comments</comments>
		<pubDate>Tue, 23 Feb 2010 17:00:47 +0000</pubDate>
		<dc:creator>alecmce</dc:creator>
				<category><![CDATA[as3]]></category>
		<category><![CDATA[gotcha]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[tdd]]></category>

		<guid isPermaLink="false">http://alecmce.com/?p=720</guid>
		<description><![CDATA[I came across this gotcha for the first time today. Praise-be to ASUnit, without which this could have been extremely difficult to unearth: Both clips have been scaled by -0.5, but because the transform was used to produce the scale for a, the scaleX/Y variables do not reflect the object&#8217;s true scale. Now, just to [...]]]></description>
			<content:encoded><![CDATA[<p>I came across this gotcha for the first time today. Praise-be to <a href="http://asunit.org/">ASUnit</a>, without which this could have been extremely difficult to unearth:</p>
<pre class="brush: as3; title: ; notranslate">
// given two sprites called &quot;a&quot; and &quot;b&quot;...

a.transform.matrix = new Matrix(-.5, 0, 0, -.5, a.x, a.y);
b.scaleX = b.scaleY = -.5;

trace(a.scaleX, a.scaleY, b.scaleX, b.scaleY); // outputs: 0.5, 0.5, -0.5, -0.5
</pre>
<p>Both clips have been scaled by -0.5, but because the transform was used to produce the scale for a, the scaleX/Y variables do not reflect the object&#8217;s true scale.</p>
<p>Now, just to confuse yourself, add a third sprite to the stage:</p>
<pre class="brush: as3; title: ; notranslate">
// given another sprite called &quot;c&quot;...

c.transform.matrix = new Matrix(-.5, 0, 0, -.5, c.x, c.y);
c.scaleX = c.scaleY = -.5;
</pre>
<p>Curiouser and curiouser! This needed a more complete experimentation&#8230;</p>
<div style="text-align: center;">

    <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" id="swfobj_1" width="550" height="400">
      <param name="movie" value="http://www.alecmce.com.php5-16.dfw1-1.websitetestlink.com/wp-content/uploads/2010/02/scaleAndTransform1.swf" />
      <!--[if !IE]>-->
      <object type="application/x-shockwave-flash" data="http://www.alecmce.com.php5-16.dfw1-1.websitetestlink.com/wp-content/uploads/2010/02/scaleAndTransform1.swf" width="550" height="400">
      <!--<![endif]-->
        
      <!--[if !IE]>-->
      </object>
      <!--<![endif]-->
    </object>

</div>
<p>The source for this experiment can be found here: <a href='http://www.alecmce.com.php5-16.dfw1-1.websitetestlink.com/wp-content/uploads/2010/02/scaleAndTransform1.zip'>scaleAndTransform</a>. The buttons call the following methods:</p>
<pre class="brush: as3; title: ; notranslate">
function onGo(event:Event):void
{
	a.transform.matrix = new Matrix(-.5, 0, 0, -.5, a.x, a.y);

	b.scaleX = b.scaleY = -.5;

	c.transform.matrix = new Matrix(-.5, 0, 0, -.5, c.x, c.y);
	c.scaleX = c.scaleY = -.5;
}

function onResetScale(event:Event):void
{
	a.scaleX = a.scaleY = 1;
	b.scaleX = b.scaleY = 1;
	c.scaleX = c.scaleY = 1;
}

function onResetTransform(event:Event):void
{
	a.transform.matrix = new Matrix(1, 0, 0, 1, a.x, a.y);
	b.transform.matrix = new Matrix(1, 0, 0, 1, b.x, b.y);
	c.transform.matrix = new Matrix(1, 0, 0, 1, c.x, c.y);
}
</pre>
<h2>What Is Going On?</h2>
<p>If you apply a transformation matrix then a scale, the magnitude of the scale is applied, but the direction is determined by the product of the transformation and the scalar&#8230; which is to say that:</p>
<pre class="brush: as3; title: ; notranslate">a.realScaleX = (a.transform.matrix.a * a.scaleX &gt; 0 ? 1 : -1) * a.scaleX;</pre>
<p>If however you apply a scale then a transformation, the scale is defined by the transformation only!</p>
<pre class="brush: as3; title: ; notranslate">a.realScaleX = a.transform.matrix.a;</pre>
<h2>Bug?</h2>
<p>Does this qualify as a bug? I haven&#8217;t thought about this for long enough. The conditions in which a transformation matrix rotates and skews the MovieClip confuse me; how would I define scaleX and scaleY in that scenario? I need to give this some thought; perhaps you the community can give me some help! I am reluctant to call anything a bug immediately now, after my experience with my <a href="http://alecmce.com/as3/the-problem-with-vector">The Problem With Vector.&lt;T&gt;</a> post, where I was just plain wrong!</p>
<p>A similar bug exists in Adobe JIRA: <a href="http://bugs.adobe.com/jira/browse/SDK-25247"> UIComponent.scaleX, scaleY differ from transform.matrix.a,d</a>. You&#8217;ll need an account to look at this &#8211; it&#8217;s free, and once you&#8217;ve got it you can pester Adobe with your bugs too.</p>
]]></content:encoded>
			<wfw:commentRss>http://alecmce.com/as3/gotcha-transform-scale/feed</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Vector.unshift Gotcha (for the time-being)</title>
		<link>http://alecmce.com/as3/vector-unshift-gotcha-for-the-time-being</link>
		<comments>http://alecmce.com/as3/vector-unshift-gotcha-for-the-time-being#comments</comments>
		<pubDate>Thu, 27 Aug 2009 18:56:33 +0000</pubDate>
		<dc:creator>alecmce</dc:creator>
				<category><![CDATA[as3]]></category>
		<category><![CDATA[gotcha]]></category>

		<guid isPermaLink="false">http://alecmce.com/?p=294</guid>
		<description><![CDATA[I didn&#8217;t see this coming: No, I haven&#8217;t made a mistake. It&#8217;s a bug! The Flash 10 Language Reference explicitly says that this isn&#8217;t supposed to happen. After a little searching, Adobe JIRA affirms that this bug is known, and fixed &#8211; well fixed in the &#8216;Future &#8211; Next Build&#8217; release. I really hate wasting [...]]]></description>
			<content:encoded><![CDATA[<p>I didn&#8217;t see this coming:</p>
<pre class="brush: as3; title: ; notranslate">
var list:Vector.&lt;MockObject&gt; = new Vector.&lt;MockObject&gt;();

var a:MockObject = new MockObject(&quot;a&quot;);
var b:MockObject = new MockObject(&quot;b&quot;);
var c:MockObject = new MockObject(&quot;c&quot;);

list.push(a);
trace(list);			// outputs &quot;a&quot;

list.push(b);
trace(list);			// outputs &quot;a,b&quot;

list.unshift(c);
trace(list);			// outputs &quot;c,a&quot;
</pre>
<p>No, I haven&#8217;t made a mistake. It&#8217;s a bug! <a href="http://help.adobe.com/en_US/AS3LCR/Flash_10.0/Vector.html#unshift()">The Flash 10 Language Reference</a> explicitly says that this isn&#8217;t supposed to happen. After a little searching, <a href="http://bugs.adobe.com/jira/browse/ASC-3620">Adobe JIRA</a> affirms that this bug is known, and fixed &#8211; well fixed in the &#8216;Future &#8211; Next Build&#8217; release.</p>
<p>I really hate wasting two hours of my time to discover that my test case is failing because the Flash Player is itself broken, and not because I&#8217;m an idiot. Clearly, Adobe don&#8217;t unit test. As ever, I am really glad that I do!</p>
]]></content:encoded>
			<wfw:commentRss>http://alecmce.com/as3/vector-unshift-gotcha-for-the-time-being/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>XML Constants Gotcha</title>
		<link>http://alecmce.com/as3/xml-constants-gotcha</link>
		<comments>http://alecmce.com/as3/xml-constants-gotcha#comments</comments>
		<pubDate>Thu, 13 Aug 2009 12:11:03 +0000</pubDate>
		<dc:creator>alecmce</dc:creator>
				<category><![CDATA[as3]]></category>
		<category><![CDATA[gotcha]]></category>
		<category><![CDATA[const]]></category>
		<category><![CDATA[mutable]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://alecmce.com/?p=259</guid>
		<description><![CDATA[In my head, XML slips between the cracks of being an Object and a primitive type. It shouldn&#8217;t, and when I&#8217;m concentrated it doesn&#8217;t. But still, there is something primitive-type about it. and are intuitively the same sorts of things. XML is like an advanced sort of String. We encode it as we encode text, [...]]]></description>
			<content:encoded><![CDATA[<p>In my head, XML slips between the cracks of being an Object and a primitive type. It shouldn&#8217;t, and when I&#8217;m concentrated it doesn&#8217;t. But still, there is something primitive-type about it.</p>
<pre class="brush: as3; title: ; notranslate">private const DATA:XML = &lt;data message=&quot;Hello World!&quot; /&gt;;</pre>
<p>and</p>
<pre class="brush: as3; first-line: 2; title: ; notranslate">private const DATA:String = &quot;Hello World!&quot;;</pre>
<p>are intuitively the same sorts of things. XML is like an advanced sort of String. We encode it as we encode text, and we convert it to and from Strings without difficulty. I don&#8217;t construct XML, at least it doesn&#8217;t <em>feel</em> like I do. In my preferred coding environment (<a href="http://fdt.powerflasher.com/">FDT</a>) by default they&#8217;re even similarly coloured.</p>
<p>However, they are clearly not the same, because XML is mutable:</p>
<pre class="brush: as3; first-line: 3; title: ; notranslate">DATA.@message = &quot;This message has been changed!&quot;;</pre>
<p>Needless to say, that doesn&#8217;t work on a String! Somehow by labelling the XML a &#8216;constant&#8217; I expect the XML to be immutable.</p>
<p>On reflection I know this about XML, but it <em>feels</em> wrong. This &#8216;gotcha&#8217; does not identify a language bug, but suggests a mental corrective: when you think about XML, make sure that you don&#8217;t mistake it for a primitive type. When I&#8217;m not concentrating I do, and it occassionally trips me up.</p>
]]></content:encoded>
			<wfw:commentRss>http://alecmce.com/as3/xml-constants-gotcha/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Weak Event Listeners and Inline Methods</title>
		<link>http://alecmce.com/as3/weak-event-listeners-and-inline-methods</link>
		<comments>http://alecmce.com/as3/weak-event-listeners-and-inline-methods#comments</comments>
		<pubDate>Fri, 07 Aug 2009 17:04:51 +0000</pubDate>
		<dc:creator>alecmce</dc:creator>
				<category><![CDATA[as3]]></category>
		<category><![CDATA[gotcha]]></category>
		<category><![CDATA[event listeners]]></category>
		<category><![CDATA[events]]></category>
		<category><![CDATA[weak reference]]></category>

		<guid isPermaLink="false">http://alecmce.com/?p=198</guid>
		<description><![CDATA[I encountered this strange issue today. The useful bulk loader library produces error events when it comes across problems and captures them with the following event listener. The accompanying comment says that this is to prevent unhandled errors that would otherwise produce a stack trace to (debug player) users indicating an error: I passed malformed [...]]]></description>
			<content:encoded><![CDATA[<p>I encountered this strange issue today. The useful <a href="http://code.google.com/p/bulk-loader/">bulk loader library</a> produces error events when it comes across problems and captures them with the following event listener. The accompanying comment says that this is to prevent unhandled errors that would otherwise produce a stack trace to (debug player) users indicating an error:</p>
<pre class="brush: as3; title: ; notranslate">addEventListener(BulkLoader.ERROR, function (e:Event):void{}, false, 1, true);</pre>
<p>I passed malformed XML into the loader, and found that about 25% of the time, I was getting just such a stack trace. I was not changing <em>anything</em>, but simply running and re-running a whole list of tests, with this test among them. The only thing that I could think of is that the Garbage Collector was occassionally considering the event listener available for collection and was removing it. Testing that hypothesis, I replaced the code with:</p>
<pre class="brush: as3; title: ; notranslate">
addEventListener(BulkLoader.ERROR, squashError, false, 1, true);
...
private function squashError(event:Event):void {}
</pre>
<p>and the error disappeared. I have <a href="http://code.google.com/p/bulk-loader/issues/detail?id=99#makechanges">posted a patch to the Bulk Library codebase</a>, so hopefully it will be resolved soon.</p>
<h3>There&#8217;s Nothing New Under The Sun</h3>
<p>I&#8217;ve known about <a href="http://gskinner.com/blog/archives/2006/07/as3_weakly_refe.html">Grant Skinner&#8217;s analysis of event listeners</a> for years, and have accepted that event listeners should always be defined weakly for as long. However, that article contains the very point that I have spent an afternoon rediscovering! It turns out I&#8217;ve been doing work that Grant Skinner did three years ago on behalf of another third party library vendor&#8230; which is disheartening because if I knew that article in more detail I could have saved my afternoon. It&#8217;s encouraging as well though; there&#8217;s a cliché about earning your learning somewhere&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://alecmce.com/as3/weak-event-listeners-and-inline-methods/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>XML Gotcha &#8211; new XML(null)</title>
		<link>http://alecmce.com/as3/xml-gotcha-new-xmlnull</link>
		<comments>http://alecmce.com/as3/xml-gotcha-new-xmlnull#comments</comments>
		<pubDate>Thu, 06 Aug 2009 17:20:52 +0000</pubDate>
		<dc:creator>alecmce</dc:creator>
				<category><![CDATA[as3]]></category>
		<category><![CDATA[gotcha]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://alecmce.com/?p=175</guid>
		<description><![CDATA[Without trying it, how would you expect this to behave? Personally I don&#8217;t think that constructing XML from a null object should work, but it does. I would have expected an error, like if I try to do this: That&#8217;s about as clear a case of error throwing as you can get, I think. Why [...]]]></description>
			<content:encoded><![CDATA[<p>Without trying it, how would you expect this to behave?</p>
<pre class="brush: as3; title: ; notranslate">var xml:XML = new XML(null);</pre>
<p>Personally I don&#8217;t think that constructing XML from a null object should work, but it does. I would have expected an error, like if I try to do this:</p>
<pre class="brush: as3; title: ; notranslate">var xml:XML = new XML(&quot;&lt;bad&gt;hello world&lt;/tags&gt;&quot;);</pre>
<p>That&#8217;s about as clear a case of error throwing as you can get, I think. Why not for null?</p>
<p>It turns out that null is interpreted as a sort of weird XML singularity. It has one node which has no name, to which you can add attributes and children. However much you add, always the length is <code>1</code> and the <code>toXMLString()</code> is an empty string. Happily, this latter fact can save you from being consumed by the singularity: you can test for it by checking whether</p>
<pre class="brush: as3; title: ; notranslate">xml.toXMLString() == &quot;&quot;;</pre>
<p>and take appropriate steps if it does!</p>
<p>I haven&#8217;t thought through all the consequences of this behaviour but it took me completely by surprise&#8230; if you can explain to me why Adobe considered this the preferred behaviour, please let me know! This is another of those hard-to-spot XML bugs like <a href="http://alecmce.com/as3/gotcha-booleans-in-xml">this one I found earlier</a>. Someone, <a href="http://blog.vizio360.co.uk/2009/08/parsing-xml-good-practice/">preferably Simone since he&#8217;s already on it</a>, should wrap fixes for this stuff into a helper class!</p>
<h3>Update</h3>
<pre class="brush: as3; title: ; notranslate">var xml:XML = new XML(null);</pre>
<p>is &#8211; of course &#8211; the same as</p>
<pre class="brush: as3; title: ; notranslate">var xml:XML = new XML();</pre>
<p>The <a href="http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/">documentation</a> doesn&#8217;t indicate that you can pass an empty value into the XML constructor, but as <a href="http://vizio360.co.uk">my colleague Simone</a> pointed out, playerglobal.swc indicates that you can. The documentation meanwhile says that the constructor parameter for an XML object should be</p>
<p>
<blockquote>Any object that can be converted to XML with the top-level XML() function.</p></blockquote>
<p>and the top-level XML function documentation states that if you pass a null into it:</p>
<p>
<blockquote>null: A runtime error occurs (TypeError exception).</p></blockquote>
<p>However, that doesn&#8217;t throw an error either!</p>
<p>I started this update having seen the playerglobal.swc definition about to eat some humble pie, but this further investigation has served to strengthen my understanding of how weird this behaviour is. Be warned, this stuff is out there waiting to spoil your afternoon&#8217;s coding!</p>
]]></content:encoded>
			<wfw:commentRss>http://alecmce.com/as3/xml-gotcha-new-xmlnull/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>ASUnit Gotcha &#8211; Empty TestSuite Bug</title>
		<link>http://alecmce.com/as3/asunit-gotcha-empty-testsuite-bug</link>
		<comments>http://alecmce.com/as3/asunit-gotcha-empty-testsuite-bug#comments</comments>
		<pubDate>Sun, 02 Aug 2009 22:21:33 +0000</pubDate>
		<dc:creator>alecmce</dc:creator>
				<category><![CDATA[as3]]></category>
		<category><![CDATA[gotcha]]></category>
		<category><![CDATA[Test Driven Development]]></category>
		<category><![CDATA[Unit Testing]]></category>

		<guid isPermaLink="false">http://alecmce.com/?p=160</guid>
		<description><![CDATA[This afternoon I discovered an interesting little bug in the ASUnit library which causes the TestRunner never to finish its suite of unit tests. It is fairly straightforward to reproduce too: simply, add create a TestSuite that doesn&#8217;t contain any tests and add the suite to the runner. Though I have created an artificial example [...]]]></description>
			<content:encoded><![CDATA[<p>This afternoon I discovered an interesting little bug in the <a href="http://asunit.org/">ASUnit library</a> which causes the <code>TestRunner</code> never to finish its suite of unit tests. It is fairly straightforward to reproduce too: simply, add create a <code>TestSuite</code> that doesn&#8217;t contain any tests and add the suite to the runner.</p>
<p>Though I have created an artificial example to isolate the problem, it arose in a real-world situation. I had two package folders in which I intended to create tests. I created all the <code>AllTest</code> <code>TestSuite</code>s but only populated one of the packages with tests. When I tried to run the <code>TestRunner</code> to see the results of these tests, though they appeared to have run the <code>TestRunner</code> had never completed.</p>
<p>The source of the problem lies in the architecture of the <code>TestSuite</code> class, the main functionality for which is launched by the <code>run</code> method but which dispatches an event to indicate that it has finished performing tests in its <code>testCompleteHandler</code> method. However, <code>testCompleteHandler</code> can only ever be triggered as a consequence of a test completing in <code>run</code>, so if no tests are defined, the <code>TestSuite</code> never finishes.</p>
<h2>Update</h2>
<p>I posted my resolution of this problem on github, but it has since been merged to the asunit trunk, so I have removed the github repository. My solution added checks for empty <code>TestCase</code>s and <code>TestSuite</code>s to prevent the <code>TestRunner</code> trying to run them.</p>
]]></content:encoded>
			<wfw:commentRss>http://alecmce.com/as3/asunit-gotcha-empty-testsuite-bug/feed</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>Booleans in XML Gotcha</title>
		<link>http://alecmce.com/as3/gotcha-booleans-in-xml</link>
		<comments>http://alecmce.com/as3/gotcha-booleans-in-xml#comments</comments>
		<pubDate>Mon, 16 Mar 2009 13:47:00 +0000</pubDate>
		<dc:creator>alecmce</dc:creator>
				<category><![CDATA[as3]]></category>
		<category><![CDATA[gotcha]]></category>

		<guid isPermaLink="false">http://flashdev.tumblr.com/post/86935183</guid>
		<description><![CDATA[The following code highlights a small but easy to miss gotcha relating to XML and Boolean types. The trace output will indicate that the flag is true. This is because when a Boolean is inputted into an XML object it is cast into the string &#8220;true&#8221; or the string &#8220;false&#8221;, but when it is parsed [...]]]></description>
			<content:encoded><![CDATA[<p>The following code highlights a small but easy to miss gotcha relating to XML and Boolean types. The trace output will indicate that the flag is true. This is because when a Boolean is inputted into an XML object it is cast into the string &#8220;true&#8221; or the string &#8220;false&#8221;, but when it is parsed out it casts the string to Boolean, returning true if the string is non-null.</p>
<pre class="brush: as3; title: ; notranslate">var flag:Boolean;
var xml:XML;

flag = false;
xml = &lt;xml value={flag} /&gt;;
flag = xml.@value;

trace(xml.toXMLString());
trace(flag);
</pre>
<p>The result is that serializing a Boolean into XML and deserializing it from XML will break. Happily, the solution is simple, to retrieve the value</p>
<pre class="brush: as3; title: ; notranslate">flag = xml.@value == &quot;true&quot;;</pre>
<p>will guarantee that the Boolean is cast correctly.</p>
]]></content:encoded>
			<wfw:commentRss>http://alecmce.com/as3/gotcha-booleans-in-xml/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

