<?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>Adnan Doric &#187; Actionscript</title>
	<atom:link href="http://www.adnandoric.com/category/actionscript/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.adnandoric.com</link>
	<description>Flex, Flash, AIR, RIA</description>
	<lastBuildDate>Thu, 02 Jun 2011 08:34:45 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.3</generator>
		<item>
		<title>Floating-point errors</title>
		<link>http://www.adnandoric.com/2010/02/07/floating-point-errors/</link>
		<comments>http://www.adnandoric.com/2010/02/07/floating-point-errors/#comments</comments>
		<pubDate>Sun, 07 Feb 2010 19:24:16 +0000</pubDate>
		<dc:creator>adnandoric</dc:creator>
				<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[Blog]]></category>
		<category><![CDATA[floating-point]]></category>
		<category><![CDATA[math]]></category>

		<guid isPermaLink="false">http://www.adnandoric.com/?p=248</guid>
		<description><![CDATA[I needed to code a calculator in Flex but as you all know, programming languages have some accuracy problems with floating points. Thank god I found this sweet little method from Josh Tynjala, it is not perfect but it really helps: /** * Corrects errors caused by floating point math. */ public function correctFloatingPointError&#40;number:Number, precision:int [...]]]></description>
			<content:encoded><![CDATA[<p>I needed to code a calculator in Flex but as you all know, <a title="Wikipedia Floating point" href="http://en.wikipedia.org/wiki/Floating_point#Accuracy_problems" target="_blank">programming languages have some accuracy problems with floating points</a>.</p>
<p>Thank god I found this sweet little method from <a title="JoshBlog" href="http://joshblog.net" target="_blank">Josh Tynjala,</a> it is not perfect but it really helps:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">/**
 * Corrects errors caused by floating point math.
 */</span>
<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> correctFloatingPointError<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">number</span>:<span style="color: #0066CC;">Number</span>, precision:<span style="color: #0066CC;">int</span> = <span style="color: #cc66cc;">5</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Number</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//default returns (10000 * number) / 10000</span>
	<span style="color: #808080; font-style: italic;">//should correct very small floating point errors</span>
	<span style="color: #000000; font-weight: bold;">var</span> correction:<span style="color: #0066CC;">Number</span> = <span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">pow</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">10</span>, precision<span style="color: #66cc66;">&#41;</span>;
	<span style="color: #b1b100;">return</span> <span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">round</span><span style="color: #66cc66;">&#40;</span>correction <span style="color: #66cc66;">*</span> <span style="color: #0066CC;">number</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">/</span> correction;
<span style="color: #66cc66;">&#125;</span>
<span style="color: #808080; font-style: italic;">/**
 * Tests if two numbers are &lt;em&gt;almost&lt;/em&gt; equal.
 */</span>
<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> fuzzyEquals<span style="color: #66cc66;">&#40;</span>number1:<span style="color: #0066CC;">Number</span>, number2:<span style="color: #0066CC;">Number</span>, precision:<span style="color: #0066CC;">int</span> = <span style="color: #cc66cc;">5</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Boolean</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">var</span> difference:<span style="color: #0066CC;">Number</span> = number1 - number2;
	<span style="color: #000000; font-weight: bold;">var</span> range:<span style="color: #0066CC;">Number</span> = <span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">pow</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">10</span>, -precision<span style="color: #66cc66;">&#41;</span>;
	<span style="color: #808080; font-style: italic;">//default check:</span>
	<span style="color: #808080; font-style: italic;">//0.00001  -0.00001</span>
	<span style="color: #b1b100;">return</span> difference  -range;
<span style="color: #66cc66;">&#125;</span>
<span style="color: #808080; font-style: italic;">/*
Copyright (c) 2007 Josh Tynjala
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the &quot;Software&quot;), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED &quot;AS IS&quot;, WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
*/</span></pre></div></div>

<p>For example, try this actionscript code :</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0.08</span> <span style="color: #66cc66;">*</span> <span style="color: #cc66cc;">0.7</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">//0.055999999999999994</span>
<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>correctFloatingPointError<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0.08</span> <span style="color: #66cc66;">*</span> <span style="color: #cc66cc;">0.7</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">//0.056</span></pre></div></div>

<p>Impressive, isn&#8217;t it ? <img src='http://www.adnandoric.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>That&#8217;s it, I hope it helps.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.adnandoric.com/2010/02/07/floating-point-errors/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>VerifyError: Error #1025: An invalid register 3 was accessed</title>
		<link>http://www.adnandoric.com/2009/03/03/verifyerror-error-1025-an-invalid-register-3-was-accessed/</link>
		<comments>http://www.adnandoric.com/2009/03/03/verifyerror-error-1025-an-invalid-register-3-was-accessed/#comments</comments>
		<pubDate>Tue, 03 Mar 2009 19:34:11 +0000</pubDate>
		<dc:creator>adnandoric</dc:creator>
				<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[AIR]]></category>
		<category><![CDATA[Blog]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[1025]]></category>
		<category><![CDATA[error]]></category>
		<category><![CDATA[VerifyError]]></category>

		<guid isPermaLink="false">http://www.adnandoric.com/?p=122</guid>
		<description><![CDATA[Working with XML in Flex or AIR can sometimes be very painfull if you mess with namespaces. I just spent few hours figuring how to solve the «VerifyError» puzzle: VerifyError: Error #1025: An invalid register 1 was accessed. VerifyError: Error #1025: An invalid register 2 was accessed. VerifyError: Error #1025: An invalid register 3 was [...]]]></description>
			<content:encoded><![CDATA[<p>Working with XML in Flex or AIR can sometimes be very painfull if you mess with namespaces.<br />
I just spent few hours figuring how to solve the «VerifyError» puzzle:</p>
<p><code>VerifyError: Error #1025: An invalid register 1 was accessed.<br />
VerifyError: Error #1025: An invalid register 2 was accessed.<br />
VerifyError: Error #1025: An invalid register 3 was accessed.<br />
VerifyError: Error #1025: An invalid register 4 was accessed.<br />
VerifyError: Error #1025: An invalid register 5 was accessed.</code></p>
<p>I was loading some fancy XML spitted by REST backend and had to set the default xml namespace in a function scope in order to access it:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">default</span> <span style="color: #0066CC;">xml</span> namespace = <span style="color: #0066CC;">xml</span>.<span style="color: #006600;">namespace</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>Don&#8217;t ask me why, but you have to «reset it» when you finish manipulating the XML otherwise you wont be able to call functions in the same scope:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">default</span> <span style="color: #0066CC;">xml</span> namespace = <span style="color: #000000; font-weight: bold;">new</span> Namespace<span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">&quot;&quot;</span> <span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<div id="attachment_128" class="wp-caption aligncenter" style="width: 509px"><img class="size-full wp-image-128" title="zoidberg-why" src="http://www.adnandoric.com/wp-content/uploads/2009/03/zoidberg-why.png" alt="Zoidberg: Why? Why? Why?" width="499" height="301" /><p class="wp-caption-text">Zoidberg: Why? Why? Why?</p></div>
<p>I hope that helps  <img src='http://www.adnandoric.com/wp-includes/images/smilies/icon_cool.gif' alt='8-)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.adnandoric.com/2009/03/03/verifyerror-error-1025-an-invalid-register-3-was-accessed/feed/</wfw:commentRss>
		<slash:comments>18</slash:comments>
		</item>
		<item>
		<title>Understanding the Flex Event propagation</title>
		<link>http://www.adnandoric.com/2008/12/29/understanding-the-flex-event-propagation/</link>
		<comments>http://www.adnandoric.com/2008/12/29/understanding-the-flex-event-propagation/#comments</comments>
		<pubDate>Mon, 29 Dec 2008 19:44:56 +0000</pubDate>
		<dc:creator>adnandoric</dc:creator>
				<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[Blog]]></category>
		<category><![CDATA[Flash Player]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[bubbling]]></category>
		<category><![CDATA[capturing]]></category>
		<category><![CDATA[event flow]]></category>
		<category><![CDATA[events]]></category>
		<category><![CDATA[itemRenderer]]></category>
		<category><![CDATA[propagation]]></category>
		<category><![CDATA[targeting]]></category>

		<guid isPermaLink="false">http://www.adnandoric.com/?p=40</guid>
		<description><![CDATA[Everybody knows that ActionScript is an event based language and that dispatching and listening events are the base of Flex development, so I&#8217;ll not emphasize it here. I will talk about something else, something more intriguing: event propagation. Most components communicate with others using events which conveys useful informations and data but only visual components [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">Everybody knows that ActionScript is an event based language and that dispatching and listening events are the base of Flex development, so I&#8217;ll not emphasize it here. I will talk about something else, something more intriguing: <strong>event propagation</strong>. Most components communicate with others using events which conveys useful informations and data but only visual components (objects in the display list) can participate in what is called &#8220;<strong>event flow</strong>&#8220;. The event flow describes how an event object propagates through the display list in three phases: capturing, targeting and bubbling. Non-visual components are only concerned by what is called targeting phase.</p>
<p style="text-align: justify;">Event propagation mechanism is rather counter-intuitive and often misunderstood by new Flex developers. I&#8217;ll try to explain some of the key principles that you should keep in mind when working with events.</p>
<p><span id="more-40"></span></p>
<p style="text-align: justify;">The Flex event model is based on (a not so perfect implementation of) the <a title="DOM Level 3 Events" href="http://www.w3.org/TR/DOM-Level-3-Events/events.html" target="_blank">Document Object Model (DOM) Level 3 events model</a>. Visit the <a title="DOM Level 3 Events" href="http://www.w3.org/TR/DOM-Level-3-Events/events.html" target="_blank">w3 site</a> for more informations on the subject.</p>
<p>Take a look at the EventPropagation application :</p>
<div id="attachment_41" class="wp-caption aligncenter" style="width: 394px"><a title="EventPropagation.html" href="http://www.adnandoric.com/wp-content/uploads/2008/12/flex/EventPropagation/EventPropagation.html" target="_blank"><img class="size-full wp-image-41" title="event_propagation" src="http://www.adnandoric.com/wp-content/uploads/2008/12/event_propagation.jpg" alt="Event Propagation" width="384" height="310" /></a><p class="wp-caption-text">Event Propagation</p></div>
<ul>
<li><a title="EventPropagation.html" href="http://www.adnandoric.com/wp-content/uploads/2008/12/flex/EventPropagation/EventPropagation.html" target="_blank">launch application</a></li>
<li><a title="EventPropagation source" href="http://www.adnandoric.com/wp-content/uploads/2008/12/flex/EventPropagation/srcview/index.html" target="_blank">view source</a></li>
</ul>
<p style="text-align: justify;">As you can see, the blueBox (Vbox) is child of the EventPropagation (Application), the redBox (Vbox) is child of the blueBox and finally, &#8220;Click me!&#8221; button (Button) is child of the redBox :</p>
<pre>EventPropagation &gt; blueBox &gt; redBox &gt; button</pre>
<p style="text-align: justify;">There is something else that you can&#8217;t see in the application, but that is very important to know: Application has one parent, mx.managers.SystemManager, who has also one parent: flash.display.Stage (display list root). Both of them are DisplayObjects in the display list and thus participate in event flow. The final chain is as follows :</p>
<pre>Stage &gt; SystemManager &gt; EventPropagation &gt; blueBox &gt; redBox &gt; button</pre>
<p style="text-align: justify;">When you click on the &#8220;Click me!&#8221; button, the flash player initiates the event flow starting from the root element of the display list, Stage, goes down to the event dispatcher (Button) then it returns up again to the stage. It visits all of display objects between the root object and the event dispatcher, looking for registered MouseEvent.CLICK listeners in three phases.</p>
<p style="text-align: justify;">If the &#8220;useCapture&#8221; CheckBox is checked, six event listeners are set for all display objects in our chain with useCapture property set to true, that means that listeners will handle only the capturing phase. Because of that behavior, useCapture should be named useCaptureOnly in my opinion.</p>
<p>Example:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">this</span>.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span> MouseEvent.<span style="color: #006600;">CLICK</span>, onClick, <span style="color: #000000; font-weight: bold;">true</span> <span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p style="text-align: justify;">If the CheckBox  is unchecked, all event listeners are removed then re-added with useCapture set to false (default behavior) thus the targeting and bubbling phases are monitored and the capturing phase is skipped.</p>
<p style="text-align: justify;">note: To monitor all three phases, event listeners need to be registered with useCapture set to false AND true.</p>
<p>Example:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">this</span>.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span> MouseEvent.<span style="color: #006600;">CLICK</span>, onClick <span style="color: #66cc66;">&#41;</span>;
<span style="color: #0066CC;">this</span>.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span> MouseEvent.<span style="color: #006600;">CLICK</span>, onClick, <span style="color: #000000; font-weight: bold;">true</span> <span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p style="text-align: justify;">The status TextArea will display information about event propagation as it gathers it. Click on the button, the colored boxes or the application, with useCapture enabled or disabled and observe the status zone.</p>
<p style="text-align: justify;">This image shows aforementioned three distinct event flow phases: capturing, targeting and bubbling:</p>
<div id="attachment_42" class="wp-caption aligncenter" style="width: 410px"><img class="size-full wp-image-42" title="event_flow" src="http://www.adnandoric.com/wp-content/uploads/2008/12/event_flow.jpg" alt="Event flow" width="400" height="400" /><p class="wp-caption-text">Event flow</p></div>
<h4>Capturing Phase</h4>
<pre>Stage &gt; SystemManager &gt; EventPropagation &gt; blueBox &gt; redBox</pre>
<p style="text-align: justify;">If capturing is enabled, each item in the display list tree, starting from the root ancestor flash.display.Stage and going down the display list to the direct ancestor of the event dispatcher (target), is checked for event listeners. If one of those display objects is registered as an event listener for that specific triggered event, flash player creates the event object and pass it to the listener so it can handle it (event object is created when needed, not before, furthermore, once created its properties are changed as it propagates, no new event object is created). Event listeners registered for this phase can handle the event before it reaches its target.</p>
<h4>Targeting Phase</h4>
<pre>&gt; button &gt;</pre>
<p style="text-align: justify;">During this phase, the event object has reached the event&#8217;s target and its listeners are invoked (if any). Event&#8217;s currentTarget and target properties are the same at this very moment.</p>
<h4>Bubbling Phase</h4>
<pre>redBox &gt; blueBox &gt; EventPropagation &gt; SystemManager &gt; Stage</pre>
<p style="text-align: justify;">In bubbling phase, if bubbling is enabled (MouseEvent bubbles by default), events are emerging like Champagne bubbles on the surface taking the exact opposite path from capturing phase examining all event dispatcher&#8217;s ancestors starting from it&#8217;s direct parent and continuing up the display list to the flash.display.Stage root. Event listeners registered for this phase can handle the event after it has reached its target.</p>
<h3>Why should I care ?</h3>
<p style="text-align: justify;">Most of the time, we don&#8217;t have to care about all this stuff. All that matters is the targeting phase: we add the listener for the event on the target dispatching object and that&#8217;s it ! But sometimes you will be confronted to a situation where you will need to intercept and stop the propagation of an event before it even reaches the dispatching object ( it sounds weird I know <img src='http://www.adnandoric.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  ).</p>
<h3>Stopping the propagation</h3>
<p style="text-align: justify;">If an object is listening for this particular event, it can intercept it and stop its propagation if needed at any given moment in any phase. Two methods: stopPropagation() prevents the event to propagate after the current listener was executed, stopImmediatePropagation() prevents even the current object&#8217;s remaining listeners to execute.</p>
<p style="text-align: justify;">An obvious example would be an itemRenderer in a List-based component. Maybe you don&#8217;t want to select the item in the list when clicking it, but still want to execute some logic inside the renderer ?</p>
<p style="text-align: justify;">In the following example, the MouseEvent.DOWN is captured inside the itemRenderer and its propagation is stopped during the bubbling phase so it&#8217;s containing List will not be notified thus the related item will not be selected.</p>
<p>Example:<br />
<p><a href="http://www.adnandoric.com/2008/12/29/understanding-the-flex-event-propagation/"><em>Click here to view the embedded video.</em></a></p></p>
<h3>Prioritize Events</h3>
<p style="text-align: justify;">If several listeners are listening a same event on a dispatcher, it is possible to define priority for each of them and thus control the order they are executed. Higher the priority, sooner the listener is triggered.</p>
<p>Example:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">this</span>.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span> MouseEvent.<span style="color: #006600;">CLICK</span>, onClick3, <span style="color: #000000; font-weight: bold;">false</span>, -<span style="color: #cc66cc;">8</span> <span style="color: #66cc66;">&#41;</span>;
<span style="color: #0066CC;">this</span>.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span> MouseEvent.<span style="color: #006600;">CLICK</span>, onClick1, <span style="color: #000000; font-weight: bold;">false</span>, <span style="color: #cc66cc;">4</span> <span style="color: #66cc66;">&#41;</span>;
<span style="color: #0066CC;">this</span>.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span> MouseEvent.<span style="color: #006600;">CLICK</span>, onClick2, <span style="color: #000000; font-weight: bold;">false</span>, <span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p style="text-align: justify;">In this example, the handlers will be called in this order : onClick1, onClick2, onClick3.</p>
<p>That&#8217;s all folks !</p>
]]></content:encoded>
			<wfw:commentRss>http://www.adnandoric.com/2008/12/29/understanding-the-flex-event-propagation/feed/</wfw:commentRss>
		<slash:comments>22</slash:comments>
		</item>
	</channel>
</rss>

