<?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>DaveOnCode &#187; actionscript</title>
	<atom:link href="http://www.daveoncode.com/category/actionscript/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.daveoncode.com</link>
	<description>coder and technology lover</description>
	<lastBuildDate>Sun, 16 May 2010 16:55:18 +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>Get hours, minutes and seconds from a number without maths operations</title>
		<link>http://www.daveoncode.com/2010/02/05/get-hours-minutes-and-seconds-from-number-without-maths-operations/</link>
		<comments>http://www.daveoncode.com/2010/02/05/get-hours-minutes-and-seconds-from-number-without-maths-operations/#comments</comments>
		<pubDate>Fri, 05 Feb 2010 15:09:11 +0000</pubDate>
		<dc:creator>Davide Zanotti</dc:creator>
				<category><![CDATA[actionscript]]></category>
		<category><![CDATA[flex]]></category>

		<guid isPermaLink="false">http://www.daveoncode.com/?p=589</guid>
		<description><![CDATA[Today I&#8217;m gonna show you a secret ninja technique to extract hours, minutes and seconds from a number (representing an amount of seconds). In my example I will show an Actionscript code, but this can be implemented in JavaScript and maybe other languages too. So, the scenario is the following: we have a number representing]]></description>
			<content:encoded><![CDATA[<p>Today I&#8217;m gonna show you a secret ninja technique to extract hours, minutes and seconds from a number (representing an amount of seconds). In my example I will show an Actionscript code, but this can be implemented in JavaScript and maybe other languages too.<br />
So, the scenario is the following: we have a number representing seconds and we want to know how many hours this amount of seconds contains, how many minutes and how many remaining seconds. We know that a minute is composed by 60 seconds and an hour by 60 minutes and we could write a series of maths operations in order to accomplish our objective, but there is a way far simple and fast: use the Date class!<br />
Date class already implements all the methods we need:</p>
<ul>
<li><strong>getHours()</strong></li>
<li><strong>getMinutes()</strong></li>
<li><strong>getSeconds()</strong></li>
</ul>
<p>So, in order to take advantage of these useful methods, all we have to do is initialize a &#8220;fake&#8221; date using the amount of seconds we want to &#8220;split&#8221; into hours, minutes and seconds. The Date class has several OPTIONAL arguments that can be specified during its initializations, these are:</p>
<ul>
<li><strong>year</strong></li>
<li><strong>month</strong></li>
<li><strong>date</strong> (day number)</li>
<li><strong>hours</strong></li>
<li><strong>minutes</strong></li>
<li><strong>seconds</strong></li>
<li><strong>milliseconds</strong></li>
</ul>
<p>Because theme all are optional, we can create a date object by specifying only the know arguments (in our case seconds) and by assigning null or zero to the others:</p>
<div class="codecolorer-container actionscript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="actionscript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">date</span>:<span style="color: #0066CC;">Date</span> = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Date</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">null</span>, <span style="color: #000000; font-weight: bold;">null</span>, <span style="color: #000000; font-weight: bold;">null</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">9137</span><span style="color: #66cc66;">&#41;</span>;</div></td></tr></tbody></table></div>
<p>Then by calling getHours, getMinutes and getSeconds we will obtain what we expect:</p>
<div class="codecolorer-container actionscript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br /></div></td><td><div class="actionscript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;hours: &quot;</span>, <span style="color: #0066CC;">date</span>.<span style="color: #0066CC;">getHours</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// 2 hours</span><br />
<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;minutes: &quot;</span>, <span style="color: #0066CC;">date</span>.<span style="color: #0066CC;">getMinutes</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// 32 minutes</span><br />
<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;seconds: &quot;</span>, <span style="color: #0066CC;">date</span>.<span style="color: #0066CC;">getSeconds</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// 17 seconds</span></div></td></tr></tbody></table></div>
<p>Not sure about the result? Let&#8217;s test it:</p>
<p>60 * 60 * 2 = <strong>7200</strong>; (seconds contained in 2 hours)<br />
32 * 60 = <strong>1920</strong>; (seconds contained in 32 minutes)<br />
7200 + 1920 + 17 = <strong>9137</strong>; (original seconds!)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.daveoncode.com/2010/02/05/get-hours-minutes-and-seconds-from-number-without-maths-operations/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Documenting Actionscript classes which make use of AIR framework</title>
		<link>http://www.daveoncode.com/2009/05/04/documenting-actionscript-classes-which-make-use-of-air-framework/</link>
		<comments>http://www.daveoncode.com/2009/05/04/documenting-actionscript-classes-which-make-use-of-air-framework/#comments</comments>
		<pubDate>Mon, 04 May 2009 21:57:09 +0000</pubDate>
		<dc:creator>Davide Zanotti</dc:creator>
				<category><![CDATA[actionscript]]></category>
		<category><![CDATA[air]]></category>
		<category><![CDATA[asdoc]]></category>
		<category><![CDATA[documentation]]></category>

		<guid isPermaLink="false">http://www.daveoncode.com/?p=392</guid>
		<description><![CDATA[This is just a quick post that may (I hope) help who are going to use asdoc tool to generate Actionscript documentation for AS classes which use AIR framework. Problem: You want to generate documentation for your package(s), but when you type asdoc command in the terminal you get a lot of errors such: Type]]></description>
			<content:encoded><![CDATA[<p>This is just a quick post that may (I hope) help who are going to use asdoc tool to generate Actionscript documentation for AS classes which use AIR framework.</p>
<p><strong>Problem:</strong></p>
<p>You want to generate documentation for your package(s), but when you type asdoc command in the terminal you get a lot of errors such:</p>
<p><em>Type was not found or was not a compile-time constant: File.</em><br />
<em>Type was not found or was not a compile-time constant: Vector</em></p>
<p>&#8230;and so on</p>
<p><strong>Solution:</strong></p>
<p>You have to use <strong>-external-library-path</strong> option to include AIR swc, because AIR classes are located into external swc apart from Flex framework.<br />
These files are under /sdks/{yourSDKVersion}/frameworks/libs/air/ (where &#8220;{yourSDKVersion}&#8221; is the version of Flex SDK you are using to deploy your applications) and are the following five:</p>
<ul>
<li>airframework.swc</li>
<li>airglobal.swc</li>
<li>applicationupdater_ui.swc</li>
<li>applicationupdater.swc</li>
<li>servicemonitor.swc</li>
</ul>
<p>The first 2 should be imported with <strong>-external-library-path</strong> option anytime you use AIR classes, the others are necessary only if you are using the update framework and/or serivicemonitor.</p>
<p>The following is an example of how to use the asdoc command with the option discussed:</p>
<p><em><br />
./asdoc -source-path=/Users/davidezanotti/Documents/workspace/myproject/src/ -external-library-path=/Applications/Adobe\ Flex\ Builder\ 3\ Plug-in/sdks/3.3.0.4589/frameworks/libs/air/airframework.swc,/Applications/Adobe\ Flex\ Builder\ 3\ Plug-in/sdks/3.3.0.4589/frameworks/libs/air/airglobal.swc -doc-sources=/Users/davidezanotti/Documents/workspace/myproject/src/ -output=/Users/davidezanotti/Desktop/doc</em>
</p>
<p>The code above will succesfull generate documentation for all classes under the folder &#8220;/Users/davidezanotti/Documents/workspace/myproject/src/&#8221; to the &#8220;doc&#8221; folder on the desktop.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.daveoncode.com/2009/05/04/documenting-actionscript-classes-which-make-use-of-air-framework/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Actionscript Vector class initialization with a source Array</title>
		<link>http://www.daveoncode.com/2009/04/06/actionscript-vector-class-initialization-with-a-source-array/</link>
		<comments>http://www.daveoncode.com/2009/04/06/actionscript-vector-class-initialization-with-a-source-array/#comments</comments>
		<pubDate>Mon, 06 Apr 2009 15:35:29 +0000</pubDate>
		<dc:creator>Davide Zanotti</dc:creator>
				<category><![CDATA[actionscript]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[collection]]></category>
		<category><![CDATA[flash]]></category>
		<category><![CDATA[vector]]></category>

		<guid isPermaLink="false">http://www.daveoncode.com/?p=367</guid>
		<description><![CDATA[What you maybe already know: Vector class is a powerful class introduced with flash player 10. It is fundamentally a special typed array into which all elements must be the same type and it is faster than a normal Array. What you maybe don&#8217;t know: It&#8217;s possible to initialize a Vector with an Array or]]></description>
			<content:encoded><![CDATA[<p><strong>What you maybe already know:</strong></p>
<p>Vector class is a powerful class introduced with flash player 10. It is fundamentally a special typed array into which all elements must be the same type and it is faster than a normal Array.</p>
<p><strong>What you maybe don&#8217;t know:</strong></p>
<p>It&#8217;s possible to initialize a Vector with an Array or another Vector as a source but you must use the Vector function not the class constructor. The class constructor in fact has the following signature:</p>
<div class="codecolorer-container actionscript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="actionscript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Vector<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">length</span>:uint = <span style="color: #cc66cc;">0</span>, fixed:<span style="color: #0066CC;">Boolean</span> = <span style="color: #000000; font-weight: bold;">false</span><span style="color: #66cc66;">&#41;</span></div></td></tr></tbody></table></div>
<p>Vector function instead has this:</p>
<div class="codecolorer-container actionscript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="actionscript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Vector<span style="color: #66cc66;">&#40;</span>sourceArray:<span style="color: #0066CC;">Object</span><span style="color: #66cc66;">&#41;</span>:Vector.<span style="color: #66cc66;">&lt;</span>T<span style="color: #66cc66;">&gt;</span></div></td></tr></tbody></table></div>
<p>So, rather than using such approach:</p>
<div class="codecolorer-container actionscript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br /></div></td><td><div class="actionscript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">var</span> vector:Vector.<span style="color: #66cc66;">&lt;</span>String<span style="color: #66cc66;">&gt;</span> = <span style="color: #000000; font-weight: bold;">new</span> Vector.<span style="color: #66cc66;">&lt;</span>String<span style="color: #66cc66;">&gt;</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
vector<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span> = <span style="color: #ff0000;">&quot;value1&quot;</span>;<br />
vector<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span> = <span style="color: #ff0000;">&quot;value2&quot;</span>;<br />
vector<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#93;</span> = <span style="color: #ff0000;">&quot;value3&quot;</span>;<br />
<span style="color: #808080; font-style: italic;">// ...and so on</span></div></td></tr></tbody></table></div>
<p>We can do the following (Notice the absence of &#8220;this&#8221; keyword):</p>
<div class="codecolorer-container actionscript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br /></div></td><td><div class="actionscript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">// create a vector with an inline Array as source</span><br />
<span style="color: #000000; font-weight: bold;">var</span> vector:Vector.<span style="color: #66cc66;">&lt;</span>String<span style="color: #66cc66;">&gt;</span> = Vector.<span style="color: #66cc66;">&lt;</span>String<span style="color: #66cc66;">&gt;</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;v1&quot;</span>, <span style="color: #ff0000;">&quot;v2&quot;</span>, <span style="color: #ff0000;">&quot;v3&quot;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>;<br />
<br />
<span style="color: #808080; font-style: italic;">// create a vector with an inline Vector as source</span><br />
<span style="color: #000000; font-weight: bold;">var</span> vector2:Vector.<span style="color: #66cc66;">&lt;</span>String<span style="color: #66cc66;">&gt;</span> = Vector.<span style="color: #66cc66;">&lt;</span>String<span style="color: #66cc66;">&gt;</span><span style="color: #66cc66;">&#40;</span>vector<span style="color: #66cc66;">&#41;</span>;<br />
<br />
<span style="color: #808080; font-style: italic;">// Testing (both trace will print the same string)</span><br />
<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>vector<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>vector2<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>;</div></td></tr></tbody></table></div>
]]></content:encoded>
			<wfw:commentRss>http://www.daveoncode.com/2009/04/06/actionscript-vector-class-initialization-with-a-source-array/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Learning design patterns with Actionscript 3: episode 2 &#8211; Decorator pattern</title>
		<link>http://www.daveoncode.com/2009/03/30/learning-design-patterns-with-actionscript-3-episode-2-decorator-pattern/</link>
		<comments>http://www.daveoncode.com/2009/03/30/learning-design-patterns-with-actionscript-3-episode-2-decorator-pattern/#comments</comments>
		<pubDate>Mon, 30 Mar 2009 09:21:26 +0000</pubDate>
		<dc:creator>Davide Zanotti</dc:creator>
				<category><![CDATA[actionscript]]></category>
		<category><![CDATA[design patterns]]></category>

		<guid isPermaLink="false">http://www.daveoncode.com/?p=354</guid>
		<description><![CDATA[The objective of the decorator pattern is to provide a way to add extra features and responsibilities to a class at runtime without to subclass it. The classes diagram of this pattern is composed by an interface or an abstract class which will define the supertype of the class that will be extended (decorated) by]]></description>
			<content:encoded><![CDATA[<p>The objective of the decorator pattern is to provide a way to add extra features and responsibilities to a class at runtime without to subclass it.</p>
<p>The classes diagram of this pattern is composed by an interface or an abstract class which will define the supertype of the class that will be extended (decorated) by one or more classes implementing the same interface and extending an abstract class which identify these as decorator classes.</p>
<p><span id="more-354"></span></p>
<p>A real scenario into which apply this pattern can be a cars store. A car is the base class and the accessories such dvd player, gps, air conditioning and so on, can be thought as decorators. Once decorated, the car (which has a method getPrice() which returns its cost) will get updated by accessories, so calling the getPrice() method will include the price of the “naked” car plus the price of the various accessories installed.</p>
<p>Let’s take a look to the classes…</p>
<div class="codecolorer-container actionscript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br /></div></td><td><div class="actionscript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">package com.<span style="color: #006600;">mycarstore</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #0066CC;">public</span> <span style="color: #0066CC;">interface</span> IVehicle <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">function</span> getPrice<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Number</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">function</span> getDescription<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">String</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; <br />
<span style="color: #66cc66;">&#125;</span></div></td></tr></tbody></table></div>
<p>The interface above will be implemented by all the classes and we will treat the car as an IVehicle not a concrete Car class.<br />
This is our simple Car class:</p>
<div class="codecolorer-container actionscript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br /></div></td><td><div class="actionscript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">package com.<span style="color: #006600;">mycarstore</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Car <span style="color: #0066CC;">implements</span> IVehicle <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Car<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getPrice<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Number</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #cc66cc;">50000</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getDescription<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">String</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #ff0000;">&quot;Base car&quot;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; <br />
<span style="color: #66cc66;">&#125;</span></div></td></tr></tbody></table></div>
<p>Now is time to see the decorators (in our scenario the accessories). This is the abstract Accessory class, all accessories will extend it:</p>
<div class="codecolorer-container actionscript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br /></div></td><td><div class="actionscript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">package com.<span style="color: #006600;">mycarstore</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Accessory <span style="color: #0066CC;">implements</span> IVehicle <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; protected <span style="color: #000000; font-weight: bold;">var</span> _vehicle:IVehicle;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Accessory<span style="color: #66cc66;">&#40;</span>vehicle:IVehicle<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">this</span>._vehicle = vehicle;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getPrice<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Number</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #0066CC;">this</span>._vehicle.<span style="color: #006600;">getPrice</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getDescription<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">String</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #0066CC;">this</span>._vehicle.<span style="color: #006600;">getDescription</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; <br />
<span style="color: #66cc66;">&#125;</span></div></td></tr></tbody></table></div>
<p>In Accessory we can notice that the class encapsulate an object of IVehicle which is passed to the constructor (of course since this class is abstract the object will be passed to the constructor of Accessory&#8217;s subclasses). This is the key concept of decorator pattern, all decorators keep a reference of the interface they are going to decorate and this reference is used as the base for methods business logic. Let&#8217;s see how:</p>
<div class="codecolorer-container actionscript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br /></div></td><td><div class="actionscript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">package com.<span style="color: #006600;">mycarstore</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> DVDPlayer <span style="color: #0066CC;">extends</span> Accessory <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> DVDPlayer<span style="color: #66cc66;">&#40;</span>vehicle:IVehicle<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">super</span><span style="color: #66cc66;">&#40;</span>vehicle<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; override <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getPrice<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Number</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #0066CC;">this</span>._vehicle.<span style="color: #006600;">getPrice</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> + <span style="color: #cc66cc;">550</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; override <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getDescription<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">String</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #0066CC;">this</span>._vehicle.<span style="color: #006600;">getDescription</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> + <span style="color: #ff0000;">&quot;, DVD Player&quot;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; <br />
<span style="color: #66cc66;">&#125;</span></div></td></tr></tbody></table></div>
<p>DVDPlayer is a concrete decorator and it uses the encapsulated IVehicle object in order to provide an extended version of the interface methods (getPrice() and getDescription()). Basically it uses first the reference methods and then it adds some other values.</p>
<p>To use the pattern we will first instantiate a Car object, than we will wrap it with one or more decorators:</p>
<div class="codecolorer-container actionscript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br /></div></td><td><div class="actionscript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">var</span> car:IVehicle = <span style="color: #000000; font-weight: bold;">new</span> Car<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Price: &quot;</span> + <span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#40;</span>car.<span style="color: #006600;">getPrice</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> + <span style="color: #ff0000;">&quot; - description: &quot;</span> + <span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#40;</span>car.<span style="color: #006600;">getDescription</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
car = <span style="color: #000000; font-weight: bold;">new</span> DVDPlayer<span style="color: #66cc66;">&#40;</span>car<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Price: &quot;</span> + <span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#40;</span>car.<span style="color: #006600;">getPrice</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> + <span style="color: #ff0000;">&quot; - description: &quot;</span> + <span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#40;</span>car.<span style="color: #006600;">getDescription</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
car = <span style="color: #000000; font-weight: bold;">new</span> GPSSystem<span style="color: #66cc66;">&#40;</span>car<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Price: &quot;</span> + <span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#40;</span>car.<span style="color: #006600;">getPrice</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> + <span style="color: #ff0000;">&quot; - description: &quot;</span> + <span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#40;</span>car.<span style="color: #006600;">getDescription</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</div></td></tr></tbody></table></div>
<p>The output will be:</p>
<pre>
Price: 50000 - description: Base car
Price: 50550 - description: Base car, DVD Player
Price: 50875 - description: Base car, DVD Player, GPS System
</pre>
<p>The order into which we wrap the concrete component (Car) is indifferent (we can add GPSSystem before or after DVDPlayer) and of course despite its readability, we can wrap the Car in a single statement:</p>
<div class="codecolorer-container actionscript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="actionscript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">var</span> car2:IVehicle = <span style="color: #000000; font-weight: bold;">new</span> GPSSystem<span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> DVDPlayer<span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Car<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</div></td></tr></tbody></table></div>
<p>In this example I used very simple classes, however in a real implementation we can create more sophisticated decorators which will accept extra arguments beyond the IVehicle reference.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.daveoncode.com/2009/03/30/learning-design-patterns-with-actionscript-3-episode-2-decorator-pattern/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>parseInt(): difference between Javascript and Actionscript</title>
		<link>http://www.daveoncode.com/2009/03/06/parseint-difference-between-javascript-and-actionscript/</link>
		<comments>http://www.daveoncode.com/2009/03/06/parseint-difference-between-javascript-and-actionscript/#comments</comments>
		<pubDate>Fri, 06 Mar 2009 15:27:06 +0000</pubDate>
		<dc:creator>Davide Zanotti</dc:creator>
				<category><![CDATA[actionscript]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://www.daveoncode.com/?p=334</guid>
		<description><![CDATA[Today is the day of the little big discoveries. I realized that the parseInt() function, which objective is the same in both languages (Actionscript and Javascript), is a bit different between them. In Actionscript the function is based on base 10 numeration, in Javascript instead the funny thing is that it try to guess the]]></description>
			<content:encoded><![CDATA[<p>Today is the day of the little big discoveries. I realized that the parseInt() function, which objective is the same in both languages (Actionscript and Javascript), is a bit different between them. In Actionscript the function is based on base 10 numeration, in Javascript instead the funny thing is that it try to guess the radix by analyzing the string received as first argument, so if you want to be sure of the result returned you have to explicitly pass the radix in the second argument.</p>
<p>Example:</p>
<pre>var myString = "000123";
alert(parseInt(myString));</pre>
<p>The code above will alert <strong>83</strong> instead of <strong>123</strong> (because the default base guessed by Javascript isn&#8217;t 10 like in Actionscript), to get <strong>123</strong> the code must be:</p>
<pre>var myString = "000123";
alert(parseInt(myString, 10));</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.daveoncode.com/2009/03/06/parseint-difference-between-javascript-and-actionscript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Converting Java to Actionscript&#8230; maybe is not so hard (I hope)</title>
		<link>http://www.daveoncode.com/2009/02/12/converting-java-to-actionscript-maybe-is-not-so-hard-i-hope/</link>
		<comments>http://www.daveoncode.com/2009/02/12/converting-java-to-actionscript-maybe-is-not-so-hard-i-hope/#comments</comments>
		<pubDate>Thu, 12 Feb 2009 12:06:19 +0000</pubDate>
		<dc:creator>Davide Zanotti</dc:creator>
				<category><![CDATA[actionscript]]></category>
		<category><![CDATA[code conversion]]></category>
		<category><![CDATA[collection]]></category>
		<category><![CDATA[framework]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[porting]]></category>

		<guid isPermaLink="false">http://www.daveoncode.com/?p=294</guid>
		<description><![CDATA[After valuate my own realization of a certain library, I opted to try the porting of an already existent one&#8230; which is made in Java. This library has a lot of classes and interfaces and the manually translation would be very time-expensive, considering also that it uses a lot of classes and functions which are]]></description>
			<content:encoded><![CDATA[<p>After valuate my own realization of a certain library, I opted to try the porting of an already existent one&#8230; which is made in Java. This library has a lot of classes and interfaces and the manually translation would be very time-expensive, considering also that it uses a lot of classes and functions which are unavailable in Actionscript 3. Fortunately I found a small and useful AIR application which automatically converts .java files to .as files (the application is here: <a href="http://thunderhead.esri.com/readonlyurl/J2AS3.air">http://thunderhead.esri.com/readonlyurl/J2AS3.air</a>), this application anyway helps a lot but many extra-work is required to properly convert the classes&#8230; for example Actionscript has not the <em><strong>abstract</strong></em> key, variable can&#8217;t be market as <em><strong>final</strong></em> and so on, so we have to manually adjust these issues. Another useful stuff I found is an Actionscript library which aim is to provide to AS3 developers the same (more or less) features and power of Java Collection framework, which provide classes like HashMap, ArrayList, LinkedList and so on (the library can be found here: <a href="http://code.google.com/p/addicted2flash/" target="_blank">http://code.google.com/p/addicted2flash/</a>). Despite these helps I&#8217;m still facing some incompatibility issues&#8230; classes/methods AS3 is missing, but implementing them by myself is quite simple. For example I implemented the Java&#8217;s System.arraycopy() in 2 minutes, by following the <a href="http://java.sun.com/j2se/1.4.2/docs/api/java/lang/System.html#arraycopy(java.lang.Object,%20int,%20java.lang.Object,%20int,%20int" target="_blank">JavaDoc</a> ).</p>
<p>In conclusion Actionscript and Java have a  lot of stuff in common, the syntax and structure is quite the same, except for some issues like abstract classes and methods, multiple methods and constructors declaration (override). The main difference is that Java provides an huge number of Classes that Actionscript doesn&#8217;t, however it seems not so hard to translate Java code and implement Java functionality in Actionscript.</p>
<p>I hope I won&#8217;t have to deny my words :)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.daveoncode.com/2009/02/12/converting-java-to-actionscript-maybe-is-not-so-hard-i-hope/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using bitwise operators with Actionscript to create arguments flags</title>
		<link>http://www.daveoncode.com/2009/02/08/using-bitwise-operators-with-actionscript-to-create-arguments-flags/</link>
		<comments>http://www.daveoncode.com/2009/02/08/using-bitwise-operators-with-actionscript-to-create-arguments-flags/#comments</comments>
		<pubDate>Sun, 08 Feb 2009 17:10:01 +0000</pubDate>
		<dc:creator>Davide Zanotti</dc:creator>
				<category><![CDATA[actionscript]]></category>
		<category><![CDATA[as3]]></category>
		<category><![CDATA[bitwise]]></category>
		<category><![CDATA[flags]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://www.daveoncode.com/?p=269</guid>
		<description><![CDATA[Among all books I bought (including Java books), I haven&#8217;t one which explain in depth and in a clear manner what bitwise operators are and how to use them. Almost all books has at least a page about the argument but none is able to give an exhaustive explanation. So, after searching the net, I]]></description>
			<content:encoded><![CDATA[<p>Among all books I bought (including Java books), I haven&#8217;t one which explain in depth and in a clear manner what bitwise operators are and how to use them. Almost all books has at least a page about the argument but none is able to give an exhaustive explanation. So, after searching the net, I found an extreme useful article by Joseph Farrell on gamedev.net (direct link: http://www.gamedev.net/reference/articles/article1563.asp), which explains bitwise operators in C starting from an introduction to Numbers and number system. The article is valid to Actionscript, Java and other languages too. I started to try to understand bitwise operators, after looked to Flex&#8217;s Alert class, which can accept several options (different buttons) as an unique argument (<strong><em>flags</em></strong>). This can be accomplished by using the bitwise OR operator |  (a pipe), which &#8220;joins&#8221; together different constants:</p>
<p><span id="more-269"></span></p>
<pre>Alert.show("Alert message", "Alert title", Alert.OK | Alert.NO | Alert.CANCEL);</pre>
<p>The code above generates an alert with 3 buttons (ok, no and cancel), but how it works exactly? Well, I don&#8217;t want to rewrite the article I read, but I would like to synthesize it and focus on an Actionscript usage, in order to give the basic information required to create custom classes which will accept multiple parameters an unique argument. So&#8230; what Alert.OK, Alert.NO and so on are? They are basically numbers (uint), and the result of the bitwise or operator still returns an integer, so the Alert class will receives a number as the third argument. The | operator, in practice produces a &#8220;binary sum&#8221; of the given numbers. A good way I figured out to understand what is going on when using that operator is to trace the binary representation of every number passed and then the result after the bitwise operation. To convert a &#8220;normal number&#8221; (base 10) into a binary one (base 2&#8230; only 1 or 0 sequence), we can use the <strong><em>toString()</em></strong> method by passing 2 as the radix argument:</p>
<pre>
var myNumber:uint = 3560;
trace(myNumber.toString(2));
</pre>
<p>The code above will trace: 110111101000.</p>
<p>So, let&#8217;s create a dummy class to do some tests:</p>
<pre>package {

    public class BitwiseTest {

        public static const FLAG_1:uint = 1;
        public static const FLAG_2:uint = 2;
        public static const FLAG_3:uint = 4;
        public static const FLAG_4:uint = 8;
        public static const FLAG_5:uint = 16;
        public static const FLAG_6:uint = 32;

        public function BitwiseTest() {

            trace("BitwiseTest.FLAG_1: " + BitwiseTest.FLAG_1.toString(2));
            trace("BitwiseTest.FLAG_3: " + BitwiseTest.FLAG_3.toString(2));
            trace("BitwiseTest.FLAG_5: " + BitwiseTest.FLAG_5.toString(2));
            trace("BitwiseTest.FLAG_6: " + BitwiseTest.FLAG_6.toString(2));

            this.test(BitwiseTest.FLAG_1 | BitwiseTest.FLAG_3 | BitwiseTest.FLAG_5 | BitwiseTest.FLAG_6);

        }

        private function test(flags:uint):void {

            trace("flags: " + flags.toString(2));

        }

    }

}</pre>
<p>The trace output will be:</p>
<pre>
BitwiseTest.FLAG_1: 1
BitwiseTest.FLAG_3: 100
BitwiseTest.FLAG_5: 10000
BitwiseTest.FLAG_6: 100000
flags: 110101
</pre>
<p>To check for a specific flag&#8217;s presence inside flags argument, we can use the bitwise &#8220;and&#8221; operator (&amp;):</p>
<pre>
if ((flags &amp; BitwiseTest.FLAG_1) &gt; 0) {

    trace("BitwiseTest.FLAG_1 passed - (" + String(flags &amp; BitwiseTest.FLAG_1) + ")");

}
</pre>
<p>By using the &amp; operator on flags it will returns the flag&#8217;s value if passed to the method and 0 otherwise. By updating the test method in this manner:</p>
<pre>
private function test(flags:uint):void {

    trace("flags: " + flags.toString(2));

    if ((flags &amp; BitwiseTest.FLAG_1) &gt; 0) {

        trace("BitwiseTest.FLAG_1 passed - (" + String(flags &amp; BitwiseTest.FLAG_1) + ")");

    }

    if ((flags &amp; BitwiseTest.FLAG_2) &gt; 0) {

        trace("BitwiseTest.FLAG_2 passed - (" + String(flags &amp; BitwiseTest.FLAG_2) + ")");

    }

    if ((flags &amp; BitwiseTest.FLAG_3) &gt; 0) {

        trace("BitwiseTest.FLAG_3 passed - (" + String(flags &amp; BitwiseTest.FLAG_3) + ")");

    }

    if ((flags &amp; BitwiseTest.FLAG_4) &gt; 0) {

        trace("BitwiseTest.FLAG_4 passed - (" + String(flags &amp; BitwiseTest.FLAG_4) + ")");

    }

    if ((flags &amp; BitwiseTest.FLAG_5) &gt; 0) {

        trace("BitwiseTest.FLAG_5 passed - (" + String(flags &amp; BitwiseTest.FLAG_5) + ")");

    }

    if ((flags &amp; BitwiseTest.FLAG_6) &gt; 0) {

        trace("BitwiseTest.FLAG_6 passed - (" + String(flags &amp; BitwiseTest.FLAG_6) + ")");

    }		

}</pre>
<p>We will obtain this output:</p>
<pre>BitwiseTest.FLAG_1: 1
BitwiseTest.FLAG_3: 100
BitwiseTest.FLAG_5: 10000
BitwiseTest.FLAG_6: 100000
flags: 110101
BitwiseTest.FLAG_1 passed - (1)
BitwiseTest.FLAG_3 passed - (4)
BitwiseTest.FLAG_5 passed - (16)
BitwiseTest.FLAG_6 passed - (32)</pre>
<p>We have successfully &#8220;intercepted&#8221; what flags has been passed to the method.<br />
Is important to know that flag&#8217;s values can&#8217;t be progressive numbers, but they must be multiples of 2, otherwise when using the bitwise &#8220;or&#8221; operator we will obtain a &#8220;ones overlap&#8221; resulting in an entity we won&#8217;t be able to properly manage. If we replace the flags values used in the previous example in this way:</p>
<pre>public static const FLAG_1:uint = 1;
public static const FLAG_2:uint = 2;
public static const FLAG_3:uint = 3;
public static const FLAG_4:uint = 4;
public static const FLAG_5:uint = 5;
public static const FLAG_6:uint = 6;</pre>
<p>We will get the following output:</p>
<pre>BitwiseTest.FLAG_1: 1
BitwiseTest.FLAG_3: 11
BitwiseTest.FLAG_5: 101
BitwiseTest.FLAG_6: 110
flags: 111
BitwiseTest.FLAG_1 passed - (1)
BitwiseTest.FLAG_2 passed - (2)
BitwiseTest.FLAG_3 passed - (3)
BitwiseTest.FLAG_4 passed - (4)
BitwiseTest.FLAG_5 passed - (5)
BitwiseTest.FLAG_6 passed - (6)</pre>
<p>As we can see our test method is unable to understand what flags it has received due to &#8220;ones overlap&#8221; (looks at te first 5 lines).</p>
<p>In conclusion&#8230; using bitwise operators for flags are pretty simple, however bitwise operators can do much more but understanding the real power of these operators and how to properly use them is not so easy (at least to me :-))</p>
]]></content:encoded>
			<wfw:commentRss>http://www.daveoncode.com/2009/02/08/using-bitwise-operators-with-actionscript-to-create-arguments-flags/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Create custom reusable Flex&#8217;s components with Actionscript</title>
		<link>http://www.daveoncode.com/2009/01/28/create-custom-reusable-flexs-components-with-actionscript/</link>
		<comments>http://www.daveoncode.com/2009/01/28/create-custom-reusable-flexs-components-with-actionscript/#comments</comments>
		<pubDate>Wed, 28 Jan 2009 21:51:26 +0000</pubDate>
		<dc:creator>Davide Zanotti</dc:creator>
				<category><![CDATA[actionscript]]></category>
		<category><![CDATA[flex]]></category>
		<category><![CDATA[as3]]></category>
		<category><![CDATA[custom components]]></category>
		<category><![CDATA[custom tags]]></category>
		<category><![CDATA[mxml]]></category>
		<category><![CDATA[namespace]]></category>

		<guid isPermaLink="false">http://www.daveoncode.com/?p=223</guid>
		<description><![CDATA[I&#8217;ve played for a while with Flex&#8217;s library and now I&#8217;m experimenting my own custom components and I would like to share them and the knowledge necessary to build personal, reusable cool components. Fundamentally there are two ways to realize custom Flex&#8217;s components: one (and the easiest) is to create an MXML file, the second]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve played for a while with Flex&#8217;s library and now I&#8217;m experimenting my own custom components and I would like to share them and the knowledge necessary to build personal, reusable cool components.</p>
<p><span>Fundamentally there are two ways to realize custom Flex&#8217;s components: one (and the easiest) is to create an MXML file, the second is to create an <span>Actionscript</span> class.</span></p>
<p><span><span id="more-223"></span><br />
</span></p>
<h2>MXML component</h2>
<p><span>The MXML file will contains a components like a Canvas (which is an excellent class to start to build on) as the root node, instead of the classic Application and it will be filled by others  several small components already available in Flex (Label, <span>TextField</span>, Button&#8230;), realizing a new complex object which can will be used inside MXML Applications by simply writing a tag with the same name of the MXML file. Another step required is to create an XML name space for the custom component created, this can be accomplished simply by insert an </span><em><strong><span><span>xmlns</span></span></strong></em><span> attribute inside the Application node, similarly to the default <span>mx</span> name space from adobe (<span>xmlns</span>:<span>mx</span>=&#8221;http://www.adobe.com/2006/<span>mxml</span>&#8220;), it must report a name after the colons and a path to the package containing the component. This is an example of a name space declaration for my own custom tag:</span></p>
<pre><span>&lt;<span>mx</span>:Application <span>xmlns</span>:<span>mx</span>="http://www.adobe.com/2006/<span>mxml</span>" <span>xmlns</span>:dz="com.<span>daveoncode</span>.controls.*" layout="vertical"&gt;</span></pre>
<p><span>It creates a name space called &#8220;dz&#8221; for all the classes (the &#8220;*&#8221; <span>wildcard</span> means &#8220;all&#8221;) inside the package &#8220;com/<span>daveoncode</span>/controls&#8221;. This is instead how a simple MXML based component could be (this is only a basic example, a such component is pretty useless):</span></p>
<pre><span>&lt;?<span>xml</span> version="1.0" encoding="<span>utf</span>-8"?&gt;</span>
<span>&lt;<span>mx</span>:Panel <span>xmlns</span>:<span>mx</span>="http://www.adobe.com/2006/<span>mxml</span>" title="<span>Login</span>" width="400" height="300"&gt;</span>
    &lt;mx:Form&gt;

<span>        &lt;<span>mx</span>:<span>FormItem</span> label="<span>UserName</span>:"&gt;</span>
<span>            &lt;<span>mx</span>:<span>TextInput</span> width="200" /&gt;</span>
<span>        &lt;/<span>mx</span>:<span>FormItem</span>&gt;</span>

<span>        &lt;<span>mx</span>:<span>FormItem</span> label="Password:"&gt;</span>
<span>            &lt;<span>mx</span>:<span>TextInput</span> width="200" <span>displayAsPassword</span>="true" /&gt;</span>
<span>        &lt;/<span>mx</span>:<span>FormItem</span>&gt;</span>

<span>        &lt;<span>mx</span>:<span>FormItem</span>&gt;</span>
<span>            &lt;<span>mx</span>:Button label="<span>Login</span>" /&gt;</span>
<span>        &lt;/<span>mx</span>:<span>FormItem</span>&gt;</span>

<span>    &lt;/<span>mx</span>:Form&gt;</span>
&lt;/mx:Panel&gt;</pre>
<p><span>Now, supposing that we save the example above as <span>LoginPanel</span>.<span>mxml</span>, under &#8220;com/<span>oursite</span>/components&#8221;, we can use it in a <span>mxml</span> Application in this way:</span></p>
<pre><span>&lt;<span>mx</span>:Application <span>xmlns</span>:cc="com.<span>oursite</span>.components.*" <span>xmlns</span>:<span>mx</span>="http://www.adobe.com/2006/<span>mxml</span>" layout="absolute"&gt;</span>
<span>    &lt;cc:<span>LoginPanel</span> /&gt;</span>
...</pre>
<p><span>This is pretty cool, because lets us separate defined areas of a GUI (like a <span>login</span> panel) and reuse them everywhere with no efforts&#8230; although this is an useful thing, is not so much exciting as creating a real custom component (something that is not made of other existent Flex components), whit its own properties, methods and style support!</span><br />
<span> To create such object, we should write it entirely in <span>Actionscript</span>.</span></p>
<h2><span><span>Actionscript</span> component</span></h2>
<p>We can start from extending an existent Flex class, in order to inherit all the necessary methods to interact properly with the framework. The most important thing to know, is how to handle our class configuration, in fact in Flex we can&#8217;t put configuration logic inside constructor, because it will be automatically called by Flex every time a tag with the components name is encountered and there is no way to pass arguments to the constructor from the MXML tag, so such approach:</p>
<pre><span>public function <span>MyCustomComponent</span>(param1:String, param2:Number) {</span>

    this.param1 = param1;
    this.param2 = param2;

}</pre>
<p><span>cant&#8217; be used! We have instead to write getter and setter which will be automatically called by Flex when required. This kind of getter/setter must use <span>Actionscript&#8217;s</span> </span><strong>get</strong> and <strong>set</strong> keywords, in order to get/set a property. So, we have to forget classic getter/setter methods like:</p>
<pre><span>public function <span>setMyParam</span>(value:String):void {</span>
    myParam = value;
}

<span>public function <span>getMyParam</span>():String {</span>
<span>    return <span>myParam</span>;</span>
}</pre>
<p>and adopt these:</p>
<pre><span>public function set <span>myParam</span>(value:String):void {</span>
    myParam = value;
}

<span>public function get <span>myParam</span>():String {</span>
<span>    return <span>myParam</span>;</span>
}</pre>
<p><span>The get/set key is a really cool feature of <span>Actionscript</span> language, because it lets to set an object property in a declarative manner (<span>myObject</span>.property = X), without <span>renunce</span> to encapsulation. In this way when in MXML file we will declare tag&#8217;s attributes:</span></p>
<pre><span>&lt;cc:<span>MyCustomComponent</span> <span>myParam</span>="Hello!" /&gt; </span></pre>
<p><span>Those setters will be used&#8230; but when exactly? Good question, another thing to understand, in order to realize custom and good components, is in fact the <span>creational</span> procedure adopted by the framework, which is divisible in these steps (in execution order):</span></p>
<ol>
<li><strong>Construction</strong>: in this step the component is instantiated and code inside the constructor is executed (typically the code will consist of a calling to super() and optionally event listeners configuration and few more)</li>
<li><strong>Configuration</strong><span>: <span>getters</span> methods are invoked by Flex to configure class&#8217; instance</span></li>
<li><strong>Attachment</strong><span>: this step <span>occours</span> only when the component is added to the display list. This is an automatic task if the component is called from an MXML (with the relative tag) and <span>occours</span> through <span>Actionscript</span> when </span><em><span><span>addChild</span>() </span></em>or <em><span><span>addChildAt</span>()</span></em> are invoked</li>
<li><strong>Initialization</strong>: this is the most intense phase, because during initialization a lot of tasks are performed and several methods invoked. The first thing that happens is the dispatching of <em><span><span>preinitialize</span></span></em><span> event (<span>FlexEvent</span>.PREINITIALIZE), then the protected method </span><em>createChildren() </em>is called (we have to override this method in order to populate our custom components with desired content), after its execution another event is dispatched, this time is the <em>initialize</em><span> (<span>FlexEvent</span>.INITIALIZE). After these, a series of <span>invalidations</span> and validations operations <span>occours</span> and finally <span>creationcomplete</span> is dispatched (<span>FlexEvent</span>.CREATION_COMPLETE)</span></li>
</ol>
<p><span>I realized a custom component called <span>StarPicker</span>, it is a control which can be used to rate objects (songs,  movies, books&#8230;)</span><span> by selecting (with a click) among N stars (N is one of the several parameters that can be <span>configurated</span> by the user, such color, size, space between stars and so on)</span><span>,  it is <span>bindable</span></span><span> (we can bind another control to <span>StarPicker&#8217;s</span> value)</span> and finally it support CSS styles&#8230; yes, with Flex we can create extreme custom components that will support custom user-defined properties (such &#8220;power&#8221;, &#8220;coolness&#8221;&#8230;)<span>. Although I&#8217;m quite embarrassed</span> (because I&#8217;m not already so confident with Flex), I&#8217;m going to show you how I realized my first Flex component.</p>
<p>My first step was create an Actionscript class (under my package &#8220;com.daveoncode.controls&#8221;) which extends Canvas, then I defined several public constants for default settings, by using the Flex naming convenction &#8220;DEFAULT_MY_SETTING_NAME&#8221;, several private variable to handle settings values and as many private variables to track settings changes. So, before the constructor I&#8217;ve such stuff (every prameter has 3 types of variables/constants related to it):</p>
<pre>public static const DEFAULT_STARS:uint = 5;
// ...more constants

private var _stars:uint;
// ...more vars

private var starsDefined:Boolean;
// ...more booleans</pre>
<p>The constructor contains a call to super and some listeners creation (and I enable the doubleclick over the component):</p>
<pre>public function StarPicker() {

    super();

    // This component allows double click in order to deselect stars
    this.doubleClickEnabled = true;

    // Listens for preinitialize event
    this.addEventListener(FlexEvent.PREINITIALIZE, this.preInitializeHandler);

    // Listens for the click event over the stars
    this.addEventListener(MouseEvent.CLICK, this.clickHandler);

    // Listens for the double click event over the stars
    this.addEventListener(MouseEvent.DOUBLE_CLICK, this.doubleClickHandler);

}</pre>
<p>The only thing I want to analize in the code above is the listener for FlexEvent.PREINITIALIZE&#8230; why I used this listener? The answer is: I use it to set default values if they are not provided by the user (with user I mean who use the component in the MXML file, not the Application&#8217;s user), because I want to handle default settings only after all the setters are called by Flex in order to set properly the boolean flags &#8220;myparamDefined&#8221;, which I use to grant the right precedence between style and inline tag&#8217;s attribute. If a flag is setted (myparamDefined == true) then when CSS assign a value to the same parameter this is not used, because inline attribute have the precedence and win. So the handler has several ternary operators like:</p>
<pre>this._stars = this._stars == 0 ? StarPicker.DEFAULT_STARS : this._stars;</pre>
<p>In order to populate the picker with stars I override  <strong>createChildren()</strong> method, into which I use <strong>addChild()</strong> to insert N Star object (which is a Class in the same package, which basically draws a star shape and has methods to change colors and size), I also override  <strong>measure()</strong> method which is used by Flex to know the exact size of the components (typycally when it must calculates clipping and resizing):</p>
<pre>override protected function measure():void {

    super.measure();

    // StarPicker's height will be the same as the stars
    this.measuredHeight = this.measuredMinHeight = this._starSize + this._starBorderSize;

    // StarPicker's width will be the same as the sum of the stars width + space
    this.measuredWidth = this.measuredMinWidth = (this._starSize + this._starSpace) * this._stars;

}</pre>
<p>In practice I&#8217;m telling Flex that my component should be never clipped and is larger as the sum of the stars width it contains. Oh&#8230; I also override the <strong>styleChanged()</strong> method, which is called by Flex every time a component&#8217;s style parameter changes:</p>
<pre>override public function styleChanged(styleProp:String):void {

    super.styleChanged(styleProp);

    this._stars = this.stars;
    // ... other (re)settings

}</pre>
<p>And style metatags? Yes, I putted them before class declaration:</p>
<pre>[Style(name="stars", type="uint", inherit="no")]
// ... more styles</pre>
<p>With style metatag we can declare the name of styles properties that our components will suport, so with the previous declaration will be possible to create CSS like:</p>
<pre>.myCustomComponent {
    stars: 4;
}</pre>
<p>Ok, I wrote too much, this is an example of the custom component that I realized (you can download, use and view source code by selecting &#8220;Flex components&#8221; on blog&#8217;s menu):</p>

    <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" id="starpicker" width="600" height="370" align="left" name="starpicker">
      <param name="movie" value="http://www.daveoncode.com/wp-content/uploads/2009/01/customcomponent.swf" />
      <param name="align" value="left" />
      <param name="name" value="starpicker" />
      <!--[if !IE]>-->
      <object type="application/x-shockwave-flash" data="http://www.daveoncode.com/wp-content/uploads/2009/01/customcomponent.swf" width="600" height="370" align="left" name="starpicker">
      <!--<![endif]-->
        <p>The Flash plugin is required to view this object.</p>
      <!--[if !IE]>-->
      </object>
      <!--<![endif]-->
    </object>

<p>The example above is not a screenshot&#8230; you can test it ;-)<br style="clear: both" /></p>
]]></content:encoded>
			<wfw:commentRss>http://www.daveoncode.com/2009/01/28/create-custom-reusable-flexs-components-with-actionscript/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Implementing Array.shuffle() in Actionscript</title>
		<link>http://www.daveoncode.com/2009/01/08/implementing-arrayshuffle-in-actionscript/</link>
		<comments>http://www.daveoncode.com/2009/01/08/implementing-arrayshuffle-in-actionscript/#comments</comments>
		<pubDate>Thu, 08 Jan 2009 13:20:38 +0000</pubDate>
		<dc:creator>Davide Zanotti</dc:creator>
				<category><![CDATA[actionscript]]></category>
		<category><![CDATA[array shuffle]]></category>
		<category><![CDATA[as3]]></category>
		<category><![CDATA[while loops]]></category>

		<guid isPermaLink="false">http://daveoncode.wordpress.com/?p=209</guid>
		<description><![CDATA[One useful thing that Actionscript doesn&#8217;t offers is the ability to shuffle an array, however this can be accomplished in a blink of an eye and with very few lines of code. Before to implement my own solution, I looked on the web, but solutions provided by others developers looks too complex and too long]]></description>
			<content:encoded><![CDATA[<p>One useful thing that Actionscript doesn&#8217;t offers is the ability to shuffle an array, however this can be accomplished in a blink of an eye and with very few lines of code. Before to implement my own solution, I looked on the web, but solutions provided by others developers looks too complex and too long to type. What I found was based on a for loop and several line of codes to fill a secondary array with the elements in a new random order. My solution instead is based on a while loop (3 lines of code only):</p>
<p><span id="more-209"></span></p>
<pre>var arr2:Array = [];

while (arr.length &gt; 0) {
    arr2.push(arr.splice(Math.round(Math.random() * (arr.length - 1)), 1)[0]);
}</pre>
<h2>How it works?</h2>
<p>First we have to create a new array (<strong><em>arr2</em></strong>) which will contains the elements from the base ordered array (<strong><em>arr</em></strong>), then we use a while loop, which can be translated into english as: &#8220;do this until base array is not empty&#8221;. Inside the loop, we push into the second array a random element from the first array and, at the same time, we remove that element from its array. Array&#8217;s method <strong><em>splice()</em></strong> in fact, removes one or more elements from an array and returns an array containing the deleted elements, that in our case is only one (as specified in the second argument). The first argument of the method represent the starting index from which to start to delete, this is dynamically calculated by using the Math.random() method (which returns a number between 0 and 1), then multiplied by the length of the base array minus 1, because array length is always 1 greater than elements array contains (cause array indexing starts from zero), finally this number is rounded to the nearest integer by Math.round(), in order to access an exact index.</p>
<p>I think this is the best and shorter way to implement a shuffle functionality :)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.daveoncode.com/2009/01/08/implementing-arrayshuffle-in-actionscript/feed/</wfw:commentRss>
		<slash:comments>17</slash:comments>
		</item>
		<item>
		<title>We should always use Actionscript&#8217;s &quot;this&quot; keyword</title>
		<link>http://www.daveoncode.com/2009/01/07/we-should-always-use-actionscripts-this-keyword/</link>
		<comments>http://www.daveoncode.com/2009/01/07/we-should-always-use-actionscripts-this-keyword/#comments</comments>
		<pubDate>Wed, 07 Jan 2009 16:52:13 +0000</pubDate>
		<dc:creator>Davide Zanotti</dc:creator>
				<category><![CDATA[actionscript]]></category>
		<category><![CDATA[as3]]></category>

		<guid isPermaLink="false">http://daveoncode.wordpress.com/?p=206</guid>
		<description><![CDATA[I was wondering if to use or not the &#8220;new&#8221; keyword for classes variables and methods, because it&#8217;s not mandatory and in as3 examples is rarely used and when it&#8217;s used, is only to avoid name collisions (typically when setting a class property using a given argument), such: package { public class MyClass { private]]></description>
			<content:encoded><![CDATA[<p>I was wondering if to use or not the &#8220;new&#8221; keyword for classes variables and methods, because it&#8217;s not mandatory and in as3 examples is rarely used and when it&#8217;s used, is only to avoid name collisions (typically when setting a class property using a given argument), such:</p>
<pre>package {

    public class MyClass {

        private var myVar:uint;

        public function MyClass(myVar:uint) {

            this.myVar = myVar;

        }

    }

}</pre>
<p>However there are 3 valid reasons to (always) use  the <strong><em>&#8220;this&#8221;</em></strong> keyword:</p>
<ol>
<li>Is an excellent way to visually differentiate between static and dynamic variable (static variables can&#8217;t use the &#8220;this&#8221; keyword, otherwise you get a compile error)</li>
<li>When invoking methods is immediately evident which is the class owner/target of the method itself</li>
<li>Faster typing and developing thanks to Flex Builder hints (after typing &#8220;this.&#8221;, I can select all the applicable methods, which is faster and error free than type the entire method name)</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://www.daveoncode.com/2009/01/07/we-should-always-use-actionscripts-this-keyword/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>
