<?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; as3</title>
	<atom:link href="http://www.daveoncode.com/tag/as3/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.daveoncode.com</link>
	<description>Objective C, iOS and more programming stuff</description>
	<lastBuildDate>Mon, 19 Dec 2011 12:11:12 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>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 [...]<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://www.daveoncode.com/2009/02/08/using-bitwise-operators-with-actionscript-to-create-arguments-flags/' addthis:title='Using bitwise operators with Actionscript to create arguments flags ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></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>
<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://www.daveoncode.com/2009/02/08/using-bitwise-operators-with-actionscript-to-create-arguments-flags/' addthis:title='Using bitwise operators with Actionscript to create arguments flags ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></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 [...]<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://www.daveoncode.com/2009/01/28/create-custom-reusable-flexs-components-with-actionscript/' addthis:title='Create custom reusable Flex&#8217;s components with Actionscript ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></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]-->
        
      <!--[if !IE]>-->
      </object>
      <!--<![endif]-->
    </object>

<p>The example above is not a screenshot&#8230; you can test it ;-)<br style="clear: both" /></p>
<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://www.daveoncode.com/2009/01/28/create-custom-reusable-flexs-components-with-actionscript/' addthis:title='Create custom reusable Flex&#8217;s components with Actionscript ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></content:encoded>
			<wfw:commentRss>http://www.daveoncode.com/2009/01/28/create-custom-reusable-flexs-components-with-actionscript/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Populate a Flex&#8217;s ArrayCollection&#8230; a consideration about addItem() performance</title>
		<link>http://www.daveoncode.com/2009/01/12/populate-a-flexs-arraycollection-a-consideration-about-additem-performance/</link>
		<comments>http://www.daveoncode.com/2009/01/12/populate-a-flexs-arraycollection-a-consideration-about-additem-performance/#comments</comments>
		<pubDate>Mon, 12 Jan 2009 15:30:19 +0000</pubDate>
		<dc:creator>Davide Zanotti</dc:creator>
				<category><![CDATA[flex]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[as3]]></category>

		<guid isPermaLink="false">http://daveoncode.wordpress.com/?p=216</guid>
		<description><![CDATA[I&#8217;m playing with Flex data Providers and actually I&#8217;m testing ArrayCollection. There are 3 ways to populate an Array collection: Initialize an ArrayCollection and then use its addItem() method (which accepts any Object type) Initialize an ArrayCollection, access its &#8220;embedded&#8221; Array by using the source property (ie: myArrayCollection.source) an then push objects inside this one [...]<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://www.daveoncode.com/2009/01/12/populate-a-flexs-arraycollection-a-consideration-about-additem-performance/' addthis:title='Populate a Flex&#8217;s ArrayCollection&#8230; a consideration about addItem() performance ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m playing with Flex data Providers and actually I&#8217;m testing <em><strong>ArrayCollection</strong></em>. There are 3 ways to populate an Array collection:</p>
<ol>
<li>Initialize an ArrayCollection and then use its <em><strong>addItem()</strong></em> method (which accepts any Object type)</li>
<li>Initialize an ArrayCollection, access its &#8220;embedded&#8221; Array by using the <strong><em>source</em></strong> property (ie: myArrayCollection.source) an then push objects inside this one</li>
<li>Initialize an Array, populate it and then initialize an ArrayCollection by passing the Array to its constructor (ie: myArrayCollection:ArrayCollection = new ArrayCollection(myPreviousDefinedArray))</li>
</ol>
<p>Last technique is (as far I saw) absolutely the fastest , I did a test in which I populated a collection with 300,000 objects and it taken about 9 seconds instead of <em><strong>32</strong></em> (350% slower) of first one! The second technique is also faster than the first, but the third is better.</p>
<p>So, what I learned is that <strong><em>addItem()</em></strong> should used only to add few items, instead for huge collection should be avoided in favor of the third technique described in order to obtain better performances.</p>
<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://www.daveoncode.com/2009/01/12/populate-a-flexs-arraycollection-a-consideration-about-additem-performance/' addthis:title='Populate a Flex&#8217;s ArrayCollection&#8230; a consideration about addItem() performance ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></content:encoded>
			<wfw:commentRss>http://www.daveoncode.com/2009/01/12/populate-a-flexs-arraycollection-a-consideration-about-additem-performance/feed/</wfw:commentRss>
		<slash:comments>7</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 [...]<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://www.daveoncode.com/2009/01/08/implementing-arrayshuffle-in-actionscript/' addthis:title='Implementing Array.shuffle() in Actionscript ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></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>
<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 /></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> arr2:<span style="color: #0066CC;">Array</span> = <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span>;<br />
<span style="color: #b1b100;">while</span> <span style="color: #66cc66;">&#40;</span>arr.<span style="color: #0066CC;">length</span> <span style="color: #66cc66;">&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; arr2.<span style="color: #0066CC;">push</span><span style="color: #66cc66;">&#40;</span>arr.<span style="color: #0066CC;">splice</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">round</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">random</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">*</span> <span style="color: #66cc66;">&#40;</span>arr.<span style="color: #0066CC;">length</span> - <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>, <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #66cc66;">&#125;</span></div></td></tr></tbody></table></div>
<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>
<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://www.daveoncode.com/2009/01/08/implementing-arrayshuffle-in-actionscript/' addthis:title='Implementing Array.shuffle() in Actionscript ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></content:encoded>
			<wfw:commentRss>http://www.daveoncode.com/2009/01/08/implementing-arrayshuffle-in-actionscript/feed/</wfw:commentRss>
		<slash:comments>30</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 [...]<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://www.daveoncode.com/2009/01/07/we-should-always-use-actionscripts-this-keyword/' addthis:title='We should always use Actionscript&#8217;s &#34;this&#34; keyword ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></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>
<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://www.daveoncode.com/2009/01/07/we-should-always-use-actionscripts-this-keyword/' addthis:title='We should always use Actionscript&#8217;s &quot;this&quot; keyword ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></content:encoded>
			<wfw:commentRss>http://www.daveoncode.com/2009/01/07/we-should-always-use-actionscripts-this-keyword/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
	</channel>
</rss>

