<?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; actionscript</title> <atom:link href="http://blog.stroep.nl/category/actionscript/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>2000 smilies, FlashDevelop, Molehill, M2D</title><link>http://blog.stroep.nl/2011/03/2000-smilies-flashdevelop-molehill-m2d/</link> <comments>http://blog.stroep.nl/2011/03/2000-smilies-flashdevelop-molehill-m2d/#comments</comments> <pubDate>Tue, 29 Mar 2011 23:58:29 +0000</pubDate> <dc:creator>Mark Knol</dc:creator> <category><![CDATA[actionscript]]></category><guid isPermaLink="false">http://blog.stroep.nl/?p=1202</guid> <description><![CDATA[<img src="http://blog.stroep.nl/wp-content/curves.jpg" alt="" title="Read more: Curves in motion" width="100" height="100" class="alignleft size-full wp-image-582" />I am really impressed by all <a href="http://blog.theflashblog.com/?p=2638">molehill demos</a> which already build. Molehill is the codename for GPU-accelerated rendering for flash. Read more about M2D and how to use Molehill in FlashDevelop and get more rendering powers.]]></description> <content:encoded><![CDATA[<p><img src="http://blog.stroep.nl/wp-content/curves.jpg" alt="" title="Read more: Curves in motion" width="100" height="100" class="alignleft size-full wp-image-582" />I am really impressed by all <a href="http://blog.theflashblog.com/?p=2638">molehill demos</a> which already build. Molehill is the codename for GPU-accelerated rendering for flash. However I was kinda depressed when visiting the presentation <a href="http://www.fitc.ca/events/presentations/presentation.cfm?event=115&#038;presentation_id=1535">Making A Molehill Out Of A Mountain</a> on <a href="http://www.fitc.ca/events/about/?event=115">FITC Amsterdam</a>. Accessing the GPU is really low-level. That is great news because it opens lots of possibilities, but it is depressing because it is low-level. Even if you are trying to be an pro/expert, you must be a freaking uber-talented geek to understand and be able to write shaders, assemblers, assembly (??) code which is needed for the real Molehill stuff. Respect for that. I am a geek, but that is way too difficult for me. I have tried to look to the content of <a href="http://away3d.googlecode.com/svn/trunk/broomstick/Away3D/src/com/adobe/utils/AGALMiniAssembler.as">AGALMiniAssembler</a> from adobe, but I think thats not written by real humans. <img src='http://blog.stroep.nl/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /></p><h2>Great frameworks</h2><p>My point is; we have to wait for great frameworks. There are already 3D engines which are molehill-enabled (Alternativa3D, Away3D and some others). The GPU gives render powers. A brilliant idea of <a href="http://www.flashrealtime.com">Tom Kcha</a> is <a href="http://www.flashrealtime.com/m2d/">M2D</a>. He came up with a 2D rendering system which uses the GPU inside a framework. I think this is a little jewel and is experiment-worthy, because I am using 2d content most of the time. The example codes are understandable. No freaking shaders, but normal AS3-like code. I think there will be more frameworks which will use the power of GPU rendering soon.</p><h2>Experimenting with M2D &#8211; Molehill 2d</h2><p>If you want to see my test with M2D, take a look at my two different versions of <a href="http://projects.stroep.nl/m2d/">2000 smilies</a>. I have noticed they both use a lot of CPU but the M2D version renders about 3x smoother. I think this whole idea of 2d content on GPU is awesome! <img src='http://blog.stroep.nl/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> Adobe should implement this in a way to the &#8216;normal flashplayer&#8217;. Yes, there are limitations in the current workflow. You are mostly limited to bitmaps only, so you&#8217;ll need to know something about real sprites and blitting. However if you are using the example files, it will convert lots of different types of object (movieclips, shapes, animated-gif-like-sprites) to bitmap-objects for you. Imaging if games will implement this, they will have more rendering powers for free; that is huge and promising.</p><p>On twitter I said M2D <a href="http://twitter.com/#!/mknol/status/52112683411050496">did not support alpha</a>, but in the latest version there is an alpha property too. I still wonder how much M2D could be optimized to get most out of 2d GPU rendering.</p><h2>Getting started with Molehill</h2><p>Now, you want to use Molehill to, right? <img src='http://blog.stroep.nl/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> Let&#8217;s do it! Setting up a molehill-enabled environment is relative easy if you are using FlashDevelop:</p><p>1. <a href="http://download.macromedia.com/pub/labs/flashplatformruntimes/incubator/flashplayer_inc_debug_ax_022711.exe">Download the activeX incubator FlashPlayer for windows</a>. IE uses activeX, and FlashDevelop tabs use IE. <img src='http://blog.stroep.nl/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /><br /> 2. Download <a href="http://www.flashdevelop.org/community/viewtopic.php?f=11&#038;t=7943">latest version of flashdevelop</a><br /> 3. Download <a href="http://opensource.adobe.com/wiki/display/flexsdk/Download+Flex+Hero">nightly build of the Flex SDK</a> (You&#8217;ll need 4.5.0.19786)<br /> 4. Download this <a href="http://download.macromedia.com/pub/labs/flashplatformruntimes/incubator/flashplayer_inc_playerglobal_022711.swc">flashplayer_inc_playerglobal_022711.swc</a>. This includes classes for Molehill. Rename it to <em>playerglobal.swc</em> and place it at <em>(flex_sdk location)\frameworks\libs\player\10.1</em><br /> 5. Create a new AS3 project in FlashDevelop, and target FlashPlayer version 10.1 in the <em>project properties</em>. Keep the properties open and choose to publish in a tab. Then navigate to tab <em>Compiler options</em>, and add <code style='background:#000'>-swf-version=13</code> to the <em>Additional Compiler Options</em>. In the <em>custom path to Flex SDK</em> (same tab) you&#8217;ll need to point to the Flex SDK folder you have just downloaded.</p><p>This should be enough to start building molehill projects.. and also to use M2D:</p><h2>Getting started with M2D</h2><p>Now, download the <a href="http://www.flashrealtime.com/m2d/">M2D.swc</a> (or <a href="https://github.com/egreenfield/M2D">latest version from github</a>) and add it to your lib-folder. Right-click > <em>add to library</em> and your basicly ready to start building 2d GPU-accelerated flashcontent!</p><p>The future is bright. Can&#8217;t wait to see more great+fast performing examples using this technology. Now.. start making cool shit already!</p><p><strong>More info:</strong><br /> <a href="http://www.flashrealtime.com/m2d/">http://www.flashrealtime.com/m2d/</a><br /> <a href="https://github.com/egreenfield/M2D">https://github.com/egreenfield/M2D</a><br /> <a href="http://projects.stroep.nl/m2d/">http://projects.stroep.nl/m2d/</a><br /> <a href="http://labs.adobe.com/technologies/flashplatformruntimes/incubator/">http://labs.adobe.com/technologies/flashplatformruntimes/incubator/</a><br /> <a href="http://blog.visualjazz.com.au/good-stuff/making-mountains-out-of-a-molehill-how-to/">http://blog.visualjazz.com.au/good-stuff/making-mountains-out-of-a-molehill-how-to/</a></p><p><a href="http://blog.stroep.nl/?flattrss_redirect&amp;id=1202&amp;md5=1be323594ae253e6bb109ad665a60598" 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/03/2000-smilies-flashdevelop-molehill-m2d/feed/</wfw:commentRss> <slash:comments>5</slash:comments> </item> <item><title>Wait a minute with setTimeout</title><link>http://blog.stroep.nl/2011/02/wait-a-minute-with-settimeout/</link> <comments>http://blog.stroep.nl/2011/02/wait-a-minute-with-settimeout/#comments</comments> <pubDate>Mon, 21 Feb 2011 07:00:44 +0000</pubDate> <dc:creator>Mark Knol</dc:creator> <category><![CDATA[actionscript]]></category> <category><![CDATA[Code snippets]]></category> <category><![CDATA[optimalisation]]></category><guid isPermaLink="false">http://blog.stroep.nl/?p=1180</guid> <description><![CDATA[<img src="/wp-content/snippet.jpg" alt="" title="snippet" width="100" height="100" class="alignleft size-full wp-image-588" />While working on a game, I created a function spawner. The idea was to make a function which would call a function like <em>createEnemy()</em> a hundred times with an interval. So I quickly created a static function. Read the full post to see that the setTimeout function should be handled with care.]]></description> <content:encoded><![CDATA[<p><img src="/wp-content/snippet.jpg" alt="" title="snippet" width="100" height="100" class="alignleft size-full wp-image-588" />While working on a game, I created a function spawner. The idea was to make a function which would call a function like <em>createEnemy()</em> a hundred times with an interval. So I quickly created a static function. Yes, I could use <a href="/2009/11/delayed-function-calling-chain/">Chain</a>, but this function was created in a minute and I haven&#8217;t thought of that class.<br clear="left"/></p><div class="bbCSH" style="font-family: monospace;"> <span style="color: #b1b100;">public</span> <span style="color: #0066CC;">static</span> <span style="color: #000000; ">function</span> create<span style="color: #66cc66;">&#40;</span>total:<span style="color: #0066CC;">int</span>, interval:<span style="color: #0066CC;">int</span>, func:<span style="color: #000000; ">Function</span>, delay:<span style="color: #0066CC;">int</span> = <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span><br /> <span style="color: #66cc66;">&#123;</span><br /> &nbsp; &nbsp; <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>; i &lt; total; ++i<span style="color: #66cc66;">&#41;</span> <br /> &nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; setTimeout<span style="color: #66cc66;">&#40;</span>func, delay + interval * i<span style="color: #66cc66;">&#41;</span>;<br /> &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br /> <span style="color: #66cc66;">&#125;</span><br /> &nbsp;</div><p>This function is easy to understand. Maybe it&#8217;s not needed as a new class, but it could be handy in a game. By the way; I use <em>setTimeout</em> a lot, it&#8217;s sometimes easier than the <em>Timer</em> class. You could call the function like this (if it is placed in a class called <em>Spawner</em>):</p><div class="bbCSH" style="font-family: monospace;"> Spawner.<span style="color: #006600;">create</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1000</span>, <span style="color: #cc66cc;">1000</span>, createEnemy<span style="color: #66cc66;">&#41;</span>; <br /> <span style="color: #808080; font-style: italic;">// a new enemy should be created after a second, a thousand times</span><br /> &nbsp;</div><p>While profiling this peace of code I found it is a bit intensive and could be optimized very simple.</p><p>So, what is the problem? This profiler shows the memory usage of a empty flash calling the example Spawner.create function:<br /> <img src="/wp-content/settimeout-loop1.jpg" alt="" title="settimeout-reference" width="575" class="alignnone size-full wp-image-1186" /><br /> I have used FlashPlayer 10.1 (release) and the FlashDevelop for profiling.</p><p>See the column &#8216;count&#8217;, which represents the current count of objects. For some reason there are a lot of &#8216;Functions&#8217; (2000?). There are also 1000x &#8216;Array&#8217; and &#8216;SetIntervalTimer&#8217;. Now I don&#8217;t know what that exactly means, but it is a lot, right? I haven&#8217;t used Arrays yet, so setTimeout must be using arrays internally. I think because the intervals are created inside a loop, they are already instantiated and there must be some references to objects too. Inside this test, the <em>createEnemy</em> function is empty, so it is a bit unexpected what happens here.</p><p>So I tried to rewrite the code a bit to see if it makes a difference if they are not instantiated at the same time, but create a new interval after one time-out has been called (after each other). It is not very hard to do, so here it is:</p><div class="bbCSH" style="font-family: monospace;"> <span style="color: #b1b100;">public</span> <span style="color: #0066CC;">static</span> <span style="color: #000000; ">function</span> create<span style="color: #66cc66;">&#40;</span>total:<span style="color: #0066CC;">int</span>, interval:<span style="color: #0066CC;">int</span>, func:<span style="color: #000000; ">Function</span>, delay:<span style="color: #0066CC;">int</span> = <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span><br /> <span style="color: #66cc66;">&#123;</span><br /> &nbsp; &nbsp; setTimeout<span style="color: #66cc66;">&#40;</span>createNext, delay, total &#8211; <span style="color: #cc66cc;">1</span>, interval, func, delay<span style="color: #66cc66;">&#41;</span>;<br /> <span style="color: #66cc66;">&#125;</span></p><p><span style="color: #b1b100;">private</span> <span style="color: #0066CC;">static</span> <span style="color: #000000; ">function</span> createNext<span style="color: #66cc66;">&#40;</span>total:<span style="color: #0066CC;">int</span>, interval:<span style="color: #0066CC;">int</span>, func:<span style="color: #000000; ">Function</span>, delay:<span style="color: #0066CC;">int</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span><br /> <span style="color: #66cc66;">&#123;</span><br /> &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>func != <span style="color: #000000; ">null</span><span style="color: #66cc66;">&#41;</span> <br /> &nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; func<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>total &gt; <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setTimeout<span style="color: #66cc66;">&#40;</span>createNext, interval, total &#8211; <span style="color: #cc66cc;">1</span>, interval, func, delay<span style="color: #66cc66;">&#41;</span>;<br /> &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br /> <span style="color: #66cc66;">&#125;</span></p><p>&nbsp;</p></div><p>The Spawner.create works the same as before, only it is not creating all intervals inside a loop, but passes a reference to the function to the new timeout. Now take a look at the profiler again:</p><p><img src="/wp-content/settimeout-reference1.jpg" alt="" title="settimeout memory with function reference" width="575" class="alignnone size-full wp-image-1182" /></p><p>There are less &#8216;Functions&#8217; and only 11 &#8216;SetIntervalTimer&#8217;, which could be considered as good. Conclusion: If you are using setTimeout, you should be aware how to use it and check if you could improve it in your own app/game.</p><p><a href="http://blog.stroep.nl/?flattrss_redirect&amp;id=1180&amp;md5=c67d9a85c2acabfd421691000e1231fe" 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/02/wait-a-minute-with-settimeout/feed/</wfw:commentRss> <slash:comments>1</slash:comments> </item> </channel> </rss>
