<?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>Barklund.org &#187; AS3</title>
	<atom:link href="http://www.barklund.org/blog/category/programming/flash-platform/as3/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.barklund.org/blog</link>
	<description>work smarter when building current web trends</description>
	<lastBuildDate>Wed, 26 May 2010 09:49:20 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Why events suck in ActionScript 3</title>
		<link>http://www.barklund.org/blog/2010/05/07/events-suck-actionscript-3/</link>
		<comments>http://www.barklund.org/blog/2010/05/07/events-suck-actionscript-3/#comments</comments>
		<pubDate>Thu, 06 May 2010 22:04:45 +0000</pubDate>
		<dc:creator>Barklund</dc:creator>
				<category><![CDATA[API]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Trends]]></category>

		<guid isPermaLink="false">http://www.barklund.org/blog/?p=788</guid>
		<description><![CDATA[I&#8217;ve had it with events in ActionScript 3! They are annoying to structure, annoying to extend, annoying to dispatch but most importantly annoying to consume &#8211; and I consume events a lot more than write or dispatch my own. But! The idea of listening for stuff I really do like. I have some ideas about [...]


Related posts:<ol><li><a href='http://www.barklund.org/blog/2009/11/23/link-sharing-spam-facebook/' rel='bookmark' title='Permanent Link: Link sharing spam on Facebook'>Link sharing spam on Facebook</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<div id="attachment_804" class="wp-caption alignright" style="width: 336px"><a href="http://www.barklund.org/blog/wp-content/uploads/2010/05/ignoring_the_event.png"><img src="http://www.barklund.org/blog/wp-content/uploads/2010/05/ignoring_the_event.png" alt="" title="Ignoring the event" width="326" height="93" class="size-full wp-image-804" /></a><p class="wp-caption-text">I actually took this from production code!</p></div>
<p>I&#8217;ve had it with events in ActionScript 3! They are annoying to structure, annoying to extend, annoying to dispatch but most importantly annoying to consume &#8211; and I consume events a lot more than write or dispatch my own.</p>
<p>But! The idea of listening for stuff I really do like. I have some ideas about how this could be done a lot easier. Oh, and please stick around until the very end of this post.</p>
<p><span id="more-788"></span></p>
<h3 id="toc-background">Background</h3>
<p>For those who can&#8217;t remember exactly, events in AS3 are consumed by listening for certain events and passing a function, that will be called when this event occurs. The function will <strong>always</strong> be invoked with the event type, that you listen for &#8211; and only that argument will be given to the callback. You cannot define extra arguments in any way or not accept the event argument. Let&#8217;s for example listen for when the use clicks a button &#8211; which is the <code>MouseEvent.CLICK</code> event (of type <code>MouseEvent</code> of course):</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> <span style="color: #004993;">init</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
  <span style="color: #6699cc; font-weight: bold;">var</span> myButton<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Sprite</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Sprite</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
  ...
  myButton.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">MouseEvent</span>.<span style="color: #004993;">CLICK</span>, buttonPressed<span style="color: #000000;">&#41;</span>;
  ...
<span style="color: #000000;">&#125;</span>
<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> buttonPressed<span style="color: #000000;">&#40;</span>me<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">MouseEvent</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
  <span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;the button has been pressed, hurray&quot;</span><span style="color: #000000;">&#41;</span>;
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>We abidingly accept the <code>me:MouseEvent</code> argument even though we don&#8217;t intend to use it.</p>
<p>The event object has information about the event, about who sent it, information related to the bubbling nature of events (that very few people ever use in ActionScript 3) and additional specific attributes only relevant for specific events. For instance, if we have several buttons representing different related actions, we can examine the <code>event.target</code property to see which of the dispatcher send the event:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> selectRed<span style="color: #000000; font-weight: bold;">:</span>ColorButton;
<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> selectGreen<span style="color: #000000; font-weight: bold;">:</span>ColorButton ;
<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> selectBlue<span style="color: #000000; font-weight: bold;">:</span>ColorButton;
<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> <span style="color: #004993;">init</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
  ...
  selectRed.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">MouseEvent</span>.<span style="color: #004993;">CLICK</span>, selectColor<span style="color: #000000;">&#41;</span>;
  selectGreen.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">MouseEvent</span>.<span style="color: #004993;">CLICK</span>, selectColor<span style="color: #000000;">&#41;</span>;
  selectBlue.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">MouseEvent</span>.<span style="color: #004993;">CLICK</span>, selectColor<span style="color: #000000;">&#41;</span>;
  ...
<span style="color: #000000;">&#125;</span>
<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> selectColor<span style="color: #000000;">&#40;</span>me<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">MouseEvent</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
  <span style="color: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">color</span><span style="color: #000000; font-weight: bold;">:</span>ColorButton = me.<span style="color: #004993;">target</span> <span style="color: #0033ff; font-weight: bold;">as</span> ColorButton;
  <span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight: bold;">!</span><span style="color: #004993;">color</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
    <span style="color: #0033ff; font-weight: bold;">throw</span> <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Error</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;color selected was not actually a color&quot;</span><span style="color: #000000;">&#41;</span>;
  <span style="color: #000000;">&#125;</span>
  <span style="color: #0033ff; font-weight: bold;">switch</span> <span style="color: #000000;">&#40;</span><span style="color: #004993;">color</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
    <span style="color: #0033ff; font-weight: bold;">case</span> selectRed<span style="color: #000000; font-weight: bold;">:</span> <span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;you chose a red Papa Smurf&quot;</span><span style="color: #000000;">&#41;</span>; <span style="color: #0033ff; font-weight: bold;">break</span>;
    <span style="color: #0033ff; font-weight: bold;">case</span> selectGreen<span style="color: #000000; font-weight: bold;">:</span> <span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;you chose a green Papa Smurf&quot;</span><span style="color: #000000;">&#41;</span>; <span style="color: #0033ff; font-weight: bold;">break</span>;
    <span style="color: #0033ff; font-weight: bold;">case</span> selectBlue<span style="color: #000000; font-weight: bold;">:</span> <span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;you chose a blue Papa Smurf&quot;</span><span style="color: #000000;">&#41;</span>; <span style="color: #0033ff; font-weight: bold;">break</span>;
  <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Here, we do use the <code>MouseEvent</code>-object, but we cast the target property to the type we expect it to have and then we need to check, if the cast went well. Because, technically we could have used the same function as a listener for something else somewhere else (even though we know we didn't) and thus <code>target</code> could have been something entirely different.</p>
<h3 id="toc-personal-experiences">Personal experiences</h3>
<p>I've just checked a recent, larger project with 185 (home-made) classes. In general I try to minimize my event usage, but a lot of things I have to use events for event though I don't really need them, so I ran a simple script counting how many times I had a function that took an event as an argument, and how many of these actually used the event object. The result was 121 functions accepting an event as argument (10 of those had default null values), and in only 4 of those I actually used the event argument (one of them a <code>KeyboardEvent</code>, the others were all the same home-made <code>PageEvent</code>). 83 of the events accepted were <code>MouseEvent</code>s (which is no surprise), and never was the <code>me:MouseEvent</code> variable used in the function body.</p>
<p>Then, I tested an even earlier project from before I started minimizing event usage (almost maximized it back then). It consisted of 50 (home-made) classes. It had 113 functions (a lot more per class than the above) accepting event arguments (16 of those defaulting to null) and 74 of those were <code>MouseEvent</code>s. And this time around, I used the event argument 25 times, but 18 of these usages were accessing <code>event.target</code> or <code>event.data</code> and casting it to the right class. Thus, yes I did use the event argument in some cases (22% of the time I accepted an argument), but in most of them I still had to do manual casting of the <code>event.target</code> (or <code>event.data</code>) attribute to get the type I knew I was accessing. This leaves only 7 "true" usages or about 6%. Please see the background section about why using the event object for casting only just moves the problem around and makes the code even more verbose.</p>
<h3 id="toc-dream-scenario">Dream scenario</h3>
<p>So, what would I like instead? Something intelligent, that allows me to accept the arguments I need <em>when</em> I need them. For instance, take this snippet here:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">close</span><span style="color: #000000; font-weight: bold;">:</span>CloseButton;
<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> <span style="color: #004993;">init</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
  <span style="color: #004993;">close</span>.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">MouseEvent</span>.<span style="color: #004993;">CLICK</span>, closeDialog<span style="color: #000000;">&#41;</span>;
<span style="color: #000000;">&#125;</span>
<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> closeDialog<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
  someDialog.fadeToOblivion<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>It is simple, I do not need any argument, so I do not expect one.</p>
<p>A often used situation is when you have several instances of the same type and attach the same listener to all of these, then you use the event argument's target attribute for switching on which instance was invoked and handling the result correspondingly:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> downloadLowRes<span style="color: #000000; font-weight: bold;">:</span>DownloadButton;
<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> downloadMediumRes<span style="color: #000000; font-weight: bold;">:</span>DownloadButton;
<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> downloadHighRes<span style="color: #000000; font-weight: bold;">:</span>DownloadButton;
<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> <span style="color: #004993;">init</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
  downloadLowRes.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">MouseEvent</span>.<span style="color: #004993;">CLICK</span>, <span style="color: #004993;">download</span><span style="color: #000000;">&#41;</span>;
  downloadMediumRes.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">MouseEvent</span>.<span style="color: #004993;">CLICK</span>, <span style="color: #004993;">download</span><span style="color: #000000;">&#41;</span>;
  downloadHighRes.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">MouseEvent</span>.<span style="color: #004993;">CLICK</span>, <span style="color: #004993;">download</span><span style="color: #000000;">&#41;</span>;
<span style="color: #000000;">&#125;</span>
<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> <span style="color: #004993;">download</span><span style="color: #000000;">&#40;</span>db<span style="color: #000000; font-weight: bold;">:</span>DownloadButton<span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
  <span style="color: #0033ff; font-weight: bold;">switch</span> <span style="color: #000000;">&#40;</span>db<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
    <span style="color: #0033ff; font-weight: bold;">case</span> downloadLowRes<span style="color: #000000; font-weight: bold;">:</span> <span style="color: #004993;">open</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;file_360p.avi&quot;</span><span style="color: #000000;">&#41;</span>; <span style="color: #0033ff; font-weight: bold;">break</span>;
    <span style="color: #0033ff; font-weight: bold;">case</span> downloadMediumRes<span style="color: #000000; font-weight: bold;">:</span> <span style="color: #004993;">open</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;file_720p.avi&quot;</span><span style="color: #000000;">&#41;</span>; <span style="color: #0033ff; font-weight: bold;">break</span>;
    <span style="color: #0033ff; font-weight: bold;">case</span> downloadHighRes<span style="color: #000000; font-weight: bold;">:</span> <span style="color: #004993;">open</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;file_1080p.avi&quot;</span><span style="color: #000000;">&#41;</span>; <span style="color: #0033ff; font-weight: bold;">break</span>;
  <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>The function expects a reference to the button clicked - not to an event, who references the button clicked, because only the actual button reference is necessary.</p>
<p>Finally, a more elaborate example of different other component listeners expecting just what they need and nothing else:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> <span style="color: #004993;">init</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
  <span style="color: #009900;">// listen for selections in a dropdown</span>
  countryDropdown.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">Event</span>.<span style="color: #004993;">SELECT</span>, selectCountry<span style="color: #000000;">&#41;</span>;
&nbsp;
  <span style="color: #009900;">// listen for checkbox changes</span>
  acceptTermsCheckbox.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">Event</span>.<span style="color: #004993;">CHANGE</span>, acceptTerms<span style="color: #000000;">&#41;</span>;
&nbsp;
  <span style="color: #009900;">// listen for focus changes between password input fields</span>
  passwordInput.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">FocusEvent</span>.<span style="color: #004993;">FOCUS_OUT</span>, checkPasswords<span style="color: #000000;">&#41;</span>;
  passwordConfirmInput.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">FocusEvent</span>.<span style="color: #004993;">FOCUS_OUT</span>, checkPasswords<span style="color: #000000;">&#41;</span>;
&nbsp;
  <span style="color: #009900;">// listen for three different radio buttons changing</span>
  languageDanish.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">Event</span>.<span style="color: #004993;">SELECT</span>, selectLanguage<span style="color: #000000;">&#41;</span>;
  languageSwedish.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">Event</span>.<span style="color: #004993;">SELECT</span>, selectLanguage<span style="color: #000000;">&#41;</span>;
  languageNorwegian.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">Event</span>.<span style="color: #004993;">SELECT</span>, selectLanguage<span style="color: #000000;">&#41;</span>;
&nbsp;
  <span style="color: #009900;">// listen for submit button presses</span>
  submitButton.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">MouseEvent</span>.<span style="color: #004993;">CLICK</span>, submit<span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span>
<span style="color: #000000;">&#125;</span>
<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> selectCountry<span style="color: #000000;">&#40;</span>country<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
  selectedCountry = country;
<span style="color: #000000;">&#125;</span>
<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> acceptTerms<span style="color: #000000;">&#40;</span>accepted<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Boolean</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
  termsAccepted = accepted;
<span style="color: #000000;">&#125;</span>
<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> checkPassword<span style="color: #000000;">&#40;</span>fe<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">FocusEvent</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
  <span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight: bold;">!</span><span style="color: #000000;">&#40;</span>fe.<span style="color: #004993;">relatedObject</span> <span style="color: #0033ff; font-weight: bold;">in</span> <span style="color: #000000;">&#91;</span>passwordInput, passwordConfirmInput<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
    <span style="color: #009900;">// focus moved away from either input field</span>
    <span style="color: #009900;">// show mismatch alert if texts are not the same</span>
    passwordMismatchAlert.<span style="color: #004993;">visible</span> = passwordInput.<span style="color: #004993;">text</span> <span style="color: #000000; font-weight: bold;">!</span>== passwordConfirmInput.<span style="color: #004993;">text</span>;
  <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span>
<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> selectLanguage<span style="color: #000000;">&#40;</span><span style="color: #004993;">language</span><span style="color: #000000; font-weight: bold;">:</span>LanguageRadioButton<span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
  <span style="color: #0033ff; font-weight: bold;">switch</span> <span style="color: #000000;">&#40;</span><span style="color: #004993;">language</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
    <span style="color: #0033ff; font-weight: bold;">case</span> languageDanish<span style="color: #000000; font-weight: bold;">:</span> <span style="color: #004993;">load</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;texts_da.xml&quot;</span><span style="color: #000000;">&#41;</span>; <span style="color: #0033ff; font-weight: bold;">break</span>;
    <span style="color: #0033ff; font-weight: bold;">case</span> languageSwedish<span style="color: #000000; font-weight: bold;">:</span> <span style="color: #004993;">load</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;texts_se.xml&quot;</span><span style="color: #000000;">&#41;</span>; <span style="color: #0033ff; font-weight: bold;">break</span>;
    <span style="color: #0033ff; font-weight: bold;">case</span> languageNorwegian<span style="color: #000000; font-weight: bold;">:</span> <span style="color: #004993;">load</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;texts_no.xml&quot;</span><span style="color: #000000;">&#41;</span>; <span style="color: #0033ff; font-weight: bold;">break</span>;
  <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span>
<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> submit<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
  sendForm<span style="color: #000000;">&#40;</span>withParameters<span style="color: #000000;">&#41;</span>;
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>One notable example among the above is, that if an event is needed, you can always have it. I only dislike events, when I don't need them but have to accept them anyway. When I actually need it, they do come quite handy - like the <code>FocusEvent.relatedObject</code> used in the above example.<br />
<h3 id="toc-announcement">Announcement</h3>
<p>Actually, I have made the above work! It is a dirty, dirty triple-hack, but it feels quite sweet to use on both ends. I need to clean it up a lot, need to come up with a good name for it and need to benchmark it to see, that it doesn't kill performance (too much).</p>
<p>So please, stay tuned. But do not fear commenting on the above ideas anyway.</p>


<p>Related posts:<ol><li><a href='http://www.barklund.org/blog/2009/11/23/link-sharing-spam-facebook/' rel='bookmark' title='Permanent Link: Link sharing spam on Facebook'>Link sharing spam on Facebook</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.barklund.org/blog/2010/05/07/events-suck-actionscript-3/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Advanced E4X &#8211; Assignment to XMLLists</title>
		<link>http://www.barklund.org/blog/2009/11/04/advanced-e4x-assignment-xmllists/</link>
		<comments>http://www.barklund.org/blog/2009/11/04/advanced-e4x-assignment-xmllists/#comments</comments>
		<pubDate>Wed, 04 Nov 2009 10:22:47 +0000</pubDate>
		<dc:creator>Barklund</dc:creator>
				<category><![CDATA[AS3]]></category>
		<category><![CDATA[E4X]]></category>

		<guid isPermaLink="false">http://www.barklund.org/blog/?p=422</guid>
		<description><![CDATA[Setup Direct children Descendants Conclusion I was playing around with E4X last night working on an upcoming blog post about the capabilities of E4X. While playing, I was thoroughly reading the ECMA-357 standard, and found that there are some special rules about assignment and compound assignment, that produce truely unexpected results. I will try to [...]


No related posts.]]></description>
			<content:encoded><![CDATA[<div class="toc">
<ol>
<li><a href="http://www.barklund.org/blog/2009/11/04/advanced-e4x-assignment-xmllists/#toc-setup">Setup</a></li>
<li><a href="http://www.barklund.org/blog/2009/11/04/advanced-e4x-assignment-xmllists/#toc-direct-children">Direct children</a></li>
<li><a href="http://www.barklund.org/blog/2009/11/04/advanced-e4x-assignment-xmllists/#toc-descendants">Descendants</a></li>
<li><a href="http://www.barklund.org/blog/2009/11/04/advanced-e4x-assignment-xmllists/#toc-conclusion">Conclusion</a></li>
</ol>
</div>
<p>I was playing around with E4X last night working on an upcoming blog post about the capabilities of E4X. While playing, I was thoroughly reading the ECMA-357 standard, and found that there are some special rules about assignment and compound assignment, that produce truely unexpected results.</p>
<p>I will try to summarize common pitfalls and provide valid work-arounds.</p>
<p><span id="more-422"></span></p>
<h3 id="toc-setup">Setup</h3>
<p>This post will for most examples work on the following document:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #6699cc; font-weight: bold;">var</span> e<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">XML</span> = <span style="color: #000000; font-weight: bold;">&lt;</span>employees<span style="color: #000000; font-weight: bold;">&gt;</span>
	<span style="color: #000000; font-weight: bold;">&lt;</span>person<span style="color: #000000; font-weight: bold;">&gt;</span>
		<span style="color: #000000; font-weight: bold;">&lt;</span>name<span style="color: #000000; font-weight: bold;">&gt;</span>John<span style="color: #000000; font-weight: bold;">&lt;/</span>name<span style="color: #000000; font-weight: bold;">&gt;</span>
		<span style="color: #000000; font-weight: bold;">&lt;</span>age<span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight:bold;">35</span><span style="color: #000000; font-weight: bold;">&lt;/</span>age<span style="color: #000000; font-weight: bold;">&gt;</span>
	<span style="color: #000000; font-weight: bold;">&lt;/</span>person<span style="color: #000000; font-weight: bold;">&gt;</span>
	<span style="color: #000000; font-weight: bold;">&lt;</span>person<span style="color: #000000; font-weight: bold;">&gt;</span>
		<span style="color: #000000; font-weight: bold;">&lt;</span>name<span style="color: #000000; font-weight: bold;">&gt;</span>Joe<span style="color: #000000; font-weight: bold;">&lt;/</span>name<span style="color: #000000; font-weight: bold;">&gt;</span>
		<span style="color: #000000; font-weight: bold;">&lt;</span>age<span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight:bold;">25</span><span style="color: #000000; font-weight: bold;">&lt;/</span>age<span style="color: #000000; font-weight: bold;">&gt;</span>
		<span style="color: #000000; font-weight: bold;">&lt;</span>hobby<span style="color: #000000; font-weight: bold;">&gt;</span>Swimming<span style="color: #000000; font-weight: bold;">&lt;/</span>hobby<span style="color: #000000; font-weight: bold;">&gt;</span>
	<span style="color: #000000; font-weight: bold;">&lt;/</span>person<span style="color: #000000; font-weight: bold;">&gt;</span>
<span style="color: #000000; font-weight: bold;">&lt;/</span>employees<span style="color: #000000; font-weight: bold;">&gt;</span></pre></div></div>

<p>Sometimes other documents will be used explicitly.</p>
<h3 id="toc-direct-children">Direct children</h3>
<p>How would you go about changing the hobby of Joe to running? Well, we could traverse all the way to the node:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;">e.person<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#93;</span>.hobby = <span style="color: #990000;">&quot;Running&quot;</span>;</pre></div></div>

<p>This of course works as expected. Actually, we ought to be able to skip the index [1], as only the second person has a hobby and thus only this node ought to be the target for the update:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;">e.person.hobby = <span style="color: #990000;">&quot;Running&quot;</span>;</pre></div></div>

<p>But no, this throws a runtime-error:</p>
<blockquote><p>TypeError: Error #1089: Assignment to lists with more than one item is not supported.</p></blockquote>
<p>But it is not a list with more than one item, is it?</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span>e.person.hobby.<span style="color: #004993;">length</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>; <span style="color: #009900;">// traces 1</span></pre></div></div>

<p>The issue here (I expect, I had a hard time figuring out what the spec actually said about it) is, that the second-to-last list in the assignment target has more than one item:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span>e.person.<span style="color: #004993;">length</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>; <span style="color: #009900;">// traces 2</span></pre></div></div>

<p>This can be tested by introducing some extra level of nodes and try to update one of these:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #6699cc; font-weight: bold;">var</span> more_nodes<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">XML</span> = <span style="color: #000000; font-weight: bold;">&lt;</span>employees<span style="color: #000000; font-weight: bold;">&gt;</span>
	<span style="color: #000000; font-weight: bold;">&lt;</span>person<span style="color: #000000; font-weight: bold;">&gt;</span>
		<span style="color: #000000; font-weight: bold;">&lt;</span>name<span style="color: #000000; font-weight: bold;">&gt;</span>John<span style="color: #000000; font-weight: bold;">&lt;/</span>name<span style="color: #000000; font-weight: bold;">&gt;</span>
		<span style="color: #000000; font-weight: bold;">&lt;</span>age<span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight:bold;">35</span><span style="color: #000000; font-weight: bold;">&lt;/</span>age<span style="color: #000000; font-weight: bold;">&gt;</span>
	<span style="color: #000000; font-weight: bold;">&lt;/</span>person<span style="color: #000000; font-weight: bold;">&gt;</span>
	<span style="color: #000000; font-weight: bold;">&lt;</span>person<span style="color: #000000; font-weight: bold;">&gt;</span>
		<span style="color: #000000; font-weight: bold;">&lt;</span>name<span style="color: #000000; font-weight: bold;">&gt;</span>Joe<span style="color: #000000; font-weight: bold;">&lt;/</span>name<span style="color: #000000; font-weight: bold;">&gt;</span>
		<span style="color: #000000; font-weight: bold;">&lt;</span>age<span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight:bold;">25</span><span style="color: #000000; font-weight: bold;">&lt;/</span>age<span style="color: #000000; font-weight: bold;">&gt;</span>
		<span style="color: #000000; font-weight: bold;">&lt;</span>hobby<span style="color: #000000; font-weight: bold;">&gt;</span>
			<span style="color: #000000; font-weight: bold;">&lt;</span>primary<span style="color: #000000; font-weight: bold;">&gt;</span>Swimming<span style="color: #000000; font-weight: bold;">&lt;/</span>primary<span style="color: #000000; font-weight: bold;">&gt;</span>
			<span style="color: #000000; font-weight: bold;">&lt;</span>secondary<span style="color: #000000; font-weight: bold;">&gt;</span>Sewing<span style="color: #000000; font-weight: bold;">&lt;/</span>secondary<span style="color: #000000; font-weight: bold;">&gt;</span>
		<span style="color: #000000; font-weight: bold;">&lt;/</span>hobby<span style="color: #000000; font-weight: bold;">&gt;</span>
	<span style="color: #000000; font-weight: bold;">&lt;/</span>person<span style="color: #000000; font-weight: bold;">&gt;</span>
<span style="color: #000000; font-weight: bold;">&lt;/</span>employees<span style="color: #000000; font-weight: bold;">&gt;</span>
<span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span>more_nodes.person.<span style="color: #004993;">length</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>; <span style="color: #009900;">// traces 2</span>
<span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span>more_nodes.person.hobby.<span style="color: #004993;">length</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>; <span style="color: #009900;">// traces 1</span>
<span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span>more_nodes.person.hobby.primary.<span style="color: #004993;">length</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>; <span style="color: #009900;">// traces 1</span>
more_nodes.person.hobby.primary = <span style="color: #990000;">&quot;Running&quot;</span>; <span style="color: #009900;">// no error, updated as expected</span></pre></div></div>

<p>Voila, it works. What this means is, that the error is not related to the actual XMLList, that you are assigning to &#8211; but to the XMLList, that is the parent of this in the statement. We could actually have reached the desired result by adding an extra level to reach the text node inside the (single) hobby node:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;">e.person.hobby.<span style="color: #000000; font-weight: bold;">*</span> = <span style="color: #990000;">&quot;Running&quot;</span>; <span style="color: #009900;">// no error, updated as expected</span></pre></div></div>

<p>In this case again, the second-to-last XMLList (the target object for the assignment) has a length of 1, and the update can be accomplished.</p>
<h3 id="toc-descendants">Descendants</h3>
<p>But if we really just wanted to update the single hobby, that we know only one of the employees have, then we would normally try:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;">e..hobby = <span style="color: #990000;">&quot;Running&quot;</span>;</pre></div></div>

<p>Now things start to get really interesting. Because, what happened? No error was thrown, but the document after this update looks like:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;</span>employees<span style="color: #000000; font-weight: bold;">&gt;</span>
  <span style="color: #000000; font-weight: bold;">&lt;</span>person<span style="color: #000000; font-weight: bold;">&gt;</span>
    <span style="color: #000000; font-weight: bold;">&lt;</span>name<span style="color: #000000; font-weight: bold;">&gt;</span>John<span style="color: #000000; font-weight: bold;">&lt;/</span>name<span style="color: #000000; font-weight: bold;">&gt;</span>
    <span style="color: #000000; font-weight: bold;">&lt;</span>age<span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight:bold;">35</span><span style="color: #000000; font-weight: bold;">&lt;/</span>age<span style="color: #000000; font-weight: bold;">&gt;</span>
  <span style="color: #000000; font-weight: bold;">&lt;/</span>person<span style="color: #000000; font-weight: bold;">&gt;</span>
  <span style="color: #000000; font-weight: bold;">&lt;</span>person<span style="color: #000000; font-weight: bold;">&gt;</span>
    <span style="color: #000000; font-weight: bold;">&lt;</span>name<span style="color: #000000; font-weight: bold;">&gt;</span>Joe<span style="color: #000000; font-weight: bold;">&lt;/</span>name<span style="color: #000000; font-weight: bold;">&gt;</span>
    <span style="color: #000000; font-weight: bold;">&lt;</span>age<span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight:bold;">25</span><span style="color: #000000; font-weight: bold;">&lt;/</span>age<span style="color: #000000; font-weight: bold;">&gt;</span>
    <span style="color: #000000; font-weight: bold;">&lt;</span>hobby<span style="color: #000000; font-weight: bold;">&gt;</span>Swimming<span style="color: #000000; font-weight: bold;">&lt;/</span>hobby<span style="color: #000000; font-weight: bold;">&gt;</span>
  <span style="color: #000000; font-weight: bold;">&lt;/</span>person<span style="color: #000000; font-weight: bold;">&gt;</span>
  <span style="color: #000000; font-weight: bold;">&lt;</span>hobby<span style="color: #000000; font-weight: bold;">&gt;</span>Running<span style="color: #000000; font-weight: bold;">&lt;/</span>hobby<span style="color: #000000; font-weight: bold;">&gt;</span>
<span style="color: #000000; font-weight: bold;">&lt;/</span>employees<span style="color: #000000; font-weight: bold;">&gt;</span></pre></div></div>

<p>That is, the existing hobby was unchanged, but a new hobby node has been appended to the end of the outermost node? The statement works the same, as if I had just written:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;">e.hobby = <span style="color: #990000;">&quot;Running&quot;</span>;</pre></div></div>

<p>Which would do exactly that &#8211; create and append a new hobby node to the outermost node and set its text value to &#8220;Running&#8221;. But the two lists aren&#8217;t identical in the original document:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span>e..hobby.<span style="color: #004993;">length</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>; <span style="color: #009900;">// traces 1</span>
<span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span>e.hobby.<span style="color: #004993;">length</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>; <span style="color: #009900;">// traces 0</span></pre></div></div>

<p>But again, assignment to xml (and xmllists) does not really have to do with the last part of the assignment target but the penultimate. And again, adding an extra level to reach the text node(s) works:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;">e..hobby.<span style="color: #000000; font-weight: bold;">*</span> = <span style="color: #990000;">&quot;Running&quot;</span>; <span style="color: #009900;">// no error, and this time updated as expected</span></pre></div></div>

<p>Why the previous statement seemed to ignore the descendant-part of the statement, I&#8217;m not really sure. Could be a bug, could be a loop-hole, but it definitely does not make sense.</p>
<p>There are many other ways to reach the same list of the single hobby node when simply consuming, but they cannot all be used in the same way when updating:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;">e..<span style="color: #000000; font-weight: bold;">*</span>.hobby = <span style="color: #990000;">&quot;Running&quot;</span>; <span style="color: #009900;">// throws the same Error #1089 about more-than-one element lists</span>
e..<span style="color: #000000; font-weight: bold;">*</span>.hobby.<span style="color: #000000; font-weight: bold;">*</span> = <span style="color: #990000;">&quot;Running&quot;</span>; <span style="color: #009900;">// and it works again</span>
&nbsp;
e.<span style="color: #004993;">descendants</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.hobby = <span style="color: #990000;">&quot;Running&quot;</span>; <span style="color: #009900;">// throws the same Error #1089 about more-than-one element lists</span>
e.<span style="color: #004993;">descendants</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.hobby.<span style="color: #000000; font-weight: bold;">*</span> = <span style="color: #990000;">&quot;Running&quot;</span>; <span style="color: #009900;">// and it works again</span>
&nbsp;
e.<span style="color: #004993;">descendants</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">'hobby'</span><span style="color: #000000;">&#41;</span> = <span style="color: #990000;">&quot;Running&quot;</span>; <span style="color: #009900;">// compile-time error</span>
<span style="color: #009900;">// the parser simply does not allow assigning to a function invocation</span>
e.<span style="color: #004993;">descendants</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">'hobby'</span><span style="color: #000000;">&#41;</span>.<span style="color: #000000; font-weight: bold;">*</span> = <span style="color: #990000;">&quot;Running&quot;</span>; <span style="color: #009900;">// and it works again</span>
&nbsp;
e..<span style="color: #000000; font-weight: bold;">*</span>.<span style="color: #000000;">&#40;</span><span style="color: #004993;">localName</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>==<span style="color: #990000;">'hobby'</span><span style="color: #000000;">&#41;</span> = <span style="color: #990000;">&quot;Running&quot;</span>; <span style="color: #009900;">// compile-time error</span>
<span style="color: #009900;">// the parser simply does not allow assigning to (what looks like) a function invocation</span>
e..<span style="color: #000000; font-weight: bold;">*</span>.<span style="color: #000000;">&#40;</span><span style="color: #004993;">localName</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>==<span style="color: #990000;">'hobby'</span><span style="color: #000000;">&#41;</span>.<span style="color: #000000; font-weight: bold;">*</span> = <span style="color: #990000;">&quot;Running&quot;</span>; <span style="color: #009900;">// and it works again</span></pre></div></div>

<h3 id="toc-conclusion">Conclusion</h3>
<p>Be aware that assignments to XMLLists does not care about the last list &#8211; it is the second-to-last list, that must contain only one value. Internally, a statement like:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;">expression.property = <span style="color: #004993;">value</span>;</pre></div></div>

<p>Is not a new thing at all, but it is handled in a new, special way, when <code>expression</code> has a value of either XML or XMLList. Thus, if you either get compile-time errors, runtime-errors or nodes appended in completely wrong places, just add extra level of element traversal with the wildcard-identifier <code>.*</code> to reach the text node of the single node you want to update.</p>


<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://www.barklund.org/blog/2009/11/04/advanced-e4x-assignment-xmllists/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MXHR4AS3 Released</title>
		<link>http://www.barklund.org/blog/2009/10/09/mxhr4as3-released/</link>
		<comments>http://www.barklund.org/blog/2009/10/09/mxhr4as3-released/#comments</comments>
		<pubDate>Fri, 09 Oct 2009 09:19:42 +0000</pubDate>
		<dc:creator>Barklund</dc:creator>
				<category><![CDATA[AS3]]></category>
		<category><![CDATA[MXHR4AS3]]></category>

		<guid isPermaLink="false">http://www.barklund.org/blog/?p=376</guid>
		<description><![CDATA[I finally got the time to clean up the code for MXHR4AS3 &#8211; an implementation of the Digg-introduced concept of requesting several files from the server in one request and returning it as a multipart/mixed http response to reduce overhead. The original was implemented in JS, and I have further re-implemented it in AS3. As [...]


Related posts:<ol><li><a href='http://www.barklund.org/blog/2009/09/04/mxhr4as3-multipart-mixed-file-download-flash/' rel='bookmark' title='Permanent Link: Announcing MXHR4AS3: Multipart/mixed-file download by Flash clients'>Announcing MXHR4AS3: Multipart/mixed-file download by Flash clients</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>I finally got the time to clean up the code for <a href="http://www.barklund.org/blog/2009/09/04/mxhr4as3-multipart-mixed-file-download-flash/" title="Announcing MXHR4AS3: Multipart/mixed-file download by Flash clients from Barklund.org">MXHR4AS3</a> &#8211; an implementation of the <a href="http://blog.digg.com/?p=621" title="DUI.Stream and MXHR from the official Digg blog">Digg-introduced concept</a> of requesting several files from the server in one request and returning it as a multipart/mixed http response to reduce overhead. The original was implemented in JS, and I have further re-implemented it in AS3.</p>
<p>As promised, the code <a href="http://bitbucket.org/barklund/mxhr4as3/">has been uploaded</a> to <a href="http://bitbucket.org/" title="Bitbucket home">Bitbucket</a>. Please enjoy and by all means request access if necessary. Please refer to the <a href="http://barklund.org/blog/projects/mxhr4as3">new project page here</a> for details.</p>


<p>Related posts:<ol><li><a href='http://www.barklund.org/blog/2009/09/04/mxhr4as3-multipart-mixed-file-download-flash/' rel='bookmark' title='Permanent Link: Announcing MXHR4AS3: Multipart/mixed-file download by Flash clients'>Announcing MXHR4AS3: Multipart/mixed-file download by Flash clients</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.barklund.org/blog/2009/10/09/mxhr4as3-released/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Announcing MXHR4AS3: Multipart/mixed-file download by Flash clients</title>
		<link>http://www.barklund.org/blog/2009/09/04/mxhr4as3-multipart-mixed-file-download-flash/</link>
		<comments>http://www.barklund.org/blog/2009/09/04/mxhr4as3-multipart-mixed-file-download-flash/#comments</comments>
		<pubDate>Fri, 04 Sep 2009 11:32:38 +0000</pubDate>
		<dc:creator>Barklund</dc:creator>
				<category><![CDATA[API]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[Trends]]></category>

		<guid isPermaLink="false">http://www.barklund.org/blog/?p=336</guid>
		<description><![CDATA[After having read about and experimented with Digg&#8217;s MXHR concept and DUI.Stream, I have now implemented the very same thing for ActionScript 3. In my tests it really shows a performance-boost over conventional queued download. Introduction Results Source Introduction I have implemented the MXHR client to be able to use multiple concurrent http-streams to load [...]


Related posts:<ol><li><a href='http://www.barklund.org/blog/2009/10/09/mxhr4as3-released/' rel='bookmark' title='Permanent Link: MXHR4AS3 Released'>MXHR4AS3 Released</a></li>
<li><a href='http://www.barklund.org/blog/2009/07/06/flash-development-scripted-layout/' rel='bookmark' title='Permanent Link: Flash development and the practice of Scripted Layout'>Flash development and the practice of Scripted Layout</a></li>
<li><a href='http://www.barklund.org/blog/2009/06/08/using-addthis-with-flash/' rel='bookmark' title='Permanent Link: Using AddThis with Flash'>Using AddThis with Flash</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>After having read about and experimented with <a href="http://blog.digg.com/?p=621" title="DUI.Stream and MXHR from the Digg Blog">Digg&#8217;s MXHR concept</a> and <a href="http://github.com/digg/stream" title="DUI.Stream repository on Github">DUI.Stream</a>, I have now implemented the very same thing for ActionScript 3. In my tests it really shows a performance-boost over conventional queued download.</p>
<div class="toc">
<ol>
<li><a href="http://www.barklund.org/blog/2009/09/04/mxhr4as3-multipart-mixed-file-download-flash/#toc-introduction">Introduction</a></li>
<li><a href="http://www.barklund.org/blog/2009/09/04/mxhr4as3-multipart-mixed-file-download-flash/#toc-results">Results</a></li>
<li><a href="http://www.barklund.org/blog/2009/09/04/mxhr4as3-multipart-mixed-file-download-flash/#toc-source">Source</a></li>
</ol>
</div>
<p><span id="more-336"></span></p>
<h3 id="toc-introduction">Introduction</h3>
<p>I have implemented the MXHR client to be able to use multiple concurrent http-streams to load the files even faster. This is done in order to make sure that the MXHR client can have the same advantage as conventional downloading, where concurrent streams perform better than simple queued streams.</p>
<p>The MXHR client is implemented pretty straight-forward in AS3 with 3 simple classes &ndash; <code>MXHR</code>, <code>MXHRFile</code> and <code>MXHREvent</code>. The serverside implementation, that I choose, was in PHP, and since the folks at Digg did not provide one such sample implementation, I did my own. It is done in PHP 5 through a single class <code>MXHR</code> taking file names and building a aggregated response (in conformance with specifications of course) &#8211; and it supports caching.</p>
<h3 id="toc-results">Results</h3>
<p>Some example download times by my tests:</p>
<table border="1">
<caption><em>Download times for different clients</em></caption>
<col style="width:100px" />
<col style="width:100px" />
<col style="width:150px" />
<col style="width:150px" />
<thead>
<tr>
<th>Method</th>
<th>Streams</th>
<th>Uncached load time</th>
<th>Cached load time</th>
</tr>
</thead>
<tbody>
<tr>
<td rowspan="2">Conventional</td>
<td>1</td>
<td>37094 ms</td>
<td>15854 ms</td>
</tr>
<tr>
<td>8</td>
<td>5319 ms</td>
<td>3126 ms</td>
</tr>
<tr>
<td rowspan="2">MXHR4AS3</td>
<td>1</td>
<td>4386 ms</td>
<td>1473 ms</td>
</tr>
<tr>
<td>8</td>
<td>3695 ms</td>
<td>1421 ms</td>
</tr>
</tbody>
</table>
<p>Try it out for yourself here:</p>
<ul>
<li><a href="/examples/mxhr4as3/conventional_single.swf" title="Link to conventional single-threaded download">Conventional single-threaded download</a></li>
<li><a href="/examples/mxhr4as3/conventional_concurrent.swf" title="Link to conventional multi-threaded download">Conventional multi-threaded download</a></li>
<li><a href="/examples/mxhr4as3/demo_single.swf" title="Link to MXHR-based single-threaded download">MXHR-based single-threaded download</a></li>
<li><a href="/examples/mxhr4as3/demo_concurrent.swf" title="Link to MXHR-based multi-threaded download">MXHR-based multi-threaded download</a></li>
</ul>
<p>As you can see, when the files are cached there is almost no difference between MXHR-based single- or multi-threaded download. And both are significantly faster (both cached and uncached) compared to either type of conventional download.</p>
<h3 id="toc-source">Source</h3>
<p>I will need to do a little bit of further testing and clean-up before I put the full source-code up for download, but when I do, it will be on <s><a href="http://code.google.com" title="Google Code">Google Code</a></s> <a href="http://bitbucket.org" title="Home of Bitbucket.org">Bitbucket</a> under the <a href="/blog/site-license" title="Site License for barklund.org content">site license</a>.</p>


<p>Related posts:<ol><li><a href='http://www.barklund.org/blog/2009/10/09/mxhr4as3-released/' rel='bookmark' title='Permanent Link: MXHR4AS3 Released'>MXHR4AS3 Released</a></li>
<li><a href='http://www.barklund.org/blog/2009/07/06/flash-development-scripted-layout/' rel='bookmark' title='Permanent Link: Flash development and the practice of Scripted Layout'>Flash development and the practice of Scripted Layout</a></li>
<li><a href='http://www.barklund.org/blog/2009/06/08/using-addthis-with-flash/' rel='bookmark' title='Permanent Link: Using AddThis with Flash'>Using AddThis with Flash</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.barklund.org/blog/2009/09/04/mxhr4as3-multipart-mixed-file-download-flash/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Top 6 AS3 features you miss when switching back to AS2</title>
		<link>http://www.barklund.org/blog/2009/07/19/top-6-as3-features-you-miss-when-switching-back-to-as2/</link>
		<comments>http://www.barklund.org/blog/2009/07/19/top-6-as3-features-you-miss-when-switching-back-to-as2/#comments</comments>
		<pubDate>Sun, 19 Jul 2009 21:40:58 +0000</pubDate>
		<dc:creator>Barklund</dc:creator>
				<category><![CDATA[AS2]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[Lists]]></category>

		<guid isPermaLink="false">http://www.barklund.org/blog/?p=234</guid>
		<description><![CDATA[I have been doing ActionScript 3 projects for quite a while, but every now and then I (as well as many others in the trade) have to switch back to AS2 or even AS1 &#8211; the line between them is quite blurry really in retrospective. When doing AS3 projects, there are many features that really [...]


Related posts:<ol><li><a href='http://www.barklund.org/blog/2009/07/01/minor-change-in-handling-nan-positions-in-flash-player-10-0-22/' rel='bookmark' title='Permanent Link: Minor change in handling NaN positions in Flash Player 10,0,22'>Minor change in handling NaN positions in Flash Player 10,0,22</a></li>
<li><a href='http://www.barklund.org/blog/2009/11/04/advanced-e4x-assignment-xmllists/' rel='bookmark' title='Permanent Link: Advanced E4X &#8211; Assignment to XMLLists'>Advanced E4X &#8211; Assignment to XMLLists</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>I have been doing ActionScript 3 projects for quite a while, but every now and then I (as well as many others in the trade) have to switch back to AS2 or even AS1 &#8211; the line between them is quite blurry really in retrospective. When doing AS3 projects, there are many features that really come in handy, but it is only when you don&#8217;t have them available, that you really find out which are the true improvements, that you cannot live without, and which are just nice little tweaks, that make your everyday scripting easier.
<p>The latter category includes e.g. <a href="http://www.barklund.org/blog/2009/05/21/for-each-in-loops-in-actionscript-3/" title="For-each-in loops in ActionScript 3 from barklund.org">the for-each-in construct</a>, <a href="http://livedocs.adobe.com/flash/9.0/main/00000047.html" title="Data type descriptions from Adobe LiveDocs">different number types</a>, <a href="http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/utils/package.html#describeType%28%29" title="describeType() in the flash.utils package description from Adobe LiveDocs">class introspection</a> or <a href="http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/utils/package.html#getDefinitionByName%28%29" title="getDefinitionByName() in the flash.utils package description from Adobe LiveDocs">built-in class look-up by string</a>, but these are my top 6 AS3 features, that I find myself constantly missing when fixing or improving scripts in older versions of ActionScript:</p>
<p><span id="more-234"></span></p>
<h3 id="toc-number-6-default-values-for-parameters">Number 6: Default values for parameters</h3>
<blockquote><p>if (!scale) scale = 1;</p></blockquote>
<p>It is very simple, it doesn&#8217;t really look nice (at least the first couple of times you read/write it), but it gets the job done and it is consistent with how other languages does it. If it is the best possible choice for implementation, I cannot really say, but I wouldn&#8217;t be without it now that I have it &#8211; though I can easily think of improvements.</p>
<p>Okay, you are going to ask anyway, so here it is: It can be greatly improved by adding the ability to add parameters by name like in Python &#8211; e.g.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">ftp.<span style="color: #0066CC;">connect</span><span style="color: #66cc66;">&#40;</span>host=<span style="color: #ff0000;">&quot;www.server.com&quot;</span>,username=<span style="color: #ff0000;">&quot;administrator&quot;</span>,<span style="color: #0066CC;">password</span>=password_input.<span style="color: #0066CC;">text</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>It looks a bit strange but it is a great improvement &#8211; just compare it to how you need to specify the third (useCapture) and fourth (priority) arguments to addEventListener whenever you need to specify, that you want it to use weak references (the fifth argument set to true) &#8211; it does not look that good and could be a lot easier to use with named arguments.</p>
<h3 id="toc-number-5-e4x">Number 5: E4X</h3>
<blockquote><p>firstChild.firstChild.childNodes[i].lastChild.firstChild.attributes.id</p></blockquote>
<p>XML-based configuration files have been around for ages. I have done banners back in Flash 5, that used external configuration files for texts, layout, flow, paths, connection properties or something similiar. Of course, back then you always loaded the ignoreWhite script (wasn&#8217;t it XMLNitro.as from Branden Hall?), but you still had to either create your own serialization schemes, load someone else&#8217;s or go through the staggering pain of navigating through the XML-node tree through simple node references without knowing their names in advance.</p>
<p>With E4X we have been equipped with XPath-like traversal schemes, that still allow for basic node traversal, but makes it infinitely much easier to reach that specific node, that you need and know the name (and maybe some of the path) of.</p>
<h3 id="toc-number-4-instantiate-movieclips-even-across-swfs">Number 4: Instantiate movieclips &#8211; even across SWF&#8217;s</h3>
<blockquote><p>No more duplicateMovieClip madness</p></blockquote>
<p>I did a large isometric chat world in the good old Flash 5 days (and it only just recently got improved &#8211; which meant doubling the version number from Flash 5 to Flash 10 &#8211; now that is rare) and split code out in several FLA-files compiled to several SWF-files. But as everything needed to be possible to lay in-between each other all graphics needed to be in the same SWF-file. Which meant they had to come from the same FLA-file (unless we used <a href="http://www.swfmill.org" title="The home of SWFMill">swfmill</a> or something similarly fancy, which were in their infancy back then). Everything had to be exportable from the library and as items in the library couldn&#8217;t really be linked to classes directly, all item script had to be placed on the timeline. Of course it was a big mess and already back then we were hoping for a better way of loading content from external files (did anyone <strong>ever</strong> find out how to utilize &#8220;shared libraries&#8221; for actual production value?).</p>
<p>And now we have this feature &#8211; it is so much better. Create a symbol in a library in any FLA-file, load the resulting SWF-file in your production and instantiate any symbol from it and attach it anywhere you like. Clean and beautiful and exactly what we needed back then &#8211; and when going back, you really get that feeling again.</p>
<h3 id="toc-number-3-error-handling-and-run-time-validation">Number 3: Error handling and run-time validation</h3>
<blockquote><p>When it doesn&#8217;t say anything, does that mean no error happened?</p></blockquote>
<p>Imagine having put your AS2 production live, have it loaded in the browser and an error happens (because you visually can see that something is wrong) &#8211; how the h¤ll do you find out what happened? There were the old debugger, but it was crap &#8211; simply crap. You could only hope that you had enough trace&#8217;s spread around to debug based on your flashlog.txt file.</p>
<p>With AS3 we got run-time validation. If something is wrong, we (most of the time) get a huge ugly error popping out. It might look bad, might give bad user experience (if so, they ought to stop using the debug player though) but to the developer it gives the necessary feedback (thank you stack trace). And not to mention all the other advantages that run-time validation gives.</p>
<h3 id="toc-number-2-function-scoping">Number 2: Function scoping</h3>
<blockquote><p>I simply <strong>hate</strong> delegate.</p></blockquote>
<p>When doing pseudo-OOP like in ActionScript 2 (or AS1 or JavaScript), you constantly need to bind your functions as methods of your class instances in order to be sure, that if passed by reference, they are invoked in the right place. In ActionScript 3, this is no longer necessary. Basically, when passing a reference to a method of a class to someone or somewhere else, the scope of the method (being the originating instance) remains intact:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #6699cc; font-weight: bold;">var</span> a<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Sprite</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Sprite</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
<span style="color: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">b</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Object</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Object</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
<span style="color: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">c</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Sprite</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Sprite</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
<span style="color: #004993;">b</span>.<span style="color: #004993;">addChild</span> = a.<span style="color: #004993;">addChild</span>;
<span style="color: #004993;">b</span>.<span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">c</span><span style="color: #000000;">&#41;</span>;
<span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span>a.<span style="color: #004993;">numChildren</span><span style="color: #000000;">&#41;</span>; <span style="color: #009900;">// traces &quot;1&quot;</span></pre></div></div>

<p>Anyone using AS3 knows this by now, but this was just to re-iterate and give an example.</p>
<p>Actually, it is not function scoping. It is more about creating a clear distinction between functions and methods. Functions are anonymous and created inline and don&#8217;t really use a &#8220;this&#8221;-scope but a reference scope. Methods are attributes of classes and are always invoked <strong>on</strong> instances of the class.</p>
<p>It does remove some &#8220;scriptability&#8221; in ActionScript, as currying and similar tricks aren&#8217;t as useful anymore and actually you never use &#8220;normal&#8221; functions at all &#8211; only methods. But intelligent use of patterns can have the same effects as such minor tricks and though they require more typing, they produce cleaner and often more intelligent, re-usable code. But that is the target of a whole other discussion.</p>
<h3 id="toc-number-1-the-display-list-instead-of-depths">Number 1: The display-list instead of depths</h3>
<blockquote><p>myClip.getNextHighestDepthLowerThanTheDepthOfTheMenu()</p></blockquote>
<p>Depth. The most annoying part of AS2. I didn&#8217;t realize it back then, you were so accustomed to writing foo.createEmptyMovieClip(&#8220;myclip&#8221;, foo.getNextHighestDepth()) and then swapDepths&#8217;ing it all around afterwards, but now that I don&#8217;t need it anymore but have to go back to using it &#8211; it is so time consuming.</p>
<p>At first, the display list sounded a bit scary to me and the very first demos of the language explaining MXML and layout schemes gave horrible flashbacks to the script-consuming &#8220;layout engine&#8221;, that Java and SWING employs. But most of us never use any of those layout schemes, simply add items to stage on coordinates with the display list being the best tool to ensure proper depth management. Thank you!</p>
<p>&nbsp;</p>
<p>Please leave comments, if you have other features than you constantly find yourself missing, or in other ways believe you can improve this list. Oh, and if you have suggestions for other lists, then by all means add them here.</p>


<p>Related posts:<ol><li><a href='http://www.barklund.org/blog/2009/07/01/minor-change-in-handling-nan-positions-in-flash-player-10-0-22/' rel='bookmark' title='Permanent Link: Minor change in handling NaN positions in Flash Player 10,0,22'>Minor change in handling NaN positions in Flash Player 10,0,22</a></li>
<li><a href='http://www.barklund.org/blog/2009/11/04/advanced-e4x-assignment-xmllists/' rel='bookmark' title='Permanent Link: Advanced E4X &#8211; Assignment to XMLLists'>Advanced E4X &#8211; Assignment to XMLLists</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.barklund.org/blog/2009/07/19/top-6-as3-features-you-miss-when-switching-back-to-as2/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Minor change in handling NaN positions in Flash Player 10,0,22</title>
		<link>http://www.barklund.org/blog/2009/07/01/minor-change-in-handling-nan-positions-in-flash-player-10-0-22/</link>
		<comments>http://www.barklund.org/blog/2009/07/01/minor-change-in-handling-nan-positions-in-flash-player-10-0-22/#comments</comments>
		<pubDate>Wed, 01 Jul 2009 12:34:38 +0000</pubDate>
		<dc:creator>Barklund</dc:creator>
				<category><![CDATA[AS2]]></category>
		<category><![CDATA[AS3]]></category>

		<guid isPermaLink="false">http://www.barklund.org/blog/?p=163</guid>
		<description><![CDATA[I&#8217;ve just discovered a &#8220;bug&#8221; in the latest Flash Player. Actually the &#8220;bug&#8221; was in a production, that only worked in the latest player and not in previous players. I&#8217;ve boiled it down to this: If the x- or y-attribute of a DisplayObject is set to NaN, in previous players this caused the attribute to [...]


Related posts:<ol><li><a href='http://www.barklund.org/blog/2009/10/21/bitmap-fill-crash-flash-cs4/' rel='bookmark' title='Permanent Link: Weird bitmap fill crash error in Flash CS4.'>Weird bitmap fill crash error in Flash CS4.</a></li>
<li><a href='http://www.barklund.org/blog/2009/06/08/using-addthis-with-flash/' rel='bookmark' title='Permanent Link: Using AddThis with Flash'>Using AddThis with Flash</a></li>
<li><a href='http://www.barklund.org/blog/2009/07/06/flash-development-scripted-layout/' rel='bookmark' title='Permanent Link: Flash development and the practice of Scripted Layout'>Flash development and the practice of Scripted Layout</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve just discovered a &#8220;bug&#8221; in the latest Flash Player. Actually the &#8220;bug&#8221; was in a production, that only worked in the latest player and not in previous players. I&#8217;ve boiled it down to this:</p>
<blockquote><p>
If the x- or y-attribute of a DisplayObject is set to NaN, in previous players this caused the attribute to be set to -107374182.4. In the latest player, the attribute is set to 0.</p></blockquote>
<p><span id="more-163"></span></p>
<p>The value -107,374,182.4 is -2,147,483,648/20. Flash uses &#8220;twixels&#8221; for positions, which is a twentieth pixel. Thus the lowest possible value x or y can hold is -(2^31)/20 which is exactly -107,374,182.4. This means, that when rendering DisplayObjects with a position in either direction with a value of NaN, the position was instead rendered with the lowest possible value for this attribute previously, but now it is rendered with this attribute set to 0 instead.</p>
<p>Thus, it is <strong>not</strong> a bug per se, but it is a change in behavior in response to an error condition &#8211; namely bad positioning.</p>
<p>My test-page can be found <a href="/examples/bughunt_undefined.html" title="Demo-page for undefined behavior">here</a>. Please try this page out and copy-paste the results in the comments, if you do not experience the same results as described above. The result in Flash Player 10,0,22(,87) on Mac is:</p>

<div class="wp_syntax"><div class="code"><pre class="plain" style="font-family:monospace;">Player version: MAC 10,0,22,87
1 + undefined: NaN
typeof (1 + undefined): number
1 + (1 + undefined): NaN
typeof (1 + (1 + undefined)): number
sprite.x: 70
typeof sprite.x: number
sprite.x += undefined
sprite.x: 0 &lt;--
typeof sprite.x: number</pre></div></div>

<p>In the previous player 10,0,12(,36) the result was:</p>

<div class="wp_syntax"><div class="code"><pre class="plain" style="font-family:monospace;">Player version: MAC 10,0,12,36
1 + undefined: NaN
typeof (1 + undefined): number
1 + (1 + undefined): NaN
typeof (1 + (1 + undefined)): number
sprite.x: 70
typeof sprite.x: number
sprite.x += undefined
sprite.x: -107374182.4 &lt;--
typeof sprite.x: number</pre></div></div>

<p>Note the second-to-last line &#8211; the value of x is different when &#8220;undefined&#8221; is added to the current value.</p>
<p>Some other test results from non-Mac versions as well as previous versions:</p>

<div class="wp_syntax"><div class="code"><pre class="plain" style="font-family:monospace;">Player version: MAC 10,0,2,54
1 + undefined: NaN
typeof (1 + undefined): number
1 + (1 + undefined): NaN
typeof (1 + (1 + undefined)): number
sprite.x: 70
typeof sprite.x: number
sprite.x += undefined
sprite.x: -107374182.4
typeof sprite.x: number
&nbsp;
Player version: MAC 9,0,159,0
1 + undefined: NaN
typeof (1 + undefined): number
1 + (1 + undefined): NaN
typeof (1 + (1 + undefined)): number
sprite.x: 70
typeof sprite.x: number
sprite.x += undefined
sprite.x: -107374182.4
typeof sprite.x: number
&nbsp;
Player version: MAC 9,0,124,0
1 + undefined: NaN
typeof (1 + undefined): number
1 + (1 + undefined): NaN
typeof (1 + (1 + undefined)): number
sprite.x: 70
typeof sprite.x: number
sprite.x += undefined
sprite.x: -107374182.4
typeof sprite.x: number
&nbsp;
Player version: WIN 10,0,22,87
1 + undefined: NaN
typeof (1 + undefined): number
1 + (1 + undefined): NaN
typeof (1 + (1 + undefined)): number
sprite.x: 70
typeof sprite.x: number
sprite.x += undefined
sprite.x: 0
typeof sprite.x: number</pre></div></div>

<p>My very simple source FLA file is available <a href="/examples/bughunt_undefined.fla" title="Link to FLA source file">here</a>. The xtra info in the above is simply to make sure, that no other changes in handling NaN or undefined has happened.</p>
<p>Thanks to David Wulff, Anders Krarup and Katharina Berg for help in testing.</p>
<p><strong>Update 4.07PM</strong>: It is actually mentioned in the <a href="http://www.adobe.com/support/documentation/en/flashplayer/releasenotes.html" title="Flash Player Release Notes for all versions">release notes</a> as: &#8220;Fix AS APIs&#8217; inconsistent handling of NaN parameters on different platforms. (<a href="https://bugs.adobe.com/jira/browse/FP-612">FP-612</a>,<a href="https://bugs.adobe.com/jira/browse/FP-903">FP- 903</a>, <a href="https://bugs.adobe.com/jira/browse/FP-964">FP-964</a>/2200454)&#8221;. However, all these reports are about Windows installations only and they all mention, that all players before FP10 set the attribute to 0 and setting the attribute to  -107374182.4 only happened in Flash Player 10,0,2 and 10,0,12 (and probably also the Linux-only 10,0,15). From my results however, it is clear that for Mac this issue has always been the case also in Flash Player 9, which is revealed in the comment about the fix as &#8220;inconsistent handling [...] on different platforms&#8221;.</p>
<p>It seems to be an actual desired result that this was &#8220;fixed&#8221; in order to bring the error condition handling back to the way it has always been in flash players for Windows in all previous versions before FP10.</p>


<p>Related posts:<ol><li><a href='http://www.barklund.org/blog/2009/10/21/bitmap-fill-crash-flash-cs4/' rel='bookmark' title='Permanent Link: Weird bitmap fill crash error in Flash CS4.'>Weird bitmap fill crash error in Flash CS4.</a></li>
<li><a href='http://www.barklund.org/blog/2009/06/08/using-addthis-with-flash/' rel='bookmark' title='Permanent Link: Using AddThis with Flash'>Using AddThis with Flash</a></li>
<li><a href='http://www.barklund.org/blog/2009/07/06/flash-development-scripted-layout/' rel='bookmark' title='Permanent Link: Flash development and the practice of Scripted Layout'>Flash development and the practice of Scripted Layout</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.barklund.org/blog/2009/07/01/minor-change-in-handling-nan-positions-in-flash-player-10-0-22/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>For-each-in loops in ActionScript 3</title>
		<link>http://www.barklund.org/blog/2009/05/21/for-each-in-loops-in-actionscript-3/</link>
		<comments>http://www.barklund.org/blog/2009/05/21/for-each-in-loops-in-actionscript-3/#comments</comments>
		<pubDate>Thu, 21 May 2009 18:03:32 +0000</pubDate>
		<dc:creator>Barklund</dc:creator>
				<category><![CDATA[AS3]]></category>

		<guid isPermaLink="false">http://www.barklund.org/blog/?p=84</guid>
		<description><![CDATA[For-each-in is quite a secret looping method in ActionScript. It was added in ActionScript 3 in Flash Player 9 (actually in the 8.5 release). It has existed in other languages like PHP for quite some time, and Java has a similar iteration method added in recent years. It is ideal for looping over arrays of [...]


Related posts:<ol><li><a href='http://www.barklund.org/blog/2010/05/07/events-suck-actionscript-3/' rel='bookmark' title='Permanent Link: Why events suck in ActionScript 3'>Why events suck in ActionScript 3</a></li>
<li><a href='http://www.barklund.org/blog/2009/11/04/advanced-e4x-assignment-xmllists/' rel='bookmark' title='Permanent Link: Advanced E4X &#8211; Assignment to XMLLists'>Advanced E4X &#8211; Assignment to XMLLists</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>For-each-in is quite a secret looping method in ActionScript. It was <a href="http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/statements.html#for_each..in" title="for each..in	description in Adobe LiveDocs">added in ActionScript 3 in Flash Player 9 (actually in the 8.5 release)</a>. It has existed in other languages like <a href="http://dk.php.net/manual/en/control-structures.foreach.php" title="foreach on PHP.net">PHP</a> for quite some time, and <a href="http://leepoint.net/notes-java/flow/loops/foreach.html" title="Java: For-each loop from leepoint.net">Java has a similar iteration method added in recent years</a>. It is ideal for looping over arrays of similar-typed elements, as it loops over values, not keys.</p>
<p><span id="more-84"></span></p>
<p>The for-in loop iterates over keys like this:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #6699cc; font-weight: bold;">var</span> items<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Array</span> = <span style="color: #000000;">&#91;</span><span style="color: #990000;">&quot;hi&quot;</span>, <span style="color: #990000;">&quot;there&quot;</span>, <span style="color: #990000;">&quot;bob&quot;</span><span style="color: #000000;">&#93;</span>;
<span style="color: #0033ff; font-weight: bold;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">index</span> <span style="color: #0033ff; font-weight: bold;">in</span> items<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
	<span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">index</span><span style="color: #000000; font-weight: bold;">+</span><span style="color: #990000;">&quot; =&gt; &quot;</span><span style="color: #000000; font-weight: bold;">+</span>items<span style="color: #000000;">&#91;</span><span style="color: #004993;">index</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span>; <span style="color: #009900;">// traces &quot;0 =&gt; hi&quot;, &quot;1 =&gt; there&quot; and &quot;2 =&gt; bob&quot;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>The problem with this construct is, that it is actually meant to loop over properties of an object, not indexes of an array. This can be seen by the strange typing requirement &#8211; if I wanted to type the value of <code>index</code> in the above (which I really ought to), it must be typed as a String:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #6699cc; font-weight: bold;">var</span> items<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Array</span> = <span style="color: #000000;">&#91;</span><span style="color: #990000;">&quot;hi&quot;</span>, <span style="color: #990000;">&quot;there&quot;</span>, <span style="color: #990000;">&quot;bob&quot;</span><span style="color: #000000;">&#93;</span>;
<span style="color: #0033ff; font-weight: bold;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">index</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span> <span style="color: #0033ff; font-weight: bold;">in</span> items<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
	<span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">index</span><span style="color: #000000; font-weight: bold;">+</span><span style="color: #990000;">&quot; =&gt; &quot;</span><span style="color: #000000; font-weight: bold;">+</span>items<span style="color: #000000;">&#91;</span><span style="color: #004993;">index</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span>; <span style="color: #009900;">// traces &quot;0 =&gt; hi&quot;, &quot;1 =&gt; there&quot; and &quot;2 =&gt; bob&quot;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>If it was typed as a number, the compile-time validator would report an error:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">1067: Implicit coercion of a value of type String to an unrelated type Number.</pre></div></div>

<p>This makes the for-in loop quite flawed for array iteration.</p>
<p>The for-each-in loop however loops over values:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #6699cc; font-weight: bold;">var</span> items<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Array</span> = <span style="color: #000000;">&#91;</span><span style="color: #990000;">&quot;hi&quot;</span>, <span style="color: #990000;">&quot;there&quot;</span>, <span style="color: #990000;">&quot;bob&quot;</span><span style="color: #000000;">&#93;</span>;
<span style="color: #0033ff; font-weight: bold;">for</span> <span style="color: #0033ff; font-weight: bold;">each</span> <span style="color: #000000;">&#40;</span><span style="color: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">value</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span> <span style="color: #0033ff; font-weight: bold;">in</span> items<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
	<span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">value</span><span style="color: #000000;">&#41;</span>; <span style="color: #009900;">// traces &quot;hi&quot;, &quot;there&quot; and &quot;bob&quot;</span>
<span style="color: #000000;">&#125;</span>
<span style="color: #6699cc; font-weight: bold;">var</span> numbers<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Array</span> = <span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">2</span>, <span style="color: #000000; font-weight:bold;">3</span>, <span style="color: #000000; font-weight:bold;">5</span>, <span style="color: #000000; font-weight:bold;">7</span><span style="color: #000000;">&#93;</span>;
<span style="color: #0033ff; font-weight: bold;">for</span> <span style="color: #0033ff; font-weight: bold;">each</span> <span style="color: #000000;">&#40;</span><span style="color: #6699cc; font-weight: bold;">var</span> prime<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">uint</span> <span style="color: #0033ff; font-weight: bold;">in</span> numbers<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
	<span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span>prime<span style="color: #000000;">&#41;</span>; <span style="color: #009900;">// traces 2, 3, 5 and 7</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>For these simple-valued arrays it might not seem like it matters, but what if we were working on an array of objects; maybe a list of XML nodes:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #6699cc; font-weight: bold;">var</span> books<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">XML</span> = <span style="color: #000000; font-weight: bold;">&lt;</span>books<span style="color: #000000; font-weight: bold;">&gt;</span>
	<span style="color: #000000; font-weight: bold;">&lt;</span>book<span style="color: #000000; font-weight: bold;">&gt;</span>
		<span style="color: #000000; font-weight: bold;">&lt;</span>author<span style="color: #000000; font-weight: bold;">&gt;</span>John Smith<span style="color: #000000; font-weight: bold;">&lt;/</span>author<span style="color: #000000; font-weight: bold;">&gt;</span>
		<span style="color: #000000; font-weight: bold;">&lt;</span>title<span style="color: #000000; font-weight: bold;">&gt;</span>The Book About Nothing<span style="color: #000000; font-weight: bold;">&lt;/</span>title<span style="color: #000000; font-weight: bold;">&gt;</span>
	<span style="color: #000000; font-weight: bold;">&lt;/</span>book<span style="color: #000000; font-weight: bold;">&gt;</span>
	<span style="color: #000000; font-weight: bold;">&lt;</span>book<span style="color: #000000; font-weight: bold;">&gt;</span>
		<span style="color: #000000; font-weight: bold;">&lt;</span>author<span style="color: #000000; font-weight: bold;">&gt;</span>Jane Doe<span style="color: #000000; font-weight: bold;">&lt;/</span>author<span style="color: #000000; font-weight: bold;">&gt;</span>
		<span style="color: #000000; font-weight: bold;">&lt;</span>title<span style="color: #000000; font-weight: bold;">&gt;</span>The Book About Everything<span style="color: #000000; font-weight: bold;">&lt;/</span>title<span style="color: #000000; font-weight: bold;">&gt;</span>
	<span style="color: #000000; font-weight: bold;">&lt;/</span>book<span style="color: #000000; font-weight: bold;">&gt;</span>
<span style="color: #000000; font-weight: bold;">&lt;/</span>books<span style="color: #000000; font-weight: bold;">&gt;</span>;
<span style="color: #0033ff; font-weight: bold;">for</span> <span style="color: #0033ff; font-weight: bold;">each</span> <span style="color: #000000;">&#40;</span><span style="color: #6699cc; font-weight: bold;">var</span> book<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">XML</span> <span style="color: #0033ff; font-weight: bold;">in</span> books.book<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
	<span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span>book.author<span style="color: #000000;">&#41;</span>; <span style="color: #009900;">// traces &quot;John Smith&quot; and &quot;Jane Doe&quot;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>See how nice it is? There is however no compile-time validation of the type of the iterator variable (<code>value</code> in the above code), and this compiles just fine (but throws an error during execution):</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #0033ff; font-weight: bold;">for</span> <span style="color: #0033ff; font-weight: bold;">each</span> <span style="color: #000000;">&#40;</span><span style="color: #6699cc; font-weight: bold;">var</span> book<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Sprite</span> <span style="color: #0033ff; font-weight: bold;">in</span> books.book<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
	<span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span>book<span style="color: #000000;">&#41;</span>; <span style="color: #009900;">// this is never reached</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>But then comes the new Vector-type. Vector is generic, which means it can be typed to reflect the values:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #6699cc; font-weight: bold;">var</span> books<span style="color: #000000; font-weight: bold;">:</span>Vector.<span style="color: #000000; font-weight: bold;">&lt;</span>XML<span style="color: #000000; font-weight: bold;">&gt;</span> = <span style="color: #0033ff; font-weight: bold;">new</span> Vector.<span style="color: #000000; font-weight: bold;">&lt;</span>XML<span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
books.<span style="color: #004993;">push</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight: bold;">&lt;</span>book<span style="color: #000000; font-weight: bold;">&gt;&lt;</span>author<span style="color: #000000; font-weight: bold;">&gt;</span>John Smith<span style="color: #000000; font-weight: bold;">&lt;/</span>author<span style="color: #000000; font-weight: bold;">&gt;&lt;</span>title<span style="color: #000000; font-weight: bold;">&gt;</span>The Book About Nothing<span style="color: #000000; font-weight: bold;">&lt;/</span>title<span style="color: #000000; font-weight: bold;">&gt;&lt;/</span>book<span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000;">&#41;</span>;
books.<span style="color: #004993;">push</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight: bold;">&lt;</span>book<span style="color: #000000; font-weight: bold;">&gt;&lt;</span>author<span style="color: #000000; font-weight: bold;">&gt;</span>Jane Doe<span style="color: #000000; font-weight: bold;">&lt;/</span>author<span style="color: #000000; font-weight: bold;">&gt;&lt;</span>title<span style="color: #000000; font-weight: bold;">&gt;</span>The Book About Everything<span style="color: #000000; font-weight: bold;">&lt;/</span>title<span style="color: #000000; font-weight: bold;">&gt;&lt;/</span>book<span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000;">&#41;</span>;
<span style="color: #0033ff; font-weight: bold;">for</span> <span style="color: #0033ff; font-weight: bold;">each</span> <span style="color: #000000;">&#40;</span><span style="color: #6699cc; font-weight: bold;">var</span> book<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">XML</span> <span style="color: #0033ff; font-weight: bold;">in</span> books<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
	<span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span>book.author<span style="color: #000000;">&#41;</span>; <span style="color: #009900;">// will again correctly trace authors</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>I for one hoped, that this for-each-in loop would be type-checked at compile-time to verify, that my iterator variable had the same type as the elements of the Vector, but no, I can still change the type to e.g. Sprite and it compiles just fine (but of course still causes an error at execution). This is a clear weakness compared to the Java for-each loop, which does exactly that &ndash; checks the type of the iterator variable compared to the generic type of the iterable list.</p>
<p>One final note about the secret loop is, that the built-in editor in Flash CS4 does not highlight the word &#8220;each&#8221; as a keyword, which is quite strange. I can even use the word &#8220;each&#8221; as a variable name, which I clearly cannot with e.g. &#8220;while&#8221;, &#8220;for&#8221;, &#8220;function&#8221; or other reserved keywords. It seems like it is not a &#8220;real&#8221; keyword, just some extra word that can be appended to &#8220;for&#8221; and have a special meaning when present. Oh, and as can be seen here, <a href="https://sourceforge.net/tracker/?func=detail&#038;aid=2795005&#038;group_id=114997&#038;atid=670231" title="Bug report by me at the GeSHI bug tracker">the GeShi syntax highlighter doesn&#8217;t highlight it either</a> ;) (UPDATE: Well, I if switched highlighting to AS3 it worked, so actually that was my own fault).</p>
<p>Nevertheless, for-each-in loops are a great asset and especially when working with XML and the new notation E4X for XML traversal.</p>


<p>Related posts:<ol><li><a href='http://www.barklund.org/blog/2010/05/07/events-suck-actionscript-3/' rel='bookmark' title='Permanent Link: Why events suck in ActionScript 3'>Why events suck in ActionScript 3</a></li>
<li><a href='http://www.barklund.org/blog/2009/11/04/advanced-e4x-assignment-xmllists/' rel='bookmark' title='Permanent Link: Advanced E4X &#8211; Assignment to XMLLists'>Advanced E4X &#8211; Assignment to XMLLists</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.barklund.org/blog/2009/05/21/for-each-in-loops-in-actionscript-3/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>GTween is the bomb for rapid prototyping</title>
		<link>http://www.barklund.org/blog/2009/05/19/gtween-bomb/</link>
		<comments>http://www.barklund.org/blog/2009/05/19/gtween-bomb/#comments</comments>
		<pubDate>Tue, 19 May 2009 11:11:55 +0000</pubDate>
		<dc:creator>Barklund</dc:creator>
				<category><![CDATA[AS3]]></category>
		<category><![CDATA[Konstellation]]></category>

		<guid isPermaLink="false">http://www.barklund.org/blog/2009/05/19/gtween-bomb/</guid>
		<description><![CDATA[I&#8217;ve been using tweens a lot recently &#8211; for example while developing the small campaign site for the new Peugeot 206+ and Peugeot Blue Drive campaign (in Danish). I used GTween and GTweenTimeline to a great extent to really make it easy for me to implement and update. One of the main things, that I [...]


No related posts.]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been using tweens a lot recently &#8211; for example while developing the small campaign site for the new <a href="http://www.bluedrive.dk/" title="Blue Drive Campaign">Peugeot 206+ and Peugeot Blue Drive campaign (in Danish)</a>. I used <a href="http://www.gskinner.com/libraries/gtween/" title="Main page for GTween library by gskinner.com">GTween and GTweenTimeline</a> to a great extent to really make it easy for me to implement and update.</p>
<p><span id="more-50"></span></p>
<p>One of the main things, that I conceptually did in a different way on this site is, that I created tweens before I needed them. Actually, I created tween objects without even knowing what I wanted to tween at the time.</p>
<p>E.g. the &#8220;sound on/off&#8221; button (which unfortunately was removed just before launch) was implemented in a very simple way. On the class I kept a reference to a tween-object, and I created an instance for this as soon as the sound started playing (in continuous loop):</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> background_music:<span style="color: #0066CC;">Sound</span> = <span style="color: #000000; font-weight: bold;">new</span> BackgroundMusic<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #000000; font-weight: bold;">var</span> sc:SoundChannel = background_music.<span style="color: #0066CC;">play</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">100000</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// to infinity and beyond</span>
soundtween = <span style="color: #000000; font-weight: bold;">new</span> GTween<span style="color: #66cc66;">&#40;</span>sc.<span style="color: #006600;">soundTransform</span>, <span style="color: #cc66cc;">0.3</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>In this way, I can simply make the sound start fading by changing the volume-attribute on the proxy object of the soundtween-property:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">soundtween.<span style="color: #006600;">proxy</span>.<span style="color: #006600;">volume</span> = <span style="color: #cc66cc;">0</span>; <span style="color: #808080; font-style: italic;">// fade out</span></pre></div></div>

<p>Though, as some might remember, you <a href="http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/media/SoundChannel.html#soundTransform" title="LiveDocs entry for flash.media.SoundChannel.soundTransform">cannot simply change the volume-attribute directly on the soundTransform-property of your SoundChannel</a>. You need to copy the SoundTransform instance to a local variable, change the volume and re-assign the soundTransform-property. However, GTween takes care of this in a very simple way:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">soundtween.<span style="color: #006600;">setAssignment</span><span style="color: #66cc66;">&#40;</span>sc, <span style="color: #ff0000;">&quot;soundTransform&quot;</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>This is not only useful for sounds but also for all the other transform objects like <a href="http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/geom/Transform.html" title="LiveDocs entry for flash.geom.Transform">DisplayObject.transform.colorTransform, DisplayObject.transform.matrix</a> and other objects that require re-assignment before having an effect.</p>
<p>I will furthermore in additional posts describe how I have used GTweenTimeline for rapid prototype development with very nice transitions.</p>


<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://www.barklund.org/blog/2009/05/19/gtween-bomb/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using ShareThis with Flash</title>
		<link>http://www.barklund.org/blog/2009/05/06/using-sharethis-with-flash/</link>
		<comments>http://www.barklund.org/blog/2009/05/06/using-sharethis-with-flash/#comments</comments>
		<pubDate>Wed, 06 May 2009 12:44:50 +0000</pubDate>
		<dc:creator>Barklund</dc:creator>
				<category><![CDATA[API]]></category>
		<category><![CDATA[AS2]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://www.barklund.org/blog/2009/05/06/using-sharethis-with-flash/</guid>
		<description><![CDATA[This feature does currently not work &#8211; see list of updates below ShareThis is a great service for adding shareability to your website in a very simple way. It even includes the &#8220;tip a friend&#8221; option, so you don&#8217;t need to code that for your own site. I will describe how to embed it from [...]


Related posts:<ol><li><a href='http://www.barklund.org/blog/2009/06/08/using-addthis-with-flash/' rel='bookmark' title='Permanent Link: Using AddThis with Flash'>Using AddThis with Flash</a></li>
<li><a href='http://www.barklund.org/blog/2009/07/01/minor-change-in-handling-nan-positions-in-flash-player-10-0-22/' rel='bookmark' title='Permanent Link: Minor change in handling NaN positions in Flash Player 10,0,22'>Minor change in handling NaN positions in Flash Player 10,0,22</a></li>
<li><a href='http://www.barklund.org/blog/2009/09/04/mxhr4as3-multipart-mixed-file-download-flash/' rel='bookmark' title='Permanent Link: Announcing MXHR4AS3: Multipart/mixed-file download by Flash clients'>Announcing MXHR4AS3: Multipart/mixed-file download by Flash clients</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<blockquote style="background: #333; font-size: 120%; padding: 5px;"><p>This feature does currently not work &#8211; see list of updates below</p></blockquote>
<p>
<a href="http://www.sharethis.com" title="ShareThis website">ShareThis</a> is a great service for adding shareability to your website in a very simple way. It even includes the &#8220;tip a friend&#8221; option, so you don&#8217;t need to code that for your own site. I will describe how to embed it from flash.
</p>
<p><strong>Note 2009/05/19: </strong>Due to changes in the ShareThis API popup-functionality is broken currently (both in my example and in some of my live projects). I have filed a bug with ShareThis to fix this ASAP.</p>
<p><strong>Note 2009/05/21: </strong>ShareThis have now fixed the above bug but there is still a minor JavaScript error thrown. The popup works now though as it should from both JS and Flash.</p>
<p><strong>Note 2009/06/08: </strong>ShareThis now broke it again. And this time their support staff doesn&#8217;t answer. Please see <a href="http://www.barklund.org/blog/2009/06/08/using-addthis-with-flash/" title="Using AddThis with Flash from Barklund.org">an example implemented with AddThis</a> instead.</p>
<p><strong>Note 2009/06/19: </strong>ShareThis finally fixed it again. They wrote a mail on the 17th to me stating the bug was reintroduced when they implemented CDN, but would be fixed in the next release. Let&#8217;s hope it stays that way.</p>
<p><strong>Note 2009/08/14: </strong>ShareThis has recently implemented a brand new version of their service &#8211; and surprise: it is <strong>not</strong> backwards compatible. Thus my popup example is broken again. I will investigate and fix soon.</p>
<p><strong>Note 2009/08/17: </strong>ShareThis has promised to deliver a flash API within a few weeks. Let&#8217;s see what happens then&#8230;</p>
<p><span id="more-48"></span></p>
<p>
You can do that in two ways, either by using a overlay div in HTML or creating a popup (that might get blocked). The prettiest and most user friendly is of course the overlay, but in order to have HTML overlays on top of Flash elements, you need to set the Flash <a href="http://livedocs.adobe.com/flash/8/main/wwhelp/wwhimpl/common/html/wwhelp.htm?context=LiveDocs_Parts&#038;file=00000852.html" title="Adobe Livedocs: wmode attribute/parameter description">wmode to transparent</a>. This has a strange and unfortunate side-effect (which has been <a href="http://bugs.adobe.com/jira/browse/FP-479" title="Adobe bug-report - it is marked as closed, but it is not resolved">bug-reported to Adobe</a>), that the keyboard defaults to standard US layout. For US websites, this is not a big problem, but for a Danish website it matters a lot. There are many differences between US and DK keyboard layout including Danish special characters and the location of the @-sign. Thus, wmode-transparent cannot be used for Flash-objects having input fields requiring users to enter text (which could contain special characters) or an email address (which contains the @-sign). Thus we need to use the popup-solution.
</p>
<p>
First, simply go to <a href="http://sharethis.com/publishers/" title="Publisher guide to ShareThis">sharethis.com</a> publisher section and <a href="http://sharethis.com/publishers/getbutton/" title="Get the Button">customize your widget</a>. Instead of placing the widget code snippet where you want the button, put it in the &lt;head&gt;-section of your website.  The code snippet I got was:
</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;script type=&quot;text/javascript&quot; src=&quot;http://w.sharethis.com/button/sharethis.js#publisher=636e055b-a4a2-4f9c-872c-b7aa9a701bb0&amp;amp;type=website&amp;amp;send_services=email&amp;amp;post_services=facebook%2Clinkedin%2Cmyspace%2Cdigg%2Cdelicious%2Ctwitter%2Creddit%2Ctechnorati%2Cwordpress%2Cblogger%2Cgoogle_bmarks%2Cwindows_live&quot;&gt;&lt;/script&gt;</pre></div></div>

<p>
Then create a little javascript function like:
</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">  <span style="color: #003366; font-weight: bold;">function</span> share<span style="color: #009900;">&#40;</span>url<span style="color: #339933;">,</span> title<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> s <span style="color: #339933;">=</span> SHARETHIS.<span style="color: #660066;">addEntry</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
      url<span style="color: #339933;">:</span> url<span style="color: #339933;">,</span>
      title<span style="color: #339933;">:</span> title
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>button<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">,</span>popup<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    s.<span style="color: #660066;">popup</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span></pre></div></div>

<p>
Finally, simple call this function from Flash using <a href="http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/external/ExternalInterface.html" title="ExternalInterface Livedocs documentation for ActionScript 3">ExternalInterface</a>:
</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">url</span>:<span style="color: #0066CC;">String</span> = <span style="color: #ff0000;">&quot;http://www.barklund.org/blog/2009/05/06/using-sharethis-with-flash/&quot;</span>;
<span style="color: #000000; font-weight: bold;">var</span> title:<span style="color: #0066CC;">String</span> = <span style="color: #ff0000;">&quot;Barklund.org - Using ShareThis with Flash&quot;</span>;
<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">external</span>.<span style="color: #006600;">ExternalInterface</span>;
ExternalInterface.<span style="color: #0066CC;">call</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;share&quot;</span>, <span style="color: #0066CC;">url</span>, title<span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>
See an example <a href="/examples/sharethis/" title="Examples using ShareThis with Flash">here</a> &#8211; the javascript function is invoked both from a regular HTML link and from a simple Flash file using just the above ActionScript.</p>


<p>Related posts:<ol><li><a href='http://www.barklund.org/blog/2009/06/08/using-addthis-with-flash/' rel='bookmark' title='Permanent Link: Using AddThis with Flash'>Using AddThis with Flash</a></li>
<li><a href='http://www.barklund.org/blog/2009/07/01/minor-change-in-handling-nan-positions-in-flash-player-10-0-22/' rel='bookmark' title='Permanent Link: Minor change in handling NaN positions in Flash Player 10,0,22'>Minor change in handling NaN positions in Flash Player 10,0,22</a></li>
<li><a href='http://www.barklund.org/blog/2009/09/04/mxhr4as3-multipart-mixed-file-download-flash/' rel='bookmark' title='Permanent Link: Announcing MXHR4AS3: Multipart/mixed-file download by Flash clients'>Announcing MXHR4AS3: Multipart/mixed-file download by Flash clients</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.barklund.org/blog/2009/05/06/using-sharethis-with-flash/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
		<item>
		<title>QR codes and Flash on the Beach</title>
		<link>http://www.barklund.org/blog/2008/10/02/qr-codes-and-flash-on-the-beach/</link>
		<comments>http://www.barklund.org/blog/2008/10/02/qr-codes-and-flash-on-the-beach/#comments</comments>
		<pubDate>Wed, 01 Oct 2008 22:32:52 +0000</pubDate>
		<dc:creator>Barklund</dc:creator>
				<category><![CDATA[AS3]]></category>

		<guid isPermaLink="false">http://www.barklund.org/blog/2008/10/02/qr-codes-and-flash-on-the-beach/</guid>
		<description><![CDATA[Flash on the Beach is just over and the final day had the most impressive (and the single most useful) presentations. The inspirational level this year has been great. The wonderfully beautiful creations by Eric Natzke or Robert Hodgin can take anyones breath away and the generally inspiring nature of Jonathan Harris&#8217; works makes everyone [...]


Related posts:<ol><li><a href='http://www.barklund.org/blog/2009/07/06/flash-development-scripted-layout/' rel='bookmark' title='Permanent Link: Flash development and the practice of Scripted Layout'>Flash development and the practice of Scripted Layout</a></li>
<li><a href='http://www.barklund.org/blog/2009/10/21/bitmap-fill-crash-flash-cs4/' rel='bookmark' title='Permanent Link: Weird bitmap fill crash error in Flash CS4.'>Weird bitmap fill crash error in Flash CS4.</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.flashonthebeach.com" "Official website">Flash on the Beach</a> is just over and the final day had the most impressive (and the single most useful) presentations. The inspirational level this year has been great. The wonderfully beautiful creations by <a href="http://jot.eriknatzke.com/" title="Eric's blog">Eric Natzke</a> or <a href="http://www.flight404.com/blog/" "Robert's blog">Robert Hodgin</a> can take anyones breath away and the generally inspiring nature of <a href="http://www.number27.org/" title="Jonathan's website">Jonathan Harris&#8217;</a> works makes everyone want to do the same (if only we could get similar great ideas).</p>
<p>But <a href="http://www.quasimondo.com/" title="Mario's blog">Mario Klingemann&#8217;s</a> presentation about reading <a href="http://en.wikipedia.org/wiki/QR_Code" title="QR Code explained on the English Wikipedia">QR codes</a> in Flash was the most (technically) inspiring presentation in my opinion and I could immediately see a bunch of ways in which, I could use this for all different sorts of purposes &#8211; in real life advertising and not just for the fun of it. I really feel that I could join this project and make it into a complete library and so far I&#8217;ve written him a mail offering my help (and a minor possibility for optimization).</p>
<p>But credits also go to <a href="http://www.gskinner.com" title="Grant's website">Grant Skinner</a>, as his talk on important things to learn as a new web developer (in Flash in particular) gave me some good inspiration for my own job as head of development with developers of my own needing help and guidelines.</p>


<p>Related posts:<ol><li><a href='http://www.barklund.org/blog/2009/07/06/flash-development-scripted-layout/' rel='bookmark' title='Permanent Link: Flash development and the practice of Scripted Layout'>Flash development and the practice of Scripted Layout</a></li>
<li><a href='http://www.barklund.org/blog/2009/10/21/bitmap-fill-crash-flash-cs4/' rel='bookmark' title='Permanent Link: Weird bitmap fill crash error in Flash CS4.'>Weird bitmap fill crash error in Flash CS4.</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.barklund.org/blog/2008/10/02/qr-codes-and-flash-on-the-beach/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
