<?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>Stroep &#187; as3</title> <atom:link href="http://blog.stroep.nl/tag/as3/feed/" rel="self" type="application/rss+xml" /><link>http://blog.stroep.nl</link> <description>Just a collection of random works - Mark Knol</description> <lastBuildDate>Tue, 10 Jan 2012 20:09:53 +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>Lights and shadows experiment</title><link>http://blog.stroep.nl/2011/11/lights-and-shadows/</link> <comments>http://blog.stroep.nl/2011/11/lights-and-shadows/#comments</comments> <pubDate>Mon, 28 Nov 2011 22:55:28 +0000</pubDate> <dc:creator>Mark Knol</dc:creator> <category><![CDATA[actionscript]]></category> <category><![CDATA[Code snippets]]></category> <category><![CDATA[as3]]></category> <category><![CDATA[experiment]]></category> <category><![CDATA[flash]]></category><guid isPermaLink="false">http://blog.stroep.nl/?p=1322</guid> <description><![CDATA[<img src="http://blog.stroep.nl/wp-content/lights-150x150.png" alt="" title="lights and shadows" width="100" height="100" class="alignleft size-thumbnail wp-image-1332" />Recently I found an interesting question on FlashFocus.nl, about creating light and shadows in a 2D environment. I started experimenting with this to see how far I could take it. I love to share the results and some insides about this fun project.]]></description> <content:encoded><![CDATA[<p><img src="http://blog.stroep.nl/wp-content/lights-150x150.png" alt="" title="lights and shadows" width="100" height="100" class="alignleft size-thumbnail wp-image-1332" />Recently I found an <a href="http://www.flashfocus.nl/forum/showthread.php?t=63044">interesting question on FlashFocus.nl</a>, about creating light and shadows in a 2D environment. I started experimenting with this to see how far I could take it. I love to share the results and some insides about this fun project.</p><p><center><a href="http://projects.stroep.nl/lights/"><img src="http://blog.stroep.nl/wp-content/lights-shadows-experiment.jpg" alt="" title="Lights and shadows experiment" width="492" height="221" class="size-full wp-image-1367" /></a></center></p><h2>How to get started</h2><p><a href="http://www.flashfocus.nl/forum/showpost.php?p=416394&#038;postcount=11" title="Initial code on FlashFocus (Dutch)">The initial code</a> that the topic starter used a loop through all objects, and used <a href="http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/DisplayObject.html?filter_flash=cs5&#038;filter_flashplayer=10.2&#038;filter_air=2.6#hitTestPoint()" title="DisplayObject.hitTestPoint function on Adobe LiveDocs">hitTestPoint</a>-function to detect if the light hits a wall. Therefore he used a loop with 360 steps, used for all directions. In the innerloop it tries if it hits the wall, otherwise goes away from the light (using the direction of the outerloop), tries if it hits the wall. That loop goes on till it does hit a wall. It is a bit of trial and error, but I think it is very clever. At the end of each loop a lineTo is used to draw the light, so in the end it will be a shape with 360 vector points.</p><h2>Speed of light</h2><p>Now, that screams for optimization and speed. It&#8217;s a fun project to mix some things you have seen and want to try out for a long time. The first thing that I tried to make it faster, draw all objects into an <a href="http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/BitmapData.html" title="BitmapData at Adobe LiveDocs">BitmapData</a>, and use the <a href="http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/BitmapData.html#getPixel()">getPixel</a> function instead of <em>hitTestPoint</em>, which is notable faster if you don&#8217;t move objects. I used that <em>BitmapData</em> object only too check if the light hits the objects; no one actually sees it. To use <em>hitTestPoint</em>, you must loop through all objects and detect if they hit. With a lot of objects, it would be slower. With <em>BitmapData</em>, you don&#8217;t need that loop, and you are free to use unlimited objects with even custom shapes. Less looping == faster, unlimited objects == awesome <img src='http://blog.stroep.nl/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /></p><h2>More realism</h2><p>I also wanted to make it look more realistic. Having light only is not close enough to realism. It needs shadows. I created an inverted shape of the light-shape and filled it with black (a). This creates a nice shadow, however has very hard bounds, which is ugly. So then I took the shape again and drawed with a nice transparent to black gradient (b). Those 2 layers have the BlendMode ALPHA. The holder of those has a solid shape fill (as big as the stage) with BlendMode LAYER. This creates a nice &#8216;hole&#8217; which I think works very well as shadow if you make it half-transparent. Oh, the colored light-layer (c) is a gradient too, with the same shape as b, only with BlendMode ADD.</p><p><center><a href="http://projects.stroep.nl/lights/"><img src="http://blog.stroep.nl/wp-content/lights-shadows-layers.jpg" alt="" title="lights and shadows layers" width="560" height="221" class="alignnone size-full wp-image-1375" /></a></center></p><h2>Re-using shape data</h2><p>Another optimization I used was to use <a href='../07/the-new-lineto/' title='The new graphics.lineTo'>graphics data API</a>. In this case I have 1 shape which could be reused 3 times. So before even drawing, I collect all points and commands in an Vector. When I have all data collected, it draws the light-layer and shadow-layer at first, then it pushes some extra points to the Array to make the outer shadow-layer. So with 1 Array of (manipulated) data I could draw multiple shapes. That&#8217;s a good case of using the graphics data, I guess.</p><h2>Optimized trig functions</h2><p>I also used the <a href="http://jacksondunstan.com/articles/1190">TrigLUT</a>-class (Trigonometry LookUp Table) from <a href="http://www.jacksondunstan.com/">jacksondunstan.com</a>, since the loop heavily uses Math.sin and Math.cos. I could use the <em>valNormalizedPositive()</em> function from that class in this case. It&#8217;s very fast and for me it was fun to use that class. <img src='http://blog.stroep.nl/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /></p><h2>But it is still too slow..</h2><p>The most intense about this thing isn&#8217;t the calculation, but the rendering itself. The whole &#8216;canvas&#8217; needs to be updated and redrawed every frame with lots of layers and blendmodes, which is very intense for the CPU. I don&#8217;t know if this would be possible  with Stage3D, but that would be the last step to render really fast. And when that is possible, I&#8217;ll guess I could move the object real-time too, which makes it very interesting for games.</p><p><strong>Check out the experiment:<br /> » <a href="http://projects.stroep.nl/lights/">Lights and shadows</a></strong></p><p>Let me know what you think about it and how does it perform on your computer?</p><p><a href="http://blog.stroep.nl/?flattrss_redirect&amp;id=1322&amp;md5=42908c11d2d8a98ce7bbb949c7b586ec" title="Flattr" target="_blank"><img src="http://blog.stroep.nl/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded> <wfw:commentRss>http://blog.stroep.nl/2011/11/lights-and-shadows/feed/</wfw:commentRss> <slash:comments>2</slash:comments> </item> <item><title>Design pattern &#8211; Behaviors</title><link>http://blog.stroep.nl/2011/10/behaviors/</link> <comments>http://blog.stroep.nl/2011/10/behaviors/#comments</comments> <pubDate>Sun, 16 Oct 2011 18:07:22 +0000</pubDate> <dc:creator>Mark Knol</dc:creator> <category><![CDATA[actionscript]]></category> <category><![CDATA[as3]]></category> <category><![CDATA[oop]]></category><guid isPermaLink="false">http://blog.stroep.nl/?p=1295</guid> <description><![CDATA[<img alt="Design pattern - Behaviors" src="http://farm3.static.flickr.com/2631/3753675658_2904c86297_t.jpg" class="alignleft" width="100" height="100" />I recently started working at <a href="http://www.mediamonks.com/" target="_blank">MediaMonks</a> where they are using this interesting pattern of coding; behaviors as a class. I started playing with 'behaviors' myself to see how this could work for me. If you use this pattern once, you'll wonder why you did not use it ever before ;) This post will lead you through some examples and shows some benefits of using it. ]]></description> <content:encoded><![CDATA[<p><img alt="Design pattern - Behaviors" src="http://farm3.static.flickr.com/2631/3753675658_2904c86297_t.jpg" class="alignleft" width="100" height="100" />I recently started working at <a href="http://www.mediamonks.com/" target="_blank">MediaMonks</a> where they are using this interesting pattern of coding; behaviors as a class. I started playing with &#8216;behaviors&#8217; myself to see how this could work for me. If you use this pattern once, you&#8217;ll wonder why you did not use it ever before <img src='http://blog.stroep.nl/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> This post will lead you through some examples and shows some benefits of using it. This post is written with focus on actionscript 3 developers, who can read code and knows already something about object oriented programming. The goal is to learn more and become a better developer.</p><h2>What is a behavior?</h2><p>Well I guess everybody knows what it means, but in terms of code I would say that a behavior-class is some kind of a simple controller with a target. It does one thing, it controls the target with a specific feature/goal. The idea is to add functionality by assigning a behavior to an object (<a href="http://en.wikipedia.org/wiki/Composition_in_object-oriented_programming">composition</a>), instead of extending it to get the functionality (<a href="http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)">inheritance</a>). That sound complicated, so why not check out what it practically means?</p><h2>Difference between inheritance and composition</h2><p>Let&#8217;s say you want to program an object that follows your mouse. How would you create it? I would create a class named <em>MouseFollowingSprite </em>which has all functions to do that. Then I would extend or create an instance of that <em>MouseFollowingSprite</em>-class and BANG I&#8217;m done <img src='http://blog.stroep.nl/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /></p><p><strong>Typical example of inheritance</strong></p><blockquote><p>class MouseFollowingSprite extends Sprite</p></blockquote><p>Okay, lets write down that killer class:</p><div class="bbCSH" style="font-family: monospace;"> <span style="color: #0066CC;">package</span> nl.<span style="color: #006600;">stroep</span>.<span style="color: #006600;">display</span><br /> <span style="color: #66cc66;">&#123;</span><br /> &nbsp; &nbsp; <span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #0066CC;">Sprite</span>;<br /> &nbsp; &nbsp; <span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">Event</span>;<br /> &nbsp; &nbsp; <br /> &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">/**<br /> &nbsp; &nbsp; &nbsp;* @author Mark Knol<br /> &nbsp; &nbsp; &nbsp;*/</span><br /> &nbsp; &nbsp; <span style="color: #b1b100;">public</span> <span style="color: #000000; ">class</span> MouseFollowingSprite <span style="color: #0066CC;">extends</span> <span style="color: #0066CC;">Sprite</span><br /> &nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">private</span> <span style="color: #000000; ">var</span> _ease:<span style="color: #0066CC;">Number</span>;<br /> &nbsp; &nbsp; &nbsp; &nbsp; <br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">public</span> <span style="color: #000000; ">function</span> MouseFollowingSprite<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; init<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; <br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">private</span> <span style="color: #000000; ">function</span> init<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">this</span>.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">ENTER_FRAME</span>, handleEnterFrame<span style="color: #66cc66;">&#41;</span>;<br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; <br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">private</span> <span style="color: #000000; ">function</span> handleEnterFrame<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; update<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; <br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">private</span> <span style="color: #000000; ">function</span> update<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">this</span>.<span style="color: #006600;">x</span> += <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span>.<span style="color: #006600;">mouseX</span> &#8211; <span style="color: #0066CC;">this</span>.<span style="color: #006600;">x</span><span style="color: #66cc66;">&#41;</span> / _ease;<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">this</span>.<span style="color: #006600;">y</span> += <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span>.<span style="color: #006600;">mouseY</span> &#8211; <span style="color: #0066CC;">this</span>.<span style="color: #006600;">y</span><span style="color: #66cc66;">&#41;</span> / _ease;<br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br /> &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br /> <span style="color: #66cc66;">&#125;</span><br /> &nbsp;</div><p>(You see that? It&#8217;s nicely done with some eased motion <img src='http://blog.stroep.nl/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> )</p><p>I have never seen a problem with extending, but I had troubles with finding a good way to add other kind of functions to this type of classes. In terms of OOP, is a rule to write classes with a <a href="http://en.wikipedia.org/wiki/Single_responsibility_principle">single responsibility</a>, and we already passed that one. But imaging you want your mouse following sprite also to scale when you scroll with your mouse wheel. Well that would be easy: Just create a class named <em>ScrollWheelScalingSprite</em>, which extends <em>MouseFollowingSprite</em>. Now it has both functionality, when you would create an instance of it.</p><p>Let&#8217;s create our ScrollWheelScalingSprite, it would like this:</p><div class="bbCSH" style="font-family: monospace;"> <span style="color: #0066CC;">package</span> nl.<span style="color: #006600;">stroep</span>.<span style="color: #006600;">display</span><br /> <span style="color: #66cc66;">&#123;</span><br /> &nbsp; &nbsp; <span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">DisplayObject</span>;<br /> &nbsp; &nbsp; <span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">Event</span>;<br /> &nbsp; &nbsp; <span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">MouseEvent</span>;<br /> &nbsp; &nbsp; <br /> &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">/**<br /> &nbsp; &nbsp; &nbsp;* &#8230;<br /> &nbsp; &nbsp; &nbsp;* @author Mark Knol<br /> &nbsp; &nbsp; &nbsp;*/</span><br /> &nbsp; &nbsp; <span style="color: #b1b100;">public</span> <span style="color: #000000; ">class</span> ScrollwheelScaleSprite <span style="color: #0066CC;">extends</span> MouseFollowingSprite<br /> &nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">private</span> <span style="color: #000000; ">var</span> _ease:<span style="color: #0066CC;">Number</span>;<br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">private</span> <span style="color: #000000; ">var</span> _targetScale:<span style="color: #0066CC;">Number</span>;<br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">private</span> <span style="color: #000000; ">var</span> _hasListener:<span style="color: #0066CC;">Boolean</span>;<br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">private</span> <span style="color: #000000; ">var</span> _deltaRatio:<span style="color: #0066CC;">Number</span> = <span style="color: #cc66cc;">0.1</span>;<br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">private</span> <span style="color: #000000; ">var</span> _minimum:<span style="color: #0066CC;">Number</span> = <span style="color: #0066CC;">NaN</span>;<br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">private</span> <span style="color: #000000; ">var</span> _maximum:<span style="color: #0066CC;">Number</span> = <span style="color: #0066CC;">NaN</span>;<br /> &nbsp; &nbsp; &nbsp; &nbsp; <br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">public</span> <span style="color: #000000; ">function</span> ScrollwheelScaleSprite<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _targetScale = <span style="color: #0066CC;">this</span>.<span style="color: #006600;">scaleX</span>;<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; init<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></p><p>&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">private</span> <span style="color: #000000; ">function</span> init<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">stage</span>.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">MOUSE_WHEEL</span>, handleMouseWheel<span style="color: #66cc66;">&#41;</span>;<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; addEnterFrameListener<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; <br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">private</span> <span style="color: #000000; ">function</span> handleMouseWheel<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:MouseEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _targetScale += <span style="color: #0066CC;">e</span>.<span style="color: #006600;">delta</span> * _deltaRatio;<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>!<span style="color: #0066CC;">isNaN</span><span style="color: #66cc66;">&#40;</span>_minimum<span style="color: #66cc66;">&#41;</span> &amp;&amp; _targetScale &lt; _minimum<span style="color: #66cc66;">&#41;</span> _targetScale = _minimum;<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>!<span style="color: #0066CC;">isNaN</span><span style="color: #66cc66;">&#40;</span>_maximum<span style="color: #66cc66;">&#41;</span> &amp;&amp;_targetScale &gt; _maximum<span style="color: #66cc66;">&#41;</span> _targetScale = _maximum;<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; addEnterFrameListener<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; <br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">private</span> <span style="color: #000000; ">function</span> addEnterFrameListener<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>!_hasListener<span style="color: #66cc66;">&#41;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">this</span>.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">ENTER_FRAME</span>, handleEnterFrame<span style="color: #66cc66;">&#41;</span>;<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _hasListener = <span style="color: #000000; ">true</span>;<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; <br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">private</span> <span style="color: #000000; ">function</span> removeEnterFrameListener<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">this</span>.<span style="color: #006600;">removeEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">ENTER_FRAME</span>, handleEnterFrame<span style="color: #66cc66;">&#41;</span>;<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _hasListener = <span style="color: #000000; ">false</span>;<br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; <br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">private</span> <span style="color: #000000; ">function</span> handleEnterFrame<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; update<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; <br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">private</span> <span style="color: #000000; ">function</span> update<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">this</span>.<span style="color: #006600;">scaleX</span> += <span style="color: #66cc66;">&#40;</span>_targetScale &#8211; <span style="color: #0066CC;">this</span>.<span style="color: #006600;">scaleX</span> <span style="color: #66cc66;">&#41;</span> / _ease;<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">this</span>.<span style="color: #006600;">scaleY</span> = <span style="color: #0066CC;">this</span>.<span style="color: #006600;">scaleX</span>;<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">abs</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span>.<span style="color: #006600;">scaleX</span> &#8211; _targetScale<span style="color: #66cc66;">&#41;</span> &lt; .<span style="color: #cc66cc;">01</span><span style="color: #66cc66;">&#41;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; removeEnterFrameListener<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br /> &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br /> <span style="color: #66cc66;">&#125;</span><br /> &nbsp;</div><p>Let&#8217;s go some deeper. What if you want a <em>ScrollwheelScaleSprite </em>which should not follow our mouse? That would be a problem, because it already has that functionality. However you could hack around around by adding some protected booleans, to enable the actual functions of the class. Name them like <em>_mouseFollowingEnabled</em>. When your class extend some other classes, you&#8217;ll probably need a <em>_scrollWheelScalingEnabled</em> too. In the constructor of the new class you just turn on/off the booleans and your done. But then again, if you want to use this to Bitmap, you have a problem, you cannot extend a Sprite and a Bitmap at the same time.</p><p>I mean, this is how we code, right? All classes have one responsibility, but could this example been done better? While writing, this post reminds me in a way to <a href="http://www.bit-101.com/blog/?p=2173">this older post</a> of Keith Peters; you should read it. It also has a interesting discussion.</p><h2>Create behaviors</h2><p>As already stated, a behavior class is a controller. It deals with an object (we call it target) to add the actual behavior. Now I don&#8217;t think extending is bad, but I think we should use it with care.</p><p>Let&#8217;s create an abstract behavior class, which can be extended. (This is not needed, but very helpful for accessing the target and prevent duplicate code)</p><div class="bbCSH" style="font-family: monospace;"> <span style="color: #0066CC;">package</span> nl.<span style="color: #006600;">stroep</span>.<span style="color: #006600;">behaviors</span> <br /> <span style="color: #66cc66;">&#123;</span><br /> &nbsp; &nbsp; <span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">DisplayObject</span>;<br /> &nbsp; &nbsp; <br /> &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">/**<br /> &nbsp; &nbsp; &nbsp;* @author Mark Knol<br /> &nbsp; &nbsp; &nbsp;*/</span><br /> &nbsp; &nbsp; <span style="color: #b1b100;">public</span> <span style="color: #000000; ">class</span> AbstractBehavior <br /> &nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; protected <span style="color: #000000; ">var</span> <span style="color: #0066CC;">_target</span>: DisplayObject;<br /> &nbsp; &nbsp; &nbsp; &nbsp; <br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">public</span> <span style="color: #000000; ">function</span> AbstractBehavior<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">target</span>: DisplayObject<span style="color: #66cc66;">&#41;</span> <br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">_target</span> = <span style="color: #0066CC;">target</span>;<br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br /> &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br /> <span style="color: #66cc66;">&#125;</span><br /> &nbsp;</div><p>I think this does not require lots of explanation. We have a class, it saves a target<br /> that is passed from the constructor. We can do lot&#8217;s of stuff inside a behavior from this point.</p><p>Now, let&#8217;s convert our <em>MouseFollowingSprite </em>to a <em>MouseFollowingBehavior</em>, which should extends the AbstractBehavior. Most drastic change is to convert &#8216;<em>this</em>&#8216;, to &#8216;<em>_target</em>&#8216; and change the constructor to make it an actual behavior:</p><div class="bbCSH" style="font-family: monospace;"> <span style="color: #0066CC;">package</span> nl.<span style="color: #006600;">stroep</span>.<span style="color: #006600;">behaviors</span> <br /> <span style="color: #66cc66;">&#123;</span><br /> &nbsp; &nbsp; <span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">DisplayObject</span>;<br /> &nbsp; &nbsp; <span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">Event</span>;<br /> &nbsp; &nbsp; <br /> &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">/**<br /> &nbsp; &nbsp; &nbsp;* @author Mark Knol<br /> &nbsp; &nbsp; &nbsp;*/</span><br /> &nbsp; &nbsp; <span style="color: #b1b100;">public</span> <span style="color: #000000; ">class</span> MouseFollowBehavior <span style="color: #0066CC;">extends</span> AbstractBehavior <br /> &nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">private</span> <span style="color: #000000; ">var</span> _ease:<span style="color: #0066CC;">Number</span>;<br /> &nbsp; &nbsp; &nbsp; &nbsp; <br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">public</span> <span style="color: #000000; ">function</span> MouseFollowBehavior<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">target</span>: DisplayObject, ease:<span style="color: #0066CC;">Number</span> = <span style="color: #cc66cc;">5</span><span style="color: #66cc66;">&#41;</span> <br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">super</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">target</span><span style="color: #66cc66;">&#41;</span>;<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _ease = ease;<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; init<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; <br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">private</span> <span style="color: #000000; ">function</span> init<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">_target</span>.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">ENTER_FRAME</span>, handleEnterFrame<span style="color: #66cc66;">&#41;</span>;<br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; <br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">private</span> <span style="color: #000000; ">function</span> handleEnterFrame<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; update<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; <br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">private</span> <span style="color: #000000; ">function</span> update<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">_target</span>.<span style="color: #006600;">x</span> += <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">_target</span>.<span style="color: #006600;">mouseX</span> &#8211; <span style="color: #0066CC;">_target</span>.<span style="color: #006600;">x</span><span style="color: #66cc66;">&#41;</span> / _ease;<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">_target</span>.<span style="color: #006600;">y</span> += <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">_target</span>.<span style="color: #006600;">mouseY</span> &#8211; <span style="color: #0066CC;">_target</span>.<span style="color: #006600;">y</span><span style="color: #66cc66;">&#41;</span> / _ease;<br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br /> &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br /> <span style="color: #66cc66;">&#125;</span><br /> &nbsp;</div><p>This is not magic, but it is very powerful. We can assign this behavior to a <em>Sprite</em>, but also to a <em>Bitmap</em> or <em>Video</em> etc. This would not that easy / possible with inheritance only. The class works standalone and we don&#8217;t need booleans to enable or disable the behavior.</p><p>Ok, let&#8217;s also create the scroll wheel scale behavior:</p><div class="bbCSH" style="font-family: monospace;"> <span style="color: #0066CC;">package</span> nl.<span style="color: #006600;">stroep</span>.<span style="color: #006600;">art</span>.<span style="color: #006600;">behaviors</span> <br /> <span style="color: #66cc66;">&#123;</span><br /> &nbsp; &nbsp; <span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">DisplayObject</span>;<br /> &nbsp; &nbsp; <span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">Event</span>;<br /> &nbsp; &nbsp; <span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">MouseEvent</span>;<br /> &nbsp; &nbsp; <br /> &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">/**<br /> &nbsp; &nbsp; &nbsp;* &#8230;<br /> &nbsp; &nbsp; &nbsp;* @author Mark Knol<br /> &nbsp; &nbsp; &nbsp;*/</span><br /> &nbsp; &nbsp; <span style="color: #b1b100;">public</span> <span style="color: #000000; ">class</span> ScrollwheelScaleBehavior <span style="color: #0066CC;">extends</span> AbstractBehavior <br /> &nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">private</span> <span style="color: #000000; ">var</span> _ease:<span style="color: #0066CC;">Number</span>;<br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">private</span> <span style="color: #000000; ">var</span> _targetScale:<span style="color: #0066CC;">Number</span>;<br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">private</span> <span style="color: #000000; ">var</span> _hasListener:<span style="color: #0066CC;">Boolean</span>;<br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">private</span> <span style="color: #000000; ">var</span> _deltaRatio:<span style="color: #0066CC;">Number</span>;<br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">private</span> <span style="color: #000000; ">var</span> _minimum:<span style="color: #0066CC;">Number</span>;<br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">private</span> <span style="color: #000000; ">var</span> _maximum:<span style="color: #0066CC;">Number</span>;<br /> &nbsp; &nbsp; &nbsp; &nbsp; <br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">public</span> <span style="color: #000000; ">function</span> ScrollwheelScaleBehavior<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">target</span>: DisplayObject, ease:<span style="color: #0066CC;">Number</span> = <span style="color: #cc66cc;">5</span>, deltaRatio:<span style="color: #0066CC;">Number</span> = <span style="color: #cc66cc;">0.1</span>, minimum:<span style="color: #0066CC;">Number</span> = <span style="color: #0066CC;">NaN</span>, maximum:<span style="color: #0066CC;">Number</span> = <span style="color: #0066CC;">NaN</span><span style="color: #66cc66;">&#41;</span> <br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">super</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">target</span><span style="color: #66cc66;">&#41;</span>;<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _ease = ease;<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _deltaRatio = deltaRatio;<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _minimum = minimum;<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _maximum = maximum;<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _targetScale = <span style="color: #0066CC;">target</span>.<span style="color: #006600;">scaleX</span>;<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; init<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; <br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">private</span> <span style="color: #000000; ">function</span> init<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">_target</span>.<span style="color: #0066CC;">stage</span>.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">MOUSE_WHEEL</span>, handleMouseWheel<span style="color: #66cc66;">&#41;</span>;<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; addEnterFrameListener<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; <br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">private</span> <span style="color: #000000; ">function</span> handleMouseWheel<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:MouseEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _targetScale += <span style="color: #0066CC;">e</span>.<span style="color: #006600;">delta</span> * _deltaRatio;<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>!<span style="color: #0066CC;">isNaN</span><span style="color: #66cc66;">&#40;</span>_minimum<span style="color: #66cc66;">&#41;</span> &amp;&amp; _targetScale &lt; _minimum<span style="color: #66cc66;">&#41;</span> _targetScale = _minimum;<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>!<span style="color: #0066CC;">isNaN</span><span style="color: #66cc66;">&#40;</span>_maximum<span style="color: #66cc66;">&#41;</span> &amp;&amp;_targetScale &gt; _maximum<span style="color: #66cc66;">&#41;</span> _targetScale = _maximum;<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; addEnterFrameListener<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; <br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">private</span> <span style="color: #000000; ">function</span> addEnterFrameListener<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>!_hasListener<span style="color: #66cc66;">&#41;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">_target</span>.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">ENTER_FRAME</span>, handleEnterFrame<span style="color: #66cc66;">&#41;</span>;<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _hasListener = <span style="color: #000000; ">true</span>;<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; <br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">private</span> <span style="color: #000000; ">function</span> removeEnterFrameListener<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">_target</span>.<span style="color: #006600;">removeEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">ENTER_FRAME</span>, handleEnterFrame<span style="color: #66cc66;">&#41;</span>;<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _hasListener = <span style="color: #000000; ">false</span>;<br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; <br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">private</span> <span style="color: #000000; ">function</span> handleEnterFrame<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; update<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; <br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">private</span> <span style="color: #000000; ">function</span> update<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">_target</span>.<span style="color: #006600;">scaleX</span> += <span style="color: #66cc66;">&#40;</span>_targetScale &#8211; <span style="color: #0066CC;">_target</span>.<span style="color: #006600;">scaleX</span> <span style="color: #66cc66;">&#41;</span> / _ease;<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">_target</span>.<span style="color: #006600;">scaleY</span> = <span style="color: #0066CC;">_target</span>.<span style="color: #006600;">scaleX</span>;<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">abs</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">_target</span>.<span style="color: #006600;">scaleX</span> &#8211; _targetScale<span style="color: #66cc66;">&#41;</span> &lt; .<span style="color: #cc66cc;">01</span><span style="color: #66cc66;">&#41;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; removeEnterFrameListener<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br /> &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br /> <span style="color: #66cc66;">&#125;</span><br /> &nbsp;</div><p>Now we can assign a behavior very easy:<br /> <strong>Example of applying the behaviors</strong></p><blockquote><p>var sprite:Sprite = new Sprite();<br /> new MouseFollowBehavior(sprite);<br /> new ScrollwheelScaleBehavior(sprite);</p></blockquote><p>You are now free to add the behaviors you&#8217;ll need, and leave the one you don&#8217;t need. It&#8217;s like creating little plugins. Fun eh? It&#8217;s nice to add parameters to the constructor, to make it less static. The other fun part is that if you build a behavior right, you could copy/paste it to another project or in you library, because it has no dependencies to classes you don&#8217;t want or need. Of course, this is not something new, you could have seen this in <a href="http://code.google.com/p/templelibrary/source/browse/trunk#trunk%2Flib%2Ftemple%2Fui%2Fbehaviors">temple-lib</a> but also the <a href="https://github.com/hype/hype/tree/master/src/hype/extended/behavior">Hype-framework</a> too, and maybe lots of other framework probably use this pattern in a way.</p><p>Now we know the difference:<br /> <strong>Inheritence:</strong> <em>&#8220;I want a Sprite that follows my mouse&#8221;</em><br /> <strong>Behaviors:</strong> <em>&#8220;I want something that makes a Sprite follow my mouse&#8221;</em></p><h2>Killing the behaviors</h2><p>The code examples in this post does not have any function to remove it, but if you want to remove a behavior, it would be wise to assign the behavior to a variable, and create a destruct() or destroy() function inside the behaviors to remove listeners and references to the target etc, and then set it to <em>null</em>.</p><div class="bbCSH" style="font-family: monospace;"> <span style="color: #808080; font-style: italic;">// Anywhere in your code:</span><br /> <span style="color: #000000; ">var</span> myBehavior:MouseFollowBehavior = <span style="color: #000000; ">new</span> MouseFollowBehavior<span style="color: #66cc66;">&#40;</span>myMc<span style="color: #66cc66;">&#41;</span>;</p><p><span style="color: #808080; font-style: italic;">// Somewhere in your code where you want to remove the behavior:</span><br /> myBehavior.<span style="color: #006600;">destroy</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// a public function which removes all listeners, references etc </span><br /> myBehavior = <span style="color: #000000; ">null</span>;<br /> &nbsp;</div><h2>Conclusion</h2><p>Behaviors are a cool and give clean reusable classes and I think we should use it more. It&#8217;s probably not new for you and the inheritence-example is not very realistic, but I hope it&#8217;s refreshing to look at it again. If you want to learn more design patterns, I also recommend <a href="http://www.as3dp.com/">this site</a>.</p><p>If you think back on how you have coded in the past, what would you change now you know this pattern? BTW I want to learn more too, so don&#8217;t hesitate to leave some nice feedback below.</p><p><small style='color:#777'>Oh, maybe you haven&#8217;t heard, but something unexpected will happen if you hold CTRL and click on the facebook like-button (found that in a YouTube comment; very lame).</small></p><p><a href="http://blog.stroep.nl/?flattrss_redirect&amp;id=1295&amp;md5=c5800b6caadc18178824a5e54b1c07a6" title="Flattr" target="_blank"><img src="http://blog.stroep.nl/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded> <wfw:commentRss>http://blog.stroep.nl/2011/10/behaviors/feed/</wfw:commentRss> <slash:comments>7</slash:comments> </item> <item><title>Actionscript 3 Event Generator</title><link>http://blog.stroep.nl/2011/10/actionscript-3-event-generator/</link> <comments>http://blog.stroep.nl/2011/10/actionscript-3-event-generator/#comments</comments> <pubDate>Mon, 10 Oct 2011 19:54:55 +0000</pubDate> <dc:creator>Mark Knol</dc:creator> <category><![CDATA[actionscript]]></category> <category><![CDATA[as3]]></category> <category><![CDATA[events]]></category> <category><![CDATA[tool]]></category><guid isPermaLink="false">http://blog.stroep.nl/?p=1283</guid> <description><![CDATA[<img src="http://blog.stroep.nl/wp-content/as3-event-generator.png" alt="" width="100" height="100" class="alignleft size-full wp-image-1286" />Finally I have created and finalized my Actionscript 3 <a href="http://projects.stroep.nl/EventGenerator/">Event Generator</a>, which was buggy but now it is very nice :)  As <a href="http://twitter.com/#!/mknol/status/120917893415321600">stated on twitter</a>, if you are a flashdeveloper and use custom events in actionscript 3, you should use this tool because it helps you create custom event classes very fast.]]></description> <content:encoded><![CDATA[<p><img src="http://blog.stroep.nl/wp-content/as3-event-generator.png" alt="" width="100" height="100" class="alignleft size-full wp-image-1286" />Finally I have created and finalized my Actionscript 3 <a href="http://projects.stroep.nl/EventGenerator/">Event Generator</a>, which was buggy but now it is very nice <img src='http://blog.stroep.nl/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> As <a href="http://twitter.com/#!/mknol/status/120917893415321600">stated on twitter</a>, if you are a flashdeveloper and use custom events in actionscript 3, you should use this tool because it helps you create custom event classes very fast.</p><p><strong>Features:</strong><br /> - Fast variable adding<br /> - Event type generation (static constants). Just use lower camelCased variables and they will be formatted nicely.<br /> - Meta data for a event dispatcher class.<br /> - Automatic clone() function<br /> - Automatic toString() function</p><p><strong>URL</strong><br /> » <strong><a href="http://projects.stroep.nl/EventGenerator/">http://projects.stroep.nl/EventGenerator/</a></strong></p><p>Don&#8217;t forget to first fill your name at @author on the right. The package and @author will be saved for next time.</p><p>I have also created a dropdown-menu on the top-right to switch to other tools. Happy coding!</p><p><a href="http://blog.stroep.nl/?flattrss_redirect&amp;id=1283&amp;md5=d6900eafca74d75ae064d94ab4f01d1d" title="Flattr" target="_blank"><img src="http://blog.stroep.nl/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded> <wfw:commentRss>http://blog.stroep.nl/2011/10/actionscript-3-event-generator/feed/</wfw:commentRss> <slash:comments>2</slash:comments> </item> <item><title>Actionscript 3 Extended valueobject generator</title><link>http://blog.stroep.nl/2011/09/enumgenerator/</link> <comments>http://blog.stroep.nl/2011/09/enumgenerator/#comments</comments> <pubDate>Tue, 20 Sep 2011 06:19:22 +0000</pubDate> <dc:creator>Mark Knol</dc:creator> <category><![CDATA[actionscript]]></category> <category><![CDATA[as3]]></category> <category><![CDATA[tool]]></category> <category><![CDATA[ValueObject]]></category><guid isPermaLink="false">http://blog.stroep.nl/?p=1272</guid> <description><![CDATA[<img src="http://img.youtube.com/vi/A-fCGKQySmI/0.jpg" class="alignleft" width="100" height="100" />I created a tool to created 'grouped' value objects nicely and very fast. It is using private classes. I found it useful enough to share it with you, so maybe if you are in need of such a tool, you are free to use it.Click on <em>read more</em> to see more details.]]></description> <content:encoded><![CDATA[<p><iframe width="100%" height="375" src="http://www.youtube.com/embed/A-fCGKQySmI" frameborder="0" allowfullscreen></iframe></p><p>I created a tool to created &#8216;grouped&#8217; value objects nicely and very fast. It is using private classes. I found it useful enough to share it with you, so maybe if you are in need of such a tool, you are free to use it.</p><p>» <strong>Check it out: <a href="http://projects.stroep.nl/enumGenerator/">http://projects.stroep.nl/enumGenerator/</a></strong></p><p>Oh, if you want to create normal value objects, use the <a href="http://projects.stroep.nl/ValueObjectGenerator/">value object generation tool</a>. For creating custom events classes, I have an <a href="http://projects.stroep.nl/EventGenerator/">custom event generator</a>.</p><p><a href="http://blog.stroep.nl/?flattrss_redirect&amp;id=1272&amp;md5=86e44790ca307ef70ef3eb3eab24a99b" title="Flattr" target="_blank"><img src="http://blog.stroep.nl/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded> <wfw:commentRss>http://blog.stroep.nl/2011/09/enumgenerator/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>The new graphics.lineTo</title><link>http://blog.stroep.nl/2011/07/the-new-lineto/</link> <comments>http://blog.stroep.nl/2011/07/the-new-lineto/#comments</comments> <pubDate>Tue, 19 Jul 2011 19:34:08 +0000</pubDate> <dc:creator>Mark Knol</dc:creator> <category><![CDATA[actionscript]]></category> <category><![CDATA[Generative art]]></category> <category><![CDATA[as3]]></category> <category><![CDATA[drawing]]></category><guid isPermaLink="false">http://blog.stroep.nl/?p=1233</guid> <description><![CDATA[<img alt="" src="http://farm4.static.flickr.com/3146/3102998311_bb74535e82_t.jpg" class="alignleft" width="100" height="100" />Most flashcoders knows you can draw lines using <em>graphics.lineTo</em>. There is another way of drawing lines in actionscript, using the <em>graphics.drawPath</em> and <em>graphics.drawGraphicsData</em> function. Let's take a look at it.]]></description> <content:encoded><![CDATA[<p><img alt="" src="http://farm4.static.flickr.com/3146/3102998311_bb74535e82_t.jpg" class="alignleft" width="100" height="100" />I am working on a Pie-chart class. Data visualisation is fun, so I want to create my own pie-chart. Reinventing the wheel (creating another pie-chart) is a good thing if you want to create a custom wheel, so you know how to modify to make it fit your needs. Now this post is not about the pie-chart, but about something I discovered inside the graphics class. Most flashcoders knows you can draw lines using <em>graphics.lineTo</em>. There is another way of drawing lines in actionscript, using the <em>graphics.drawPath</em> and <em>graphics.drawGraphicsData</em> function. Let&#8217;s take a look at it.</p><h2>graphics.lineTo</h2><p>The good old method. Drawing a line from point (x:100, y:150) to point (x:300, y:300). Check out these four lines of clear code.</p><div class="bbCSH" style="font-family: monospace;"> graphics.<span style="color: #0066CC;">clear</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// start with blank canvas</span><br /> graphics.<span style="color: #0066CC;">lineStyle</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">2</span>, 0xFF9900<span style="color: #66cc66;">&#41;</span>;<br /> graphics.<span style="color: #0066CC;">moveTo</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">100</span>,<span style="color: #cc66cc;">150</span><span style="color: #66cc66;">&#41;</span>;<br /> graphics.<span style="color: #0066CC;">lineTo</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">300</span>,<span style="color: #cc66cc;">300</span><span style="color: #66cc66;">&#41;</span>;<br /> &nbsp;</div><h2>graphics.drawPath</h2><p>Now, you could also use this alternative way of drawing. There is no such thing as creating circles or squares using this method, only manual. It is using commands (moveTo, lineTo, curveTo) and has a separate list of data containing the actual values. All values in the data-list represents the x-positions (odd values) and y-positions (even values). So we are pushing x, y, x, y etc.<br /> When using CURVE_TO, you need to define four values; controlX, controlY, x, y. Take a look at the code:<div class="bbCSH" style="font-family: monospace;"><span style="color: #000000; ">var</span> commands:Vector.&lt;int&gt; = <span style="color: #000000; ">new</span> Vector.&lt;int&gt;<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br /> <span style="color: #000000; ">var</span> <span style="color: #0066CC;">data</span>:Vector.&lt;Number&gt; = <span style="color: #000000; ">new</span> Vector.&lt;Number&gt;<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br /> commands.<span style="color: #0066CC;">push</span><span style="color: #66cc66;">&#40;</span>GraphicsPathCommand.<span style="color: #006600;">MOVE_TO</span>, GraphicsPathCommand.<span style="color: #006600;">LINE_TO</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// add 2 commands, moveTo and lineTo</span><br /> <span style="color: #0066CC;">data</span>.<span style="color: #0066CC;">push</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">100</span>, <span style="color: #cc66cc;">150</span><span style="color: #66cc66;">&#41;</span><br /> <span style="color: #0066CC;">data</span>.<span style="color: #0066CC;">push</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">300</span>, <span style="color: #cc66cc;">300</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// push all values into data-list</span></p><p>graphics.<span style="color: #0066CC;">clear</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// clear the canvas</span><br /> graphics.<span style="color: #0066CC;">lineStyle</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">2</span>, 0xFFCC00<span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// Set the stroke style</span><br /> graphics.<span style="color: #006600;">drawPath</span><span style="color: #66cc66;">&#40;</span>commands, <span style="color: #0066CC;">data</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// finally, draw it all</span><br /> &nbsp;</div><p>As you could see, you need to write a bit more code. As you can see you can combine the <em>graphics.lineStyle</em> and the <em>graphics.drawPath</em> functions, so its not completely new.</p><p>By the way, did you know you could push multiple items to an list at a time?<br /><h2>graphics.drawGraphicsData</h2><p>Be prepared, 12 lines to draw the same a line.</p><div class="bbCSH" style="font-family: monospace;"> <span style="color: #000000; ">var</span> commands:Vector.&lt;int&gt; = <span style="color: #000000; ">new</span> Vector.&lt;int&gt;<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br /> <span style="color: #000000; ">var</span> <span style="color: #0066CC;">data</span>:Vector.&lt;Number&gt; = <span style="color: #000000; ">new</span> Vector.&lt;Number&gt;<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br /> <span style="color: #000000; ">var</span> path:GraphicsPath = <span style="color: #000000; ">new</span> GraphicsPath<span style="color: #66cc66;">&#40;</span>commands, <span style="color: #0066CC;">data</span><span style="color: #66cc66;">&#41;</span>;<br /> <span style="color: #000000; ">var</span> drawing:Vector.&lt;IGraphicsData&gt; = <span style="color: #000000; ">new</span> Vector.&lt;IGraphicsData&gt;<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</p><p><span style="color: #000000; ">var</span> stroke:GraphicsStroke = <span style="color: #000000; ">new</span> GraphicsStroke<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span>;<br /> stroke.<span style="color: #006600;">fill</span> = <span style="color: #000000; ">new</span> GraphicsSolidFill<span style="color: #66cc66;">&#40;</span>0xFF9900<span style="color: #66cc66;">&#41;</span>;<br /> <span style="color: #808080; font-style: italic;">// The stroke definition with a fill</span></p><p>commands.<span style="color: #0066CC;">push</span><span style="color: #66cc66;">&#40;</span>GraphicsPathCommand.<span style="color: #006600;">MOVE_TO</span>, GraphicsPathCommand.<span style="color: #006600;">LINE_TO</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// add 2 commands, moveTo and lineTo</span><br /> <span style="color: #0066CC;">data</span>.<span style="color: #0066CC;">push</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">100</span>, <span style="color: #cc66cc;">150</span><span style="color: #66cc66;">&#41;</span><br /> <span style="color: #0066CC;">data</span>.<span style="color: #0066CC;">push</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">300</span>, <span style="color: #cc66cc;">300</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// push all values into data, odd represents the x-positions, even represents the y-positions. So we are pushing x, y, x, y etc.</span><br /> drawing.<span style="color: #0066CC;">push</span><span style="color: #66cc66;">&#40;</span>stroke, path<span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// collect all data inside one list</span></p><p>graphics.<span style="color: #0066CC;">clear</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// clear the canvas</span><br /> graphics.<span style="color: #006600;">drawGraphicsData</span><span style="color: #66cc66;">&#40;</span>drawing<span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// finally, draw it all</span><br /> &nbsp;</div><p>The drawGraphicsData function forces you to push all data into a new list. The fun thing is, you feel very exited when you have the last example running. I don&#8217;t know why, but getting advanced stuff working feels good. Note, for the last example we need to import these classes to draw that orange line. Five classes for one simple orange line, pretty impressive eh? <img src='http://blog.stroep.nl/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /></p><div class="bbCSH" style="font-family: monospace;"> <span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">GraphicsPath</span>;<br /> <span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">GraphicsPathCommand</span>;<br /> <span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">GraphicsSolidFill</span>;<br /> <span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">GraphicsStroke</span>;<br /> <span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">IGraphicsData</span>;<br /> &nbsp;</div><h2>Borders/fills</h2><p>When I first started to use this way of drawing lines, I noticed that I needed 2 lines of code to define a stroke. The fill is the last parameter of the GraphicsStroke class, and I really dislike to define all other optional parameters just to set that one parameter at the end. Oh, without a fill or color, a border is invisble <img src='http://blog.stroep.nl/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> Still don&#8217;t get it! The fill is a fun part of this advance drawing method. Now in the normal lineTo-world we know these linestyles <em>graphics.lineBitmapStyle, graphics.lineGradientStyle, graphics.lineShaderStyle</em> and ofcourse the <em>graphics.lineStyle</em>. Now if you want to create a border to be &#8216;filled&#8217; with something, you could create a<em> GraphicsSolidFill, GraphicsGradientFill, GraphicsBitmapFill</em> or <em>GraphicsShaderFill</em>.</p><p>It is nice to know the same classes could be used to fill a shape, so they could be added to the <em>drawing</em>-list too. That is really a nice part of this advanced way of drawing, just add or remove instances of classes to the <em>drawing</em>-list (it should implement IGraphicsData).</p><p>You can now define fills, strokes and multiple paths before you actually draw it on the stage.</p><h2>Saving graphics; saving numbers</h2><p>The cool thing of this whole advanced drawing method is that you could save the commands/paths as a file and redo it more easy than <em>graphics.lineTo</em>. If you do generative arts this could be a benefit. There is no official function for saving the data, but you could write it yourself since we are dealing with numbers. When opening a file you could theoretically push all commands/paths/fills inside the <em>drawing</em>-list and you could draw the same drawing. I haven&#8217;t test this, but maybe you could save the whole vector as <em>ByteArray</em> using <em>ByteArray.writeObject()</em>, compress it and then you have a nice way of saving &#8216;graphics&#8217;. Another benefit, you could delay &#8216;the draw&#8217; and still collect data which could be drawed later. Very handy if you are doing heavy generative arts.</p><h2>Conclusion?</h2><p>It is not easy to draw lines using the the advanced method, since you are dealing with vectors with numbers, instead of lines. It is cool to have every graphic-thing as a separate classes and fills are nicely done and easily swap-able. Graphics data is now more flexible. Adobe could add more graphics classes. I hope they will add support for custom strokes (like Illustrator). At the other side, most things still fit in the good-old lineTo coding-style, which is very clean and straight-forward. Anyhow, it is nice to see a new way of saving graphics.</p><p>When you are drawing lots of lines, this advanced way should bring better performance since it is delaying the actual draw. I haven&#8217;t benchmarked how much exactly, but since you can mix <em>graphics.drawPath</em> with <em>graphics.lineStyle</em>, it feels <em>graphics.drawGraphicsData</em> is <a href='http://en.wikipedia.org/wiki/Syntactic_sugar' title='Syntactic sugar on Wikipedia'>syntactic sugar</a> which has the benefits noted I already mentioned.</p><p>I also wonder if it there would be a performance boost if you create your own lineTo function (which only pushes data to a list) based on the graphics.drawGraphicsData, with one drawNow() function.</p><p>Hope this will inspire you to share other or new possibilities.</p><div class="bbCSH" style="font-family: monospace;"> <span style="color: #000000; ">var</span> theEnd:GraphicsEndFill = <span style="color: #000000; ">new</span> GraphicsEndFill<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// thanks for reading</span><br /> &nbsp;</div><p><a href="http://blog.stroep.nl/?flattrss_redirect&amp;id=1233&amp;md5=5354a53fc0f23599d9e352144f205470" title="Flattr" target="_blank"><img src="http://blog.stroep.nl/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded> <wfw:commentRss>http://blog.stroep.nl/2011/07/the-new-lineto/feed/</wfw:commentRss> <slash:comments>5</slash:comments> </item> <item><title>Optimized loops</title><link>http://blog.stroep.nl/2011/07/optimized-loops-for-actionscript/</link> <comments>http://blog.stroep.nl/2011/07/optimized-loops-for-actionscript/#comments</comments> <pubDate>Sun, 17 Jul 2011 13:33:07 +0000</pubDate> <dc:creator>Mark Knol</dc:creator> <category><![CDATA[actionscript]]></category> <category><![CDATA[as3]]></category> <category><![CDATA[code]]></category> <category><![CDATA[optimalisation]]></category><guid isPermaLink="false">http://blog.stroep.nl/?p=1225</guid> <description><![CDATA[<img src="http://farm4.static.flickr.com/3430/3757660341_2b4d557f18_t.jpg" alt="Optimized loops" class="alignleft"/>Every one should write code that performs well. Do you use loops in your code? This is a simple trick everyone could apply to his loops. ]]></description> <content:encoded><![CDATA[<p><img src="http://farm4.static.flickr.com/3430/3757660341_2b4d557f18_t.jpg" alt="Optimized loops" class="alignleft"/>Every one should write code that performs well. Do you use loops in your code? This is a simple trick everyone could apply to his loops.</p><h2>How you write a loop</h2><p>Mostly when people are writing loops, it looks like this:</p><div class="bbCSH" style="font-family: monospace;"> <span style="color: #b1b100;">for</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; ">var</span> i:<span style="color: #0066CC;">int</span> = <span style="color: #cc66cc;">0</span>; i &lt; <span style="color: #0066CC;">list</span>.<span style="color: #0066CC;">length</span>; i++<span style="color: #66cc66;">&#41;</span><br /> <span style="color: #66cc66;">&#123;</span><br /> &nbsp; &nbsp;<span style="color: #000000; ">var</span> item:MyItem = <span style="color: #0066CC;">list</span><span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>;<br /> &nbsp; &nbsp;item.<span style="color: #006600;">doSomething</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br /> &nbsp; &nbsp;item.<span style="color: #006600;">doAnotherThing</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br /> <span style="color: #66cc66;">&#125;</span><br /> &nbsp;</div><p>Now this loops works, but there are some small things that could be improved. If you use lots of loops it should be needed to improve your loops, but I hope this is just nice to know anyway.</p><h2>The optimized version</h2><p>Take a look at the improved loop:</p><div class="bbCSH" style="font-family: monospace;"> <span style="color: #000000; ">var</span> item:MyItem;<br /> <span style="color: #000000; ">var</span> total:uint = <span style="color: #0066CC;">list</span>.<span style="color: #0066CC;">length</span>;<br /> <span style="color: #b1b100;">for</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; ">var</span> i:uint; i &lt; total; i++<span style="color: #66cc66;">&#41;</span><br /> <span style="color: #66cc66;">&#123;</span><br /> &nbsp; &nbsp;item = <span style="color: #0066CC;">list</span><span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span> as MyItem;<br /> &nbsp; &nbsp;item.<span style="color: #006600;">doSomething</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br /> &nbsp; &nbsp;item.<span style="color: #006600;">doAnotherThing</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br /> <span style="color: #66cc66;">&#125;</span><br /> &nbsp;</div><p>What is improved in this loop? If you take a close look in both loops you will see I access the list-item once using <em>list[i]</em>, and assign it to a variable called <em>item</em>. I don&#8217;t what it is, but for me it looks like most people who write javascript always use <em>list[i]</em> instead of creating a <em>var</em> of it, is that right? It is important to know you don&#8217;t need to create the var inside the loop, but create it once outside the loop, and (re-)use it while looping. I forget this most of the times, but it is a good practice to check if all the variables you are using inside a loop. Most of the times variables could be defined outside the loop, which could increase performance big time.</p><p>The second thing is to define a variable called total, which pre-calculates the length of the loop as a local variable. If you don&#8217;t define it, the FlashPlayer checks the length of the Array every time in the loop, now it is like a constant. Note, when you are removing items from the list, you should ajust the <em>total</em>-variable too, since the total does not equal the length of the list anymore ofcourse. <img src='http://blog.stroep.nl/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /></p><p>Another thing you probably noticed is that I used the &#8216;as&#8217; keyword to give the FlashPlayer a direct hint of the type of variable. This another way of casting, and slightly faster than <em>MyItem(list[i])</em>.<br /><blockquote>Update: Take a look at this great article about the performance of this casting method. <a href="http://jacksondunstan.com/articles/1305" target="_blank">http://jacksondunstan.com/articles/1305</a></p></blockquote><p>Some other small things are to use uints for <em>i</em> and <em>total</em> in loops if you are using positive numbers. Another small thing is that you don&#8217;t have to define <em>i = 0</em>, only when you are using nested loops or re-using the <em>i</em> or <em>j</em> in the same function/scope.</p><p>Want a more freaky way to define the same optimized loop?</p><div class="bbCSH" style="font-family: monospace;"> <span style="color: #b1b100;">for</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; ">var</span> i:uint, item:MyItem, total:uint = <span style="color: #0066CC;">list</span>.<span style="color: #0066CC;">length</span>; i &lt; total; i++<span style="color: #66cc66;">&#41;</span><br /> <span style="color: #66cc66;">&#123;</span><br /> &nbsp; &nbsp;item = <span style="color: #0066CC;">list</span><span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span> as MyItem;<br /> &nbsp; &nbsp;item.<span style="color: #006600;">doSomething</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br /> &nbsp; &nbsp;item.<span style="color: #006600;">doAnotherThing</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br /> <span style="color: #66cc66;">&#125;</span><br /> &nbsp;</div><p>Are you using FlashDevelop? You could <a href="http://projects.stroep.nl/flashdevelop/" title="For loop snippet" target="_blank">this for-loop snippet</a> (scroll down).</p><p>Most of the loop-optimisation tips also applies to javascript.</p><p><a href="http://blog.stroep.nl/?flattrss_redirect&amp;id=1225&amp;md5=ba0c22d1d31dead4db61c598af01c3a2" title="Flattr" target="_blank"><img src="http://blog.stroep.nl/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded> <wfw:commentRss>http://blog.stroep.nl/2011/07/optimized-loops-for-actionscript/feed/</wfw:commentRss> <slash:comments>12</slash:comments> </item> <item><title>Javascript for AS3 developers</title><link>http://blog.stroep.nl/2011/01/javascript-for-as3-developers/</link> <comments>http://blog.stroep.nl/2011/01/javascript-for-as3-developers/#comments</comments> <pubDate>Tue, 25 Jan 2011 23:46:17 +0000</pubDate> <dc:creator>Mark Knol</dc:creator> <category><![CDATA[Code snippets]]></category> <category><![CDATA[Javascript]]></category> <category><![CDATA[as3]]></category> <category><![CDATA[html5]]></category> <category><![CDATA[oop]]></category> <category><![CDATA[snippet]]></category><guid isPermaLink="false">http://blog.stroep.nl/?p=1146</guid> <description><![CDATA[<img src="http://blog.stroep.nl/wp-content/netscape_logo.jpg" alt="" title="Javascript for AS3 developers" width="100" height="100" class="alignleft size-full wp-image-1158" />This article is some training for your JavaScript skills. I love Actionscript , and I am also starting to like JavaScript. HTML5 is an hot topic today, and it seems using JavaScript is an important part of it, so why not learn more of it?]]></description> <content:encoded><![CDATA[<p><img src="http://blog.stroep.nl/wp-content/netscape_logo.jpg" alt="" title="Javascript for AS3 developers" width="100" height="100" class="alignleft size-full wp-image-1158" />This article is some training for your JavaScript skills. I love Actionscript , and I am also starting to like JavaScript. However, coding in JavaScript feels like Actionscript 1.0. It&#8217;s a pity there aren&#8217;t decent editors, especially if you want to write object-oriented-code, which <em>is</em> possible with JavaScript . At the moment I think VisualStudio is the best JavaScript editor, but I really wish there was a tool for JavaScript like FlashDevelop (a clean editor for Actionscript projects) with great intellisense, code completion and snippets etc. But we should not focus on tools, lets focus on the language and get the most out of it. HTML5 is an hot topic today, and it seems using JavaScript is an important part of it, so why not learn more of it? I have seen some <a href="http://mrdoob.com/">stunning HTML5 examples</a> over the last year. As a developer it is very interesting to see how others code JavaScript. By the way; Personally I find it very funny to see how JavaScript has changed it&#8217;s &#8216;image&#8217;. Some of us remember the good days in Netscape. JavaScript was .. *kuch* not fast and associated with ugly rainbow mouse pointers and aggressive popup screens. Now it is associated with HTML5, which is supposed to be the future and it will be bright <img src='http://blog.stroep.nl/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> I refuse to put my opinion about this topic here.<br /> I think the &#8216;image&#8217; of JS has slowly changed after great frameworks like jQuery have reached the developers. jQuery is awesome, I advice to use jQuery as default in all sites/apps.</p><p>Anyway, there are (ofcourse) some techniques to code like Actionscript in JavaScript. These snippets have helped me to understand object oriented JavaScript. For too long I did not even tried to learn what was possible with JavaScript because I thought it was a very limited language. Maybe some information is outdated; I am still learning, so please share your feedback.</p><h2>Object oriented javascript</h2><p>In javascript it is possible to create classes, public and private vars, getters/setters and there are even constructors. A function could be seen as a class. This is a bit strange at first, but if you use it for a while you will understand. Now lets create our own class right now.</p><p>Lets port some stuff of <a href="http://code.google.com/p/stroep/source/browse/trunk/flashsources/utils%20classes/nl/stroep/utils/Color.as">this Color Class</a> to javascript. It has private vars and public functions.</p><h2>Declare classes</h2><p>Defining classes is as easy as defining functions, because it is almost the same. After creating an function, you could already make some instances of it. The example below creates a <em>Color</em> class with a public variable called <em>value</em>. After that we create 2 instances of the <em>Color</em> class with some other values.</p><p>Note: I use the same code conventions as Actionscript: Classes should be <em>UpperCased</em>, functions/vars should be <em>lowerCased </em>and constants should be <em>FULL_CAPS</em>. Oh and since javascript looks like as1.0, I sometimes use an underscore before private vars <img src='http://blog.stroep.nl/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /></p><div class="bbCSH" style="font-family: monospace;"> function Color( value )<br /> {<br /> &nbsp; &nbsp; this.value = value || 0;<br /> &nbsp; &nbsp; alert(&quot;Color created with value: &quot; + value)<br /> }</p><p>var myRedColor = new Color(0xFF0000); // create instance of Color &nbsp;(a red one)<br /> var myOrangeColor = new Color(0xFFCC00); // create instance of Color &nbsp;(an orange one)<br /> alert( myRedColor.value );<br /> alert( myOrangeColor.value );<br /> &nbsp;</div><h2>Public / Private</h2><p>In actionscript it is very useful to use <a href="http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming)">encapsulation</a>, which is a mechanism for restricting access to other objects. The previous+following example should work in all browsers. The difference between public and private vars/functions in javascript can be declared like this:</p><div class="bbCSH" style="font-family: monospace;"> function Color( value )<br /> {<br /> &nbsp; &nbsp; // public variable<br /> &nbsp; &nbsp; this.value = value || 0xFFFFFF; // set default value to 0xFFFFFF for parameter if it isn&#8217;t defined</p><p>&nbsp; &nbsp; // private variable<br /> &nbsp; &nbsp; var _name = &quot;test&quot;;</p><p>&nbsp; &nbsp;// public function<br /> &nbsp; &nbsp;this.getRandomColor = function( )<br /> &nbsp; {<br /> &nbsp; &nbsp; &nbsp;return Math.random() * 0xFFFFFF;<br /> &nbsp; }</p><p>&nbsp; // private function<br /> &nbsp; function getNiceColor()<br /> &nbsp; {<br /> &nbsp; &nbsp; &nbsp;return 0xffcc00;<br /> &nbsp; }<br /> }</p><p>// create instance of Color<br /> var color = new Color(0xFF0000);<br /> alert( color.value ); // returns red color<br /> alert( color.getRandomColor() ); // returns random color</p><p>// not possible :<br /> alert( color.getNiceColor() ); error in console; property does not exist, because function is private.<br /> alert( color._name ); // error in console; property does not exist, because variable is private.<br /> &nbsp;</div><p>I think with this principles (maybe in combination with namespaces, see below) you can create clean javascript code.</p><h2>Packages &gt; Namespaces</h2><p>Now before using classes in javascript I had some conflicts with function and variable names because there were unwanted duplicates. I have written a little helper tool to use namespaces. It helps to prevent those conflicts. Now it looks more like Actionscript 3, only there is no such thing like &#8216;imports&#8217;. This snippet works in all browsers.</p><div class="bbCSH" style="font-family: monospace;"> /// create namespace like &#8216;com.yourcompany.projectname&#8217;<br /> function Namespace(namespace)<br /> {<br /> &nbsp; &nbsp; var parts = namespace.split(&quot;.&quot;);<br /> &nbsp; &nbsp; var root = window;<br /> &nbsp; &nbsp; for(var i = 0; i &lt; parts.length; i++)<br /> &nbsp; &nbsp; {<br /> &nbsp; &nbsp; &nbsp; &nbsp; if(typeof root[parts[i]] == &quot;undefined&quot;) <br /> &nbsp; &nbsp; &nbsp; &nbsp; {<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; root[parts[i]] = {};<br /> &nbsp; &nbsp; &nbsp; &nbsp; }<br /> &nbsp; &nbsp; &nbsp; &nbsp; root = root[parts[i]];<br /> &nbsp; &nbsp; }<br /> }</p><p>// creating my own namespace here<br /> Namespace(&quot;nl.stroep.utils&quot;);<br /> nl.stroep.utils.Color = function(value) // create class inside package<br /> {<br /> &nbsp; this.value = value || 0xFFFFFF;<br /> }</p><p>var myRedColor = &nbsp;new nl.stroep.utils.Color(0xff0000);<br /> alert(myRedColor.value);<br /> &nbsp;</div><h2>Getters and Setters</h2><p>Now it is possible to use getters and setters. NOTE (!): Ofcourse this method does not work in IE, so this is pointless but also a bit cool to see how far we could go. Anyway there are 2 ways to define them. Read more about it <a href="http://ejohn.org/blog/javascript-getters-and-setters/">here</a>. I think the <em>__defineGetter__</em> way is better; it looks ugly but you still have access to private objects.</p><p>Take a look at this getter/setter declaration example. It&#8217;s not as clean as AS3 getters/setters but it works like a charm in my browser.</p><div class="bbCSH" style="font-family: monospace;"> function Color( value )<br /> {</p><p>&nbsp; &nbsp;/* public getter */<br /> &nbsp; &nbsp;this.__defineGetter__(&quot;value&quot;, function()<br /> &nbsp; &nbsp;{<br /> &nbsp; &nbsp; &nbsp; &nbsp; alert(&quot;getter called; current value: &quot; +value);<br /> &nbsp; &nbsp; &nbsp; &nbsp; return value;<br /> &nbsp; &nbsp; });<br /> &nbsp; &nbsp; <br /> &nbsp; &nbsp;/* public setter */<br /> &nbsp; &nbsp;this.__defineSetter__(&quot;value&quot;, function(val)<br /> &nbsp; &nbsp;{<br /> &nbsp; &nbsp; &nbsp; &nbsp; alert(&quot;setter called; new value: &quot; +val);<br /> &nbsp; &nbsp; &nbsp; &nbsp; value = val;<br /> &nbsp; &nbsp; });<br /> &nbsp; &nbsp;<br /> &nbsp; &nbsp;// public variable. For a strange reason I have to put this below the get/set definition.<br /> &nbsp; &nbsp;this.value = value || 0xFFFFFF;<br /> }</p><p>// create instance of Color<br /> var color = new Color(0xFF0000);<br /> color.value; // getter<br /> color.value = 0xff0000; // setter<br /> &nbsp;</div><h2>Constants</h2><p>Javascript has a <em>const</em> too, you could use it instead of <em>var</em>. Ofcourse this is not implemented in IE, so it is also pretty pointless to use.</p><h2>Mix them all</h2><p>Now lets use namespaces, getters/setters and private/public variables and functions together.<br /> This is a simplified ported version of <a href="http://code.google.com/p/stroep/source/browse/trunk/flashsources/utils%20classes/nl/stroep/utils/Color.as">this Color Class</a>.<br /> With this class you can modify the red,green and blue channels of a color individually. In the original class it is possible to lighten/darken the color too, but for this post I left these functions, because this only illustrates the possibilities of nice OO javascript.</p><div class="bbCSH" style="font-family: monospace;">Namespace(&quot;nl.stroep.utils&quot;);<br /> nl.stroep.utils.Color = function( color )<br /> {&nbsp; &nbsp;<br /> &nbsp; &nbsp; /* PUBLIC FUNCTIONS */<br /> &nbsp; &nbsp; <br /> &nbsp; &nbsp; this.grayscale = function( val )<br /> &nbsp; &nbsp; {<br /> &nbsp; &nbsp; &nbsp; &nbsp; val = val || 0;<br /> &nbsp; &nbsp; &nbsp; &nbsp; if (val &lt; 0){ val = 0 }<br /> &nbsp; &nbsp; &nbsp; &nbsp; if (val &gt; 255) { val = 255 }<br /> &nbsp; &nbsp; &nbsp; &nbsp; <br /> &nbsp; &nbsp; &nbsp; &nbsp; return (val &lt;&lt; 16) | (val &lt;&lt; 8 ) | val;<br /> &nbsp; &nbsp; }<br /> &nbsp; &nbsp; <br /> &nbsp; &nbsp; /* PRIVATE FUNCTIONS */<br /> &nbsp; &nbsp; <br /> &nbsp; &nbsp; function limit( val, lowerLimit, upperLimit )<br /> &nbsp; &nbsp; {<br /> &nbsp; &nbsp; &nbsp; &nbsp; if (val &lt; lowerLimit){ return lowerLimit }<br /> &nbsp; &nbsp; &nbsp; &nbsp; if (val &gt; upperLimit) { return upperLimit }<br /> &nbsp; &nbsp; &nbsp; &nbsp; return val;<br /> &nbsp; &nbsp; }<br /> &nbsp; &nbsp; &nbsp; &nbsp; <br /> &nbsp; &nbsp; /* PUBLIC GETTER FUNCTIONS */<br /> &nbsp; &nbsp; <br /> &nbsp; &nbsp; this.__defineGetter__(&quot;value&quot;, function()<br /> &nbsp; &nbsp; {<br /> &nbsp; &nbsp; &nbsp; &nbsp; return (_red &lt;&lt; 16) | (_green &lt;&lt; 8 ) | _blue; <br /> &nbsp; &nbsp; })<br /> &nbsp; &nbsp; <br /> &nbsp; &nbsp; this.__defineGetter__(&quot;red&quot;, function()<br /> &nbsp; &nbsp; {<br /> &nbsp; &nbsp; &nbsp; &nbsp; return _red;<br /> &nbsp; &nbsp; })<br /> &nbsp; &nbsp; <br /> &nbsp; &nbsp; this.__defineGetter__(&quot;green&quot;, function()<br /> &nbsp; &nbsp; {<br /> &nbsp; &nbsp; &nbsp; &nbsp; return _green;<br /> &nbsp; &nbsp; })<br /> &nbsp; &nbsp; <br /> &nbsp; &nbsp; this.__defineGetter__(&quot;blue&quot;, function()<br /> &nbsp; &nbsp; {<br /> &nbsp; &nbsp; &nbsp; &nbsp; return _blue;<br /> &nbsp; &nbsp; })<br /> &nbsp; &nbsp; <br /> &nbsp; &nbsp; /* PUBLIC SETTER FUNCTIONS */<br /> &nbsp; &nbsp; <br /> &nbsp; &nbsp; this.__defineSetter__(&quot;value&quot;, function(val)<br /> &nbsp; &nbsp; {<br /> &nbsp; &nbsp; &nbsp; &nbsp; _red &nbsp;= val &gt;&gt; 16 &amp; 0xFF; // red<br /> &nbsp; &nbsp; &nbsp; &nbsp; _green &nbsp;= val &nbsp;&gt;&gt; 8 &amp; 0xFF; // green<br /> &nbsp; &nbsp; &nbsp; &nbsp; _blue = val &amp; 0xFF; // blue<br /> &nbsp; &nbsp; &nbsp; &nbsp; <br /> &nbsp; &nbsp; &nbsp; &nbsp; _value = val;<br /> &nbsp; &nbsp; })<br /> &nbsp; &nbsp; <br /> &nbsp; &nbsp; this.__defineSetter__(&quot;red&quot;, function(val)<br /> &nbsp; &nbsp; {<br /> &nbsp; &nbsp; &nbsp; &nbsp; _red = val;<br /> &nbsp; &nbsp; &nbsp; &nbsp; _red = limit( _red, 0, 255 );<br /> &nbsp; &nbsp; })<br /> &nbsp; &nbsp; <br /> &nbsp; &nbsp; this.__defineSetter__(&quot;green&quot;, function(val)<br /> &nbsp; &nbsp; {<br /> &nbsp; &nbsp; &nbsp; &nbsp; _green = val;<br /> &nbsp; &nbsp; &nbsp; &nbsp; _green = limit( _green, 0, 255 );<br /> &nbsp; &nbsp; })<br /> &nbsp; &nbsp; <br /> &nbsp; &nbsp; this.__defineSetter__(&quot;blue&quot;, function(val)<br /> &nbsp; &nbsp; {<br /> &nbsp; &nbsp; &nbsp; &nbsp; _blue = val;<br /> &nbsp; &nbsp; &nbsp; &nbsp; _blue = limit( _blue, 0, 255 );<br /> &nbsp; &nbsp; })<br /> &nbsp; &nbsp; <br /> &nbsp; &nbsp; /* PRIVATE VARIABLES */<br /> &nbsp; &nbsp; var _value = color;<br /> &nbsp; &nbsp; var _red;<br /> &nbsp; &nbsp; var _green;<br /> &nbsp; &nbsp; var _blue;<br /> &nbsp; &nbsp; <br /> &nbsp; &nbsp; /* PUBLIC VARIABLES */<br /> &nbsp; &nbsp; this.value = _value;<br /> &nbsp; &nbsp; <br /> };<br /> &nbsp;</div><p>With this javascript class, you could use it like this:</p><div class="bbCSH" style="font-family: monospace;"> var color = new nl.stroep.utils.Color(0xFFCC00) // define orange.<br /> color.green = 0; // remove green.. now it is red..<br /> color.blue = 255; // add some blue.. now it is purple..<br /> alert(color.value.toString(16)) // alerts FF00FF and that is purple.<br /> &nbsp;</div><p>Hope you enjoyed this article. Feel free to share or comment.</p><p><a href="http://blog.stroep.nl/?flattrss_redirect&amp;id=1146&amp;md5=2a3c43bbcb2a14cb40dd0ee0e476f64f" title="Flattr" target="_blank"><img src="http://blog.stroep.nl/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded> <wfw:commentRss>http://blog.stroep.nl/2011/01/javascript-for-as3-developers/feed/</wfw:commentRss> <slash:comments>8</slash:comments> </item> <item><title>Web Design Tips for 2011:How to be a Better Flash Developer</title><link>http://blog.stroep.nl/2011/01/tips-to-start-a-flash-project/</link> <comments>http://blog.stroep.nl/2011/01/tips-to-start-a-flash-project/#comments</comments> <pubDate>Sun, 02 Jan 2011 19:31:00 +0000</pubDate> <dc:creator>Mark Knol</dc:creator> <category><![CDATA[actionscript]]></category> <category><![CDATA[as3]]></category> <category><![CDATA[code]]></category><guid isPermaLink="false">http://blog.stroep.nl/?p=958</guid> <description><![CDATA[<img alt="" src="http://blog.stroep.nl/wp-content/3370961087_3779c4f4e7-150x150.jpg" title="Tips" class="alignleft" width="100" height="100" />I hope 2011 will be a successful year! This post is mostly targeting to developers who like to dive deeper into actionscript and are searching for a another way to start a project. ]]></description> <content:encoded><![CDATA[<p><img alt="" src="http://blog.stroep.nl/wp-content/3370961087_3779c4f4e7-150x150.jpg" title="Tips" class="alignleft" width="100" height="100" /><br /><h3>I hope 2011 will be a successful year!</h3><p>This post is mostly targeting to developers who like to dive deeper into actionscript and are searching for a another way to start a project, and not so much for the newbies to web design or those just starting out in <a href="http://www.onlinewebdesigndegree.com/">online web design</a> degree programs. In my opinion there are missing a lot of these types of posts, so I just share mine. This post is a collection of post I have never posted in 2010. I hope it will trigger more flashdevelopers to share there view on this topic. Disclaimer: There are several ways to start a flashproject, and I don&#8217;t think my examples are the best way, but I hope they challenge you to think about your setup. In case you work in a webteam, it&#8217;s important to work in a way that is understandable and transparent. You should talk to each other what you really need and expect from each others code.</p><h2>Use existing frameworks (?|!)</h2><p>There are companies who use very transparent architecture frameworks like <a href="http://puremvc.org/">PureMVC</a> or <a href="http://www.robotlegs.org/">RobotLegs</a>, others use there own frameworks and build on that. Personally I don&#8217;t use that big frameworks (yet) as architecture base, because I don&#8217;t create that large applications/sites and for now its more overkill than helpful I guess. There are also some helper frameworks like <a href="http://casalib.org/">casalib</a> and <a href="http://code.google.com/p/templelibrary/">templelibrary</a> around, which can provide you a set of (util-)classes and interfaces. This post is not created to discuss if its better or not to use these already accepted frameworks. If it fits your needs and you want to standardize flash coding; just use it; they are great and very helpful. At the other hand some (mostly larger) frameworks are hard to understand and requires some learning curves / basic skills. I am currently working on <a href="http://flashflowfactory.stroep.nl/">flashflowfactory</a>, which is designed to be an easy-to-learn framework, mostly for medium/small websites which needs deeplinking and page transitions; you should check it out. So at the other hand, it is cool to create your own framework and you will learn a lot from it, but there is already lots of good stuff on the internets.</p><h2>Building</h2><p><strong>Get your tools</strong><br /> For most projects I&#8217;d love to use FlashDevelop. It&#8217;s my code editor of choice. I&#8217;m Dutch and this is a free tool, so there is no need to look at other tools <img src='http://blog.stroep.nl/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> Download and install it and follow the instructions. Install the <a href="http://www.adobe.com/support/flashplayer/downloads.html">latest debug activeX player</a> and download <a href="http://opensource.adobe.com/wiki/display/flexsdk/Flex+SDK">the latest Flex SDK</a>. After you tried FlashDevelop for a while, just donate some money to support the project. Even if your Dutch. Just give back. Oh, and download the <a href="http://www.flashdevelop.org/community/viewtopic.php?t=2993">duplicate plugin</a> and you don&#8217;t want to switch to another tool ever.</p><p><strong>Setup of the site assets</strong><br /> I love to store my assets inside an SWC, which are viewable (with some logic) in Flash IDE too. Mostly I use the Flash IDE to generate an SWC. All my &#8216;views&#8217; like pages, vectorimages, bitmaps, icons and advanced animations are stored inside that SWC. This of course except the assets I want to load from external sources. I don&#8217;t use much code on the timeline, except some play/stop()-commandos, because it stopping an animation cannot be done more clear than on the timeline. I don&#8217;t feel ashamed anymore to say that I sometimes put code on the timeline (think of a preloader, framelabel jumps etc). As long as you keep the project structured, this should never be a problem. All my views (that need to be accessible from code) are linked to external classes. In this way I can get the most out of the Flash IDE too; there are lots of animation- and designthings that cannot be done quickly with code. So don&#8217;t forget the Flash IDE; It&#8217;s a really powerful tool. Don&#8217;t loose your creativity on code only.</p><p><strong>So what is the plan?</strong><br /> It&#8217;s important to plan your website/application. If you want to be serious, at least just make a small plan. In case of a webteam: Make a checklist what should be done, where you are currently working on and what is done. Having a list of todo&#8217;s helps to be more open. In case of a personal project: Most projects fail if you don&#8217;t write things down, because you loose overview and also the interest for your potential killer-app. Just break things down into small peaces.</p><h2>Getting control</h2><p><strong>What do you need to get started?</strong><br /> Some people start creating cool UML diagrams, others just grab some piece of paper. Others start in Photoshop creating the design rightaway, but it would be helpful to create a wireframe in some cases. What the most important thing is, is to be aware of what you are going to create and which classes you need. Don&#8217;t start to think about the features (yet), think plain. What is the essence of this app? What does it needs to do before it &#8216;works&#8217;? What is needed to get to this point, and how would you built it? Think about how to setup a useful way for navigation. You want to use deeplinking? At what point/place should this happen? What kind of events do we need? Are there recurring elements? Which things should be (dynamic) components, what should be fixed/hardcoded? Which creative parts are hard to develop, and which parts should be more creative? Also add to your todo-list the hours you think it would take to build it; you get more awareness of how to deal with time managing.</p><p><strong>Start collecting puzzle pieces</strong><br /> It&#8217;s time to collect things we need. It maybe sounds stupid, but why wouldn&#8217;t you start with collection the global things? These things are important. Think about if you want to use frameworks and which parts you would code yourself. Are you going to use programmatic tweens? You could use a tween engine. Create a basic HTML file, embed the flash already. Create your package-structure. I don&#8217;t know if its is the way, but I love to write down stub- or pseudo code. Create as much empty classes which need to be filled. Keep in mind you need to refactor a lot of times if you start like this, but I don&#8217;t mind. This forces me to keep thinking about the structure, refactoring is a good thing, right?</p><p><strong>Learn from the past; find recurring elements</strong><br /> Now I have created a mini framework for setting up a website with pages. In my professional life I use Flash to create a lot of viral websites. Most of the pages are not the same, every design is different and has it&#8217;s own &#8216;thing&#8217;. So most of the times I find it difficult to fit this into a system. At the other hand, I like to prevent to have too much abstract classes; I want too keep things clear.  However the challenge is to find the recurring elements and create a system which can be reused later. This is one of the reasons why I am developing flashflowfactory. Some examples; I always want to have deeplinking, I want to have a transition when I go from <em>page A</em> to <em>page B</em>. And I would love to have the ability to go from <em>page A</em> to <em>page C</em> etc. I also want to send simple events/notifications to anywhere in the application. Well the framework covers that already, however it is still in progress. I love to keep its simplicity in usage, but also the fact I already used it in multiple projects and it slowly satisfies my everchanging needs.</p><p><strong>Keep the freedom</strong><br /> From the coding perspective, I love to have a structured project, even if the designs are not that consequent. Mostly I start with creating the raw pages. These are mostly the starting point. I have already said I am using SWC&#8217;s for the views; I point the base Class of a MovieClip to my classfiles (.as). Inside my class file I say it extends Sprite. As Classname I mostly give the same path, only I add <em>VC</em> (ViewComponent) after the name. For example; When creating a contact page, I use &#8216;<em>ContactPage</em>&#8216; as base class, and &#8216;<em>ContactPageVC</em>&#8216; as class name. This allows me to add the view from code (by adding the view-component) or to just put it directly from the library on the stage inside the Flash IDE. Yes, it is a bit pain in the ass to apply this to lots of pages/components, but it gives freedom of how to use your assets. If anyone knows a better/more simple solution, let me know!</p><p>Happy 2011! <img src='http://blog.stroep.nl/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> Please share your thoughts.</p><p><a href="http://blog.stroep.nl/?flattrss_redirect&amp;id=958&amp;md5=5a05fd499e4e32bb6eb076a555438a83" title="Flattr" target="_blank"><img src="http://blog.stroep.nl/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded> <wfw:commentRss>http://blog.stroep.nl/2011/01/tips-to-start-a-flash-project/feed/</wfw:commentRss> <slash:comments>5</slash:comments> </item> </channel> </rss>
