<?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/tag/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>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> <item><title>Actionscript 3 to vector graphic</title><link>http://blog.stroep.nl/2009/08/actionscript-3-to-vector-file/</link> <comments>http://blog.stroep.nl/2009/08/actionscript-3-to-vector-file/#comments</comments> <pubDate>Mon, 03 Aug 2009 19:57:14 +0000</pubDate> <dc:creator>Mark Knol</dc:creator> <category><![CDATA[actionscript]]></category><guid isPermaLink="false">http://blog.stroep.nl/?p=690</guid> <description><![CDATA[<img src="http://blog.stroep.nl/wp-content/behind.jpg" alt="Actionscript to vector graphic" title="Actionscript to vector graphic" width="100" height="100" class="alignleft size-full wp-image-586" />I like to generate art and objects with Flash/actionscript. Mostly I use a bitmapdata for that and export this as a PNG-24. This works perfect in most cases. But sometimes I wish there was a simple way to export my art to a scalable vector graphic. This would save a lot of data, and I'll be able to create unlimited large and sharp prints.I started searching and found some nice-to-know options.]]></description> <content:encoded><![CDATA[<p><img src="http://blog.stroep.nl/wp-content/behind.jpg" alt="Actionscript to vector graphic" title="Actionscript to vector graphic" width="100" height="100" class="alignleft size-full wp-image-586" />I like to generate art and objects with Flash/actionscript. Mostly I use a bitmapdata for that and export this as a PNG-24. This works perfect in most cases. But sometimes I wish there was a simple way to export my art to a scalable vector graphic. This would save a lot of data, and I&#8217;ll be able to create unlimited large and sharp prints.</p><p>I started searching and found some nice-to-know options.<br /> <br clear="both"/><strong>Save as PDF</strong><br /> If you have a PDF-printer installed, you could save the vectors as PDF (drawed with lineTo, curveTo etc). Just right-click on your flashmovie, and choose &#8216;<em>print..</em>&#8216;. Then choose &#8216;<em>Adobe PDF</em>&#8216; or another <a href="http://www.google.com/search?q=PDF+printer+driver" target="_blank">PDF printer</a>. This is very simple!<br /> Downside; My flash movie is becoming very slow when I create a lot of shapes.. After all; That&#8217;s the reason I use BitmapData objects. Sometimes I let Flash render a whole night before saving it and maybe it create more then a million shapes. To be realistic; I think a million shapes wouldn&#8217;t be cool as vector graphic, I guess my Illustrator doesn&#8217;t like that or the printshop-dude starts crying <img src='http://blog.stroep.nl/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> However.. I think there are tools to optimize/clean shapes that could be helpful too.</p><p><strong>Open vector formats</strong><br /> I didn&#8217;t know this exist (because I never searched for something like this), but there is a lot information about a SVG file format. A lot of vector programs support it already and even modern browser could show SVG file formats too.<br /> I found <a href="http://code.google.com/p/as3svgrendererlib">this</a> and <a href="http://labs.zavoo.com/?p=7">this</a> link. The opposite would be an great idea; It could be cool if we could export SVG. While rendering we could write/add into a file. Then I could use a small bitmapdata object, just show it and render it real-time. Downside; I think SVG doesn&#8217;t support blendmodes or filters.</p><p>I really think there is a lot more to explore. If anyone have some other suggestions or ideas, feel free to post it.</p> ]]></content:encoded> <wfw:commentRss>http://blog.stroep.nl/2009/08/actionscript-3-to-vector-file/feed/</wfw:commentRss> <slash:comments>13</slash:comments> </item> <item><title>AS3 ValueObject generator &#8211; beta</title><link>http://blog.stroep.nl/2009/04/as3-valueobject-generator-beta/</link> <comments>http://blog.stroep.nl/2009/04/as3-valueobject-generator-beta/#comments</comments> <pubDate>Sat, 18 Apr 2009 15:21:59 +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=504</guid> <description><![CDATA[<img src="http://blog.stroep.nl/wp-content/vo.jpg" alt="" title="AS3 Class Generator" width="100" height="100" class="alignleft size-full wp-image-568" />Since I am using actionscript 3 and an external actionscript editor with nice code completion (FlashDevelop in my case), I am using a lot of value objects.Flashdevelop has no value object generator tool, so I've build one myself. It is beta.<strong>ValueObject generator</strong> <a href="http://projects.stroep.nl/ValueObjectGenerator/">http://projects.stroep.nl/ValueObjectGenerator/</a>]]></description> <content:encoded><![CDATA[<p><img src="http://blog.stroep.nl/wp-content/vo.jpg" alt="" title="AS3 Class Generator" width="100" height="100" class="alignleft size-full wp-image-568" />Since I am using actionscript 3 and an external actionscript editor with nice code completion (FlashDevelop in my case), I am using a lot of value objects.</p><p>Flashdevelop has no value object generator tool, so I&#8217;ve build one myself. It is beta.</p><p><strong>ValueObject generator</strong><br /> <a href="http://projects.stroep.nl/ValueObjectGenerator/">http://projects.stroep.nl/ValueObjectGenerator/</a><br /> <br style='clear:both'/><br /> <object type="application/x-shockwave-flash" width="400" height="300" data="http://www.flickr.com/apps/video/stewart.swf?v=71075" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"><param name="flashvars" value="intl_lang=en-us&#038;photo_secret=644e636b54&#038;photo_id=3451978609"></param><param name="movie" value="http://www.flickr.com/apps/video/stewart.swf?v=71075"></param><param name="bgcolor" value="#000000"></param><param name="allowFullScreen" value="true"></param><embed type="application/x-shockwave-flash" src="http://www.flickr.com/apps/video/stewart.swf?v=71075" bgcolor="#000000" allowfullscreen="true" flashvars="intl_lang=en-us&#038;photo_secret=644e636b54&#038;photo_id=3451978609" height="300" width="400"></embed></object></p> ]]></content:encoded> <wfw:commentRss>http://blog.stroep.nl/2009/04/as3-valueobject-generator-beta/feed/</wfw:commentRss> <slash:comments>3</slash:comments> </item> <item><title>Generative art &#8211; The sky isn&#8217;t the limit</title><link>http://blog.stroep.nl/2009/02/generative-art-the-sky-isnt-the-limit/</link> <comments>http://blog.stroep.nl/2009/02/generative-art-the-sky-isnt-the-limit/#comments</comments> <pubDate>Tue, 03 Feb 2009 18:26:13 +0000</pubDate> <dc:creator>Mark Knol</dc:creator> <category><![CDATA[Generative art]]></category> <category><![CDATA[actionscript]]></category> <category><![CDATA[typographic]]></category><guid isPermaLink="false">http://blog.stroep.nl/?p=357</guid> <description><![CDATA[<img src="http://blog.stroep.nl/wp-content/feel.jpg" alt="" title="feel" width="100" height="100" class="alignleft size-full wp-image-584" />We all love the sky. We make <a href="http://www.flickr.com/search/?q=sky&#038;w=all">millions</a> of <a href="http://www.flickr.com/search/?ct=3&#038;w=all&#038;q=clouds&#038;m=text">pictures</a> of it, because it different every second. It is inspirational, every time you look at is, whenever it is day or night. Inspired by the sky, which is a little bit gray in Holland right right now, I created these images. I actually love the color palette, there is a little winter and some spring feeling in it. :)I did something different this time; there are some typographic elements added. Just a few words that describe the feeling I have when looking at it.. What do you think about adding text to it?]]></description> <content:encoded><![CDATA[<p>We all love the sky. We make <a href="http://www.flickr.com/search/?q=sky&#038;w=all">millions</a> of <a href="http://www.flickr.com/search/?ct=3&#038;w=all&#038;q=clouds&#038;m=text">pictures</a> of it, because it different every second. It is inspirational, every time you look at is, whenever it is day or night. Inspired by the sky, which is a little bit gray in Holland right right now, I created these images. I actually love the color palette, there is a little winter and some spring feeling in it. <img src='http://blog.stroep.nl/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /></p><p>I did something different this time; there are some typographic elements added. Just a few words that describe the feeling I have when looking at it.. What do you think about adding text to it?</p><p><a href="http://www.flickr.com/photos/markknol/3249241064/" title="see.the.sky by mark knol, on Flickr"><img src="http://farm4.static.flickr.com/3490/3249241064_de89c3de67.jpg" width="500" height="500" alt="see.the.sky" /></a></p><p><a href="http://www.flickr.com/photos/markknol/3249176116/" title="feel.the.sky by mark knol, on Flickr"><img src="http://farm4.static.flickr.com/3068/3249176116_d9b84f5eca.jpg" width="500" height="500" alt="feel.the.sky" /></a></p><p>Created with actionscript. Render size: 10.000&#215;10.000 px.</p> ]]></content:encoded> <wfw:commentRss>http://blog.stroep.nl/2009/02/generative-art-the-sky-isnt-the-limit/feed/</wfw:commentRss> <slash:comments>2</slash:comments> </item> </channel> </rss>
