<?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; Code snippets</title> <atom:link href="http://blog.stroep.nl/category/code-snippets/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>JSFL dialog</title><link>http://blog.stroep.nl/2011/12/jsfl-dialog/</link> <comments>http://blog.stroep.nl/2011/12/jsfl-dialog/#comments</comments> <pubDate>Wed, 30 Nov 2011 23:35:04 +0000</pubDate> <dc:creator>Mark Knol</dc:creator> <category><![CDATA[Code snippets]]></category> <category><![CDATA[JSFL]]></category> <category><![CDATA[flash]]></category> <category><![CDATA[jsfl]]></category> <category><![CDATA[workflow]]></category><guid isPermaLink="false">http://blog.stroep.nl/?p=1392</guid> <description><![CDATA[<img src="http://blog.stroep.nl/wp-content/flash-ide.jpg" width="100" height="100" class="alignleft" alt="JSFL Dialog"/>While playing with JSFL and trying to create a faster interface for my <a href="http://blog.stroep.nl/2011/11/name-it-right-jsfl/" target="_blank">name it right.jsfl</a> I stumbled across a very interesting function inside JSFL. There is a powerful function called <em>fl.getDocumentDOM().xmlPanel</em> which shows dialogs using .xml-files. Actually you could use XUL (XML User Interface Language), which kinda looks like a mix of HTML and Flex. If you need a GUI for your JSFL script, you can open a nice custom dialog using XUL.]]></description> <content:encoded><![CDATA[<p><img src="http://blog.stroep.nl/wp-content/flash-ide.jpg" width="100" height="100" class="alignleft" alt="JSFL Dialog"/>While playing with JSFL and trying to create a faster interface for my <a href="http://blog.stroep.nl/2011/11/name-it-right-jsfl/" target="_blank">name it right.jsfl</a> I stumbled across a very interesting function inside JSFL. There is a powerful function called <em>fl.getDocumentDOM().xmlPanel</em> which shows dialogs using .xml-files. Actually you could use XUL (XML User Interface Language), which kinda looks like a mix of HTML and Flex. If you need a GUI for your JSFL script, you can open a nice custom dialog using XUL.</p><h2>Why create tools inside a tool?</h2><p>Yes, the Flash is a very nice IDE. But with JSFL you can extend it. You can build your own snippets and tools. It could be very helpful to automate some boring things. I think it is very powerful to have the ability to extend your environment for your everyday tasks.</p><h2>Show a dialog with JSFL</h2><p><strong>Create a dialog file</strong><br /> This is an example of XUL. Save it as <em>test-dialog.xml</em> in your <a href="http://blog.stroep.nl/2011/11/name-it-right-jsfl/#command-locations">local commands folder</a>.</p><div class="bbCSH" style="font-family: monospace;"> <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;dialog</span> <span style="color: #000066;">title</span>=<span style="color: #ff0000;">&quot;JSFL dialog&quot;</span> <span style="color: #000066;">buttons</span>=<span style="color: #ff0000;">&quot;accept, cancel&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span><br /> &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;vbox<span style="font-weight: bold; color: black;">&gt;</span></span></span><br /> &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;label</span> <span style="color: #000066;">control</span>=<span style="color: #ff0000;">&quot;myTextfield&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;Enter some text&quot;</span><span style="font-weight: bold; color: black;">/&gt;</span></span> &nbsp;<br /> &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;textbox</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;myTextfield&quot;</span><span style="font-weight: bold; color: black;">/&gt;</span></span><br /> &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;label</span> <span style="color: #000066;">control</span>=<span style="color: #ff0000;">&quot;myColor&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;Choose a nice color&quot;</span><span style="font-weight: bold; color: black;">/&gt;</span></span> &nbsp;<br /> &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;radiogroup</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;myColor&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span> &nbsp;<br /> &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;radio</span> <span style="color: #000066;">label</span>=<span style="color: #ff0000;">&quot;Orange&quot;</span><span style="font-weight: bold; color: black;">/&gt;</span></span> &nbsp;<br /> &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;radio</span> <span style="color: #000066;">selected</span>=<span style="color: #ff0000;">&quot;true&quot;</span> <span style="color: #000066;">label</span>=<span style="color: #ff0000;">&quot;Violet&quot;</span><span style="font-weight: bold; color: black;">/&gt;</span></span> &nbsp;<br /> &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;radio</span> <span style="color: #000066;">label</span>=<span style="color: #ff0000;">&quot;Yellow&quot;</span><span style="font-weight: bold; color: black;">/&gt;</span></span> &nbsp;<br /> &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/radiogroup<span style="font-weight: bold; color: black;">&gt;</span></span></span> &nbsp;<br /> &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/vbox<span style="font-weight: bold; color: black;">&gt;</span></span></span><br /> <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/dialog<span style="font-weight: bold; color: black;">&gt;</span></span></span><br /> &nbsp;</div><p><strong>Open the dialog with JSFL</strong><br /> Save it as <em>test-dialog.jsfl</em> in the same folder as the .xml file.</p><div class="bbCSH" style="font-family: monospace;"> // xmlPanel returns an Object with values of the dialog. <br /> var xmlPanelOutput = fl.getDocumentDOM().xmlPanel(fl.configURI + &quot;/Commands/test-dialog.xml&quot;);</p><p>if (xmlPanelOutput.dismiss == &quot;accept&quot;) // user confirms dialog<br /> {<br /> &nbsp; fl.trace(&quot;myTextfield: &quot; + xmlPanelOutput.myTextfield);<br /> &nbsp; fl.trace(&quot;myColor: &quot; + xmlPanelOutput.myColor);<br /> }<br /> else // user canceled dialog<br /> {<br /> &nbsp; <br /> }<br /> &nbsp;</div><p>Run it by clicking <em>&#8216;Commands&#8217; > &#8216;test-dialog&#8217;</em> in Flash; our custom dialog will show up. Nice! <img src='http://blog.stroep.nl/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /></p><h2>Give me more; I want dynamic dialogs</h2><p>If you check out the documentation of <em>fl.getDocumentDOM().xmlPanel</em>, you see you only can pass a uri to a xml file, but there is no way to pass a XML or Object to it. Adobe if you read this post; that would be nice. But I found a hack to create a dialog from JSFL by dynamically create a XML and save it as temporary file and open that one as xmlDialog. The file will be removed after showing the dialog.</p><div class="bbCSH" style="font-family: monospace;">function showXMLPanel(xmlString)<br /> {<br /> &nbsp; var tempUrl = fl.configURI + &quot;/Commands/temp-dialog-&quot; + parseInt(777 * 777) + &quot;.xml&quot;<br /> &nbsp; FLfile.write(tempUrl, xmlString);<br /> &nbsp; var xmlPanelOutput = fl.getDocumentDOM().xmlPanel(tempUrl);<br /> &nbsp; FLfile.remove(tempUrl);<br /> &nbsp; return xmlPanelOutput;<br /> }</div><p>This function displays a dialog from a (xml) string and returns the values like you would expect from the <em>xmlPanel</em>-function. You could use a XML object for this too, but I think it&#8217;s easier to hack some variables into it or make templates with strings.</p><div class="bbCSH" style="font-family: monospace;"> // create an XUL with JSFL<br /> var dialogXML = &#8221;;<br /> dialogXML += &#8216;&lt;dialog title=&quot;JSFL dynamic dialog&quot; buttons=&quot;accept, cancel&quot;&gt;&#8217;;<br /> dialogXML += &nbsp; &#8216;&lt;hbox&gt;&#8217;;<br /> dialogXML += &nbsp; &nbsp; &#8216;&lt;label value=&quot;Enter 2x something&quot;/&gt;&#8217;;<br /> dialogXML += &nbsp; &nbsp; &#8216;&lt;textbox id=&quot;myText1&quot; value=&quot;&quot;/&gt;&#8217;;<br /> dialogXML += &nbsp; &nbsp; &#8216;&lt;textbox id=&quot;myText2&quot; value=&quot;&quot;/&gt;&#8217;;<br /> dialogXML += &nbsp; &#8216;&lt;/hbox&gt;&#8217;;<br /> dialogXML += &#8216;&lt;/dialog&gt;&#8217;;</p><p>var xmlPanelOutput = showXMLPanel(dialogXML);<br /> if (xmlPanelOutput.dismiss == &quot;accept&quot;) // user confirms dialog<br /> {<br /> &nbsp; fl.trace(&quot;myText1: &quot; + xmlPanelOutput.myText1);<br /> &nbsp; fl.trace(&quot;myText2: &quot; + xmlPanelOutput.myText2);<br /> }<br /> else // user canceled dialog<br /> {<br /> &nbsp; <br /> }<br /> &nbsp;</div><p>Since you are dealing with strings, you can manipulate and add extra elements. This allows you to pass data from JSFL to your dialog.</p><h2>So, can you make panels too?</h2><p>I really hate custom panels, because I never get my workspace right for two screens (moving/closing/opening panels the whole day), so there is no room for other panels. I start loving JSFL-scripts because you can assign a shortcut-key to it, which runs the tool, without having a panel in my workspace.</p><h2>Conclusion</h2><p>I like to create handy snippets and will share more in the future. But if you thought (just like me) this is some kind of a new feature: <a href="http://twitter.com/#!/bit101/status/142003152454041600" target="_blank">It&#8217;s not</a>. <img src='http://blog.stroep.nl/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> Let me know if you have some nice references or JSFL scripts to share.</p><h2>Learn more:</h2><ul><li><a href="http://help.adobe.com/en_US/flash/cs/extend/flash_cs5_extending.pdf" target="_blank">JSFL Reference</a> (PDF)</li><li><a href="https://developer.mozilla.org/en/XUL_controls" target="_blank">XUL controls</a></li><li><a href="http://www.mozilla.org/projects/xul/" target="_blank">XUL reference</a></li></ul><p><a href="http://blog.stroep.nl/?flattrss_redirect&amp;id=1392&amp;md5=d89701ffee4197ea9f8c2ae86cd1b3dd" 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/12/jsfl-dialog/feed/</wfw:commentRss> <slash:comments>2</slash:comments> </item> <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>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>Flashflowfactory FlashDevelop assets</title><link>http://blog.stroep.nl/2011/01/flashflowfactory-flashdevelop-assets/</link> <comments>http://blog.stroep.nl/2011/01/flashflowfactory-flashdevelop-assets/#comments</comments> <pubDate>Sat, 29 Jan 2011 22:04:25 +0000</pubDate> <dc:creator>Mark Knol</dc:creator> <category><![CDATA[Code snippets]]></category> <category><![CDATA[flashdevelop]]></category> <category><![CDATA[flashflowfactory]]></category><guid isPermaLink="false">http://blog.stroep.nl/?p=1169</guid> <description><![CDATA[<img src="http://blog.stroep.nl/wp-content/flashflowfactory1.png" alt="" title="flashflowfactory" width="100" height="100" class="alignleft size-full wp-image-1023" />Quick post for all FlashDevelop and flashflowfactory users. I have created a projectfile and templates for flashdevelop.]]></description> <content:encoded><![CDATA[<p><img src="http://blog.stroep.nl/wp-content/flashflowfactory1.png" alt="" title="flashflowfactory" width="100" height="100" class="alignleft size-full wp-image-1023" />Quick post for all FlashDevelop and flashflowfactory users. I have created a projectfile and templates for flashdevelop.<br class='clear'/><br /> <a href="http://code.google.com/p/stroep/downloads/list"><img src="http://blog.stroep.nl/wp-content/fff-project.jpg" alt="" title="flashflowfactory project" width="543" height="479" class="alignnone size-full wp-image-1170" /></p><p><img src="http://blog.stroep.nl/wp-content/fff-template.jpg" alt="" title="flashflowfactory template" width="391" height="511" class="alignnone size-full wp-image-1171" /></a></p><p>When you have installed FlashDevelop, it will automatically extract/install itself.</p><p>&raquo; <strong><a href="http://code.google.com/p/stroep/downloads/list">Download here</a></strong> (<em>flashdevelop flashflowfactory assets.fdz</em>)</p><p>Related:<br /> &raquo; <a href="http://projects.stroep.nl/flashdevelop/">More flashdevelop assets</a></p><p><a href="http://blog.stroep.nl/?flattrss_redirect&amp;id=1169&amp;md5=7c9a76d0c40b37ae20b909760fcbf94c" title="Flattr" target="_blank"><img src="http://blog.stroep.nl/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded> <wfw:commentRss>http://blog.stroep.nl/2011/01/flashflowfactory-flashdevelop-assets/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Javascript for AS3 developers</title><link>http://blog.stroep.nl/2011/01/javascript-for-as3-developers/</link> <comments>http://blog.stroep.nl/2011/01/javascript-for-as3-developers/#comments</comments> <pubDate>Tue, 25 Jan 2011 23:46:17 +0000</pubDate> <dc:creator>Mark Knol</dc:creator> <category><![CDATA[Code snippets]]></category> <category><![CDATA[Javascript]]></category> <category><![CDATA[as3]]></category> <category><![CDATA[html5]]></category> <category><![CDATA[oop]]></category> <category><![CDATA[snippet]]></category><guid isPermaLink="false">http://blog.stroep.nl/?p=1146</guid> <description><![CDATA[<img src="http://blog.stroep.nl/wp-content/netscape_logo.jpg" alt="" title="Javascript for AS3 developers" width="100" height="100" class="alignleft size-full wp-image-1158" />This article is some training for your JavaScript skills. I love Actionscript , and I am also starting to like JavaScript. HTML5 is an hot topic today, and it seems using JavaScript is an important part of it, so why not learn more of it?]]></description> <content:encoded><![CDATA[<p><img src="http://blog.stroep.nl/wp-content/netscape_logo.jpg" alt="" title="Javascript for AS3 developers" width="100" height="100" class="alignleft size-full wp-image-1158" />This article is some training for your JavaScript skills. I love Actionscript , and I am also starting to like JavaScript. However, coding in JavaScript feels like Actionscript 1.0. It&#8217;s a pity there aren&#8217;t decent editors, especially if you want to write object-oriented-code, which <em>is</em> possible with JavaScript . At the moment I think VisualStudio is the best JavaScript editor, but I really wish there was a tool for JavaScript like FlashDevelop (a clean editor for Actionscript projects) with great intellisense, code completion and snippets etc. But we should not focus on tools, lets focus on the language and get the most out of it. HTML5 is an hot topic today, and it seems using JavaScript is an important part of it, so why not learn more of it? I have seen some <a href="http://mrdoob.com/">stunning HTML5 examples</a> over the last year. As a developer it is very interesting to see how others code JavaScript. By the way; Personally I find it very funny to see how JavaScript has changed it&#8217;s &#8216;image&#8217;. Some of us remember the good days in Netscape. JavaScript was .. *kuch* not fast and associated with ugly rainbow mouse pointers and aggressive popup screens. Now it is associated with HTML5, which is supposed to be the future and it will be bright <img src='http://blog.stroep.nl/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> I refuse to put my opinion about this topic here.<br /> I think the &#8216;image&#8217; of JS has slowly changed after great frameworks like jQuery have reached the developers. jQuery is awesome, I advice to use jQuery as default in all sites/apps.</p><p>Anyway, there are (ofcourse) some techniques to code like Actionscript in JavaScript. These snippets have helped me to understand object oriented JavaScript. For too long I did not even tried to learn what was possible with JavaScript because I thought it was a very limited language. Maybe some information is outdated; I am still learning, so please share your feedback.</p><h2>Object oriented javascript</h2><p>In javascript it is possible to create classes, public and private vars, getters/setters and there are even constructors. A function could be seen as a class. This is a bit strange at first, but if you use it for a while you will understand. Now lets create our own class right now.</p><p>Lets port some stuff of <a href="http://code.google.com/p/stroep/source/browse/trunk/flashsources/utils%20classes/nl/stroep/utils/Color.as">this Color Class</a> to javascript. It has private vars and public functions.</p><h2>Declare classes</h2><p>Defining classes is as easy as defining functions, because it is almost the same. After creating an function, you could already make some instances of it. The example below creates a <em>Color</em> class with a public variable called <em>value</em>. After that we create 2 instances of the <em>Color</em> class with some other values.</p><p>Note: I use the same code conventions as Actionscript: Classes should be <em>UpperCased</em>, functions/vars should be <em>lowerCased </em>and constants should be <em>FULL_CAPS</em>. Oh and since javascript looks like as1.0, I sometimes use an underscore before private vars <img src='http://blog.stroep.nl/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /></p><div class="bbCSH" style="font-family: monospace;"> function Color( value )<br /> {<br /> &nbsp; &nbsp; this.value = value || 0;<br /> &nbsp; &nbsp; alert(&quot;Color created with value: &quot; + value)<br /> }</p><p>var myRedColor = new Color(0xFF0000); // create instance of Color &nbsp;(a red one)<br /> var myOrangeColor = new Color(0xFFCC00); // create instance of Color &nbsp;(an orange one)<br /> alert( myRedColor.value );<br /> alert( myOrangeColor.value );<br /> &nbsp;</div><h2>Public / Private</h2><p>In actionscript it is very useful to use <a href="http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming)">encapsulation</a>, which is a mechanism for restricting access to other objects. The previous+following example should work in all browsers. The difference between public and private vars/functions in javascript can be declared like this:</p><div class="bbCSH" style="font-family: monospace;"> function Color( value )<br /> {<br /> &nbsp; &nbsp; // public variable<br /> &nbsp; &nbsp; this.value = value || 0xFFFFFF; // set default value to 0xFFFFFF for parameter if it isn&#8217;t defined</p><p>&nbsp; &nbsp; // private variable<br /> &nbsp; &nbsp; var _name = &quot;test&quot;;</p><p>&nbsp; &nbsp;// public function<br /> &nbsp; &nbsp;this.getRandomColor = function( )<br /> &nbsp; {<br /> &nbsp; &nbsp; &nbsp;return Math.random() * 0xFFFFFF;<br /> &nbsp; }</p><p>&nbsp; // private function<br /> &nbsp; function getNiceColor()<br /> &nbsp; {<br /> &nbsp; &nbsp; &nbsp;return 0xffcc00;<br /> &nbsp; }<br /> }</p><p>// create instance of Color<br /> var color = new Color(0xFF0000);<br /> alert( color.value ); // returns red color<br /> alert( color.getRandomColor() ); // returns random color</p><p>// not possible :<br /> alert( color.getNiceColor() ); error in console; property does not exist, because function is private.<br /> alert( color._name ); // error in console; property does not exist, because variable is private.<br /> &nbsp;</div><p>I think with this principles (maybe in combination with namespaces, see below) you can create clean javascript code.</p><h2>Packages &gt; Namespaces</h2><p>Now before using classes in javascript I had some conflicts with function and variable names because there were unwanted duplicates. I have written a little helper tool to use namespaces. It helps to prevent those conflicts. Now it looks more like Actionscript 3, only there is no such thing like &#8216;imports&#8217;. This snippet works in all browsers.</p><div class="bbCSH" style="font-family: monospace;"> /// create namespace like &#8216;com.yourcompany.projectname&#8217;<br /> function Namespace(namespace)<br /> {<br /> &nbsp; &nbsp; var parts = namespace.split(&quot;.&quot;);<br /> &nbsp; &nbsp; var root = window;<br /> &nbsp; &nbsp; for(var i = 0; i &lt; parts.length; i++)<br /> &nbsp; &nbsp; {<br /> &nbsp; &nbsp; &nbsp; &nbsp; if(typeof root[parts[i]] == &quot;undefined&quot;) <br /> &nbsp; &nbsp; &nbsp; &nbsp; {<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; root[parts[i]] = {};<br /> &nbsp; &nbsp; &nbsp; &nbsp; }<br /> &nbsp; &nbsp; &nbsp; &nbsp; root = root[parts[i]];<br /> &nbsp; &nbsp; }<br /> }</p><p>// creating my own namespace here<br /> Namespace(&quot;nl.stroep.utils&quot;);<br /> nl.stroep.utils.Color = function(value) // create class inside package<br /> {<br /> &nbsp; this.value = value || 0xFFFFFF;<br /> }</p><p>var myRedColor = &nbsp;new nl.stroep.utils.Color(0xff0000);<br /> alert(myRedColor.value);<br /> &nbsp;</div><h2>Getters and Setters</h2><p>Now it is possible to use getters and setters. NOTE (!): Ofcourse this method does not work in IE, so this is pointless but also a bit cool to see how far we could go. Anyway there are 2 ways to define them. Read more about it <a href="http://ejohn.org/blog/javascript-getters-and-setters/">here</a>. I think the <em>__defineGetter__</em> way is better; it looks ugly but you still have access to private objects.</p><p>Take a look at this getter/setter declaration example. It&#8217;s not as clean as AS3 getters/setters but it works like a charm in my browser.</p><div class="bbCSH" style="font-family: monospace;"> function Color( value )<br /> {</p><p>&nbsp; &nbsp;/* public getter */<br /> &nbsp; &nbsp;this.__defineGetter__(&quot;value&quot;, function()<br /> &nbsp; &nbsp;{<br /> &nbsp; &nbsp; &nbsp; &nbsp; alert(&quot;getter called; current value: &quot; +value);<br /> &nbsp; &nbsp; &nbsp; &nbsp; return value;<br /> &nbsp; &nbsp; });<br /> &nbsp; &nbsp; <br /> &nbsp; &nbsp;/* public setter */<br /> &nbsp; &nbsp;this.__defineSetter__(&quot;value&quot;, function(val)<br /> &nbsp; &nbsp;{<br /> &nbsp; &nbsp; &nbsp; &nbsp; alert(&quot;setter called; new value: &quot; +val);<br /> &nbsp; &nbsp; &nbsp; &nbsp; value = val;<br /> &nbsp; &nbsp; });<br /> &nbsp; &nbsp;<br /> &nbsp; &nbsp;// public variable. For a strange reason I have to put this below the get/set definition.<br /> &nbsp; &nbsp;this.value = value || 0xFFFFFF;<br /> }</p><p>// create instance of Color<br /> var color = new Color(0xFF0000);<br /> color.value; // getter<br /> color.value = 0xff0000; // setter<br /> &nbsp;</div><h2>Constants</h2><p>Javascript has a <em>const</em> too, you could use it instead of <em>var</em>. Ofcourse this is not implemented in IE, so it is also pretty pointless to use.</p><h2>Mix them all</h2><p>Now lets use namespaces, getters/setters and private/public variables and functions together.<br /> This is a simplified ported version of <a href="http://code.google.com/p/stroep/source/browse/trunk/flashsources/utils%20classes/nl/stroep/utils/Color.as">this Color Class</a>.<br /> With this class you can modify the red,green and blue channels of a color individually. In the original class it is possible to lighten/darken the color too, but for this post I left these functions, because this only illustrates the possibilities of nice OO javascript.</p><div class="bbCSH" style="font-family: monospace;">Namespace(&quot;nl.stroep.utils&quot;);<br /> nl.stroep.utils.Color = function( color )<br /> {&nbsp; &nbsp;<br /> &nbsp; &nbsp; /* PUBLIC FUNCTIONS */<br /> &nbsp; &nbsp; <br /> &nbsp; &nbsp; this.grayscale = function( val )<br /> &nbsp; &nbsp; {<br /> &nbsp; &nbsp; &nbsp; &nbsp; val = val || 0;<br /> &nbsp; &nbsp; &nbsp; &nbsp; if (val &lt; 0){ val = 0 }<br /> &nbsp; &nbsp; &nbsp; &nbsp; if (val &gt; 255) { val = 255 }<br /> &nbsp; &nbsp; &nbsp; &nbsp; <br /> &nbsp; &nbsp; &nbsp; &nbsp; return (val &lt;&lt; 16) | (val &lt;&lt; 8 ) | val;<br /> &nbsp; &nbsp; }<br /> &nbsp; &nbsp; <br /> &nbsp; &nbsp; /* PRIVATE FUNCTIONS */<br /> &nbsp; &nbsp; <br /> &nbsp; &nbsp; function limit( val, lowerLimit, upperLimit )<br /> &nbsp; &nbsp; {<br /> &nbsp; &nbsp; &nbsp; &nbsp; if (val &lt; lowerLimit){ return lowerLimit }<br /> &nbsp; &nbsp; &nbsp; &nbsp; if (val &gt; upperLimit) { return upperLimit }<br /> &nbsp; &nbsp; &nbsp; &nbsp; return val;<br /> &nbsp; &nbsp; }<br /> &nbsp; &nbsp; &nbsp; &nbsp; <br /> &nbsp; &nbsp; /* PUBLIC GETTER FUNCTIONS */<br /> &nbsp; &nbsp; <br /> &nbsp; &nbsp; this.__defineGetter__(&quot;value&quot;, function()<br /> &nbsp; &nbsp; {<br /> &nbsp; &nbsp; &nbsp; &nbsp; return (_red &lt;&lt; 16) | (_green &lt;&lt; 8 ) | _blue; <br /> &nbsp; &nbsp; })<br /> &nbsp; &nbsp; <br /> &nbsp; &nbsp; this.__defineGetter__(&quot;red&quot;, function()<br /> &nbsp; &nbsp; {<br /> &nbsp; &nbsp; &nbsp; &nbsp; return _red;<br /> &nbsp; &nbsp; })<br /> &nbsp; &nbsp; <br /> &nbsp; &nbsp; this.__defineGetter__(&quot;green&quot;, function()<br /> &nbsp; &nbsp; {<br /> &nbsp; &nbsp; &nbsp; &nbsp; return _green;<br /> &nbsp; &nbsp; })<br /> &nbsp; &nbsp; <br /> &nbsp; &nbsp; this.__defineGetter__(&quot;blue&quot;, function()<br /> &nbsp; &nbsp; {<br /> &nbsp; &nbsp; &nbsp; &nbsp; return _blue;<br /> &nbsp; &nbsp; })<br /> &nbsp; &nbsp; <br /> &nbsp; &nbsp; /* PUBLIC SETTER FUNCTIONS */<br /> &nbsp; &nbsp; <br /> &nbsp; &nbsp; this.__defineSetter__(&quot;value&quot;, function(val)<br /> &nbsp; &nbsp; {<br /> &nbsp; &nbsp; &nbsp; &nbsp; _red &nbsp;= val &gt;&gt; 16 &amp; 0xFF; // red<br /> &nbsp; &nbsp; &nbsp; &nbsp; _green &nbsp;= val &nbsp;&gt;&gt; 8 &amp; 0xFF; // green<br /> &nbsp; &nbsp; &nbsp; &nbsp; _blue = val &amp; 0xFF; // blue<br /> &nbsp; &nbsp; &nbsp; &nbsp; <br /> &nbsp; &nbsp; &nbsp; &nbsp; _value = val;<br /> &nbsp; &nbsp; })<br /> &nbsp; &nbsp; <br /> &nbsp; &nbsp; this.__defineSetter__(&quot;red&quot;, function(val)<br /> &nbsp; &nbsp; {<br /> &nbsp; &nbsp; &nbsp; &nbsp; _red = val;<br /> &nbsp; &nbsp; &nbsp; &nbsp; _red = limit( _red, 0, 255 );<br /> &nbsp; &nbsp; })<br /> &nbsp; &nbsp; <br /> &nbsp; &nbsp; this.__defineSetter__(&quot;green&quot;, function(val)<br /> &nbsp; &nbsp; {<br /> &nbsp; &nbsp; &nbsp; &nbsp; _green = val;<br /> &nbsp; &nbsp; &nbsp; &nbsp; _green = limit( _green, 0, 255 );<br /> &nbsp; &nbsp; })<br /> &nbsp; &nbsp; <br /> &nbsp; &nbsp; this.__defineSetter__(&quot;blue&quot;, function(val)<br /> &nbsp; &nbsp; {<br /> &nbsp; &nbsp; &nbsp; &nbsp; _blue = val;<br /> &nbsp; &nbsp; &nbsp; &nbsp; _blue = limit( _blue, 0, 255 );<br /> &nbsp; &nbsp; })<br /> &nbsp; &nbsp; <br /> &nbsp; &nbsp; /* PRIVATE VARIABLES */<br /> &nbsp; &nbsp; var _value = color;<br /> &nbsp; &nbsp; var _red;<br /> &nbsp; &nbsp; var _green;<br /> &nbsp; &nbsp; var _blue;<br /> &nbsp; &nbsp; <br /> &nbsp; &nbsp; /* PUBLIC VARIABLES */<br /> &nbsp; &nbsp; this.value = _value;<br /> &nbsp; &nbsp; <br /> };<br /> &nbsp;</div><p>With this javascript class, you could use it like this:</p><div class="bbCSH" style="font-family: monospace;"> var color = new nl.stroep.utils.Color(0xFFCC00) // define orange.<br /> color.green = 0; // remove green.. now it is red..<br /> color.blue = 255; // add some blue.. now it is purple..<br /> alert(color.value.toString(16)) // alerts FF00FF and that is purple.<br /> &nbsp;</div><p>Hope you enjoyed this article. Feel free to share or comment.</p><p><a href="http://blog.stroep.nl/?flattrss_redirect&amp;id=1146&amp;md5=2a3c43bbcb2a14cb40dd0ee0e476f64f" title="Flattr" target="_blank"><img src="http://blog.stroep.nl/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded> <wfw:commentRss>http://blog.stroep.nl/2011/01/javascript-for-as3-developers/feed/</wfw:commentRss> <slash:comments>8</slash:comments> </item> <item><title>Chain Tween</title><link>http://blog.stroep.nl/2010/10/chain-tween/</link> <comments>http://blog.stroep.nl/2010/10/chain-tween/#comments</comments> <pubDate>Sat, 30 Oct 2010 08:13:56 +0000</pubDate> <dc:creator>Mark Knol</dc:creator> <category><![CDATA[actionscript]]></category> <category><![CDATA[Code snippets]]></category> <category><![CDATA[as3]]></category> <category><![CDATA[chain]]></category> <category><![CDATA[googlecode]]></category><guid isPermaLink="false">http://blog.stroep.nl/?p=1043</guid> <description><![CDATA[<img src="http://blog.stroep.nl/wp-content/chain.jpg" alt="Chain" title="Chain" width="100" height="100" class="alignleft size-full wp-image-832">Previous year I was working on a little class called <a href="/2009/11/delayed-function-calling-chain/">Chain</a>. Now I was already working at a updated version which could animate too (see <a href="http://twitter.com/mknol/statuses/6142155230">this tweet</a> from last year). Basically it has become a small tween engine. You have to know there are much better tween engines, like TweenLite, Tweener, eaze tween, gtween etc.. So you don't need a new tween engine, and definitely not this one :) I just love to share some thoughts.]]></description> <content:encoded><![CDATA[<p><img src="http://blog.stroep.nl/wp-content/chain.jpg" alt="Chain" title="Chain" width="100" height="100" class="alignleft size-full wp-image-832">Previous year I was working on a little class called <a href="/2009/11/delayed-function-calling-chain/">Chain</a>. Chain is a delayed function-calling util class for AS3. Now I was already working at a updated version which could animate too (see <a href="http://twitter.com/mknol/statuses/6142155230">this tweet</a> from last year). Basically it has become a small tween engine. You have to know there are much better tween engines, like TweenLite, Tweener, eaze tween, gtween etc.. So you don&#8217;t need a new tween engine, and definitely not this one <img src='http://blog.stroep.nl/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> I just love to share some thoughts.</p><p>I just finished this to see how far I could come and where I have to deal with by creating such an engine. It is just fun to develop classes which will bring you to a higher level of coding, because I really want to become an actionscript sensai master or something. When starting the animation part, I was very inspired by <a href="http://philippe.elsass.me/2009/09/as3-designing-a-new-tween-library/">this post</a>, which has the same idea of combining jQuery-syntax-style with tweening, which I was already using in Chain.<br /> Making a class chainable is very easy, you just have to return the class instance on every public function, like this:</p><div class="bbCSH" style="font-family: monospace;"><span style="color: #0066CC;">package</span><br /> <span style="color: #66cc66;">&#123;</span><br /> &nbsp; <span style="color: #b1b100;">public</span> <span style="color: #000000; ">class</span> ExampleChain<br /> &nbsp; <span style="color: #66cc66;">&#123;</span><br /> &nbsp; &nbsp; <span style="color: #b1b100;">public</span> <span style="color: #000000; ">function</span> <span style="color: #0066CC;">add</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:ExampleChain<br /> &nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span>, <span style="color: #ff0000;">&quot;saying hi!&quot;</span><span style="color: #66cc66;">&#41;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #b1b100;">return</span> <span style="color: #0066CC;">this</span>;<br /> &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br /> &nbsp; <span style="color: #66cc66;">&#125;</span><br /> <span style="color: #66cc66;">&#125;</span><br /> &nbsp;</div><p> This already allows you to do this:<div class="bbCSH" style="font-family: monospace;"> <span style="color: #000000; ">var</span> chain:ExampleChain = <span style="color: #000000; ">new</span> ExampleChain<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br /> chain.<span style="color: #0066CC;">add</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #0066CC;">add</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #0066CC;">add</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #0066CC;">add</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br /> &nbsp;</div><p>I think this is very powerful and we should use more chaining.</p><p>Anyway, Chain was originally created to make delayed function calling. I wrapped code around this feature, so it is still usable like it was intended, only I improved some functions and added tweening. You can pass a complete handler to the play() function. And it is also possible to reverse the full chain, even with animation.</p><p>Here again, the original simple usage</p><div class="bbCSH" style="font-family: monospace;"> <span style="color: #000000; ">var</span> myChain:Chain = <span style="color: #000000; ">new</span> Chain<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>; &nbsp; &nbsp;<br /> myChain.<span style="color: #0066CC;">add</span><span style="color: #66cc66;">&#40;</span>one, <span style="color: #cc66cc;">2000</span><span style="color: #66cc66;">&#41;</span><br /> &nbsp; .<span style="color: #0066CC;">add</span><span style="color: #66cc66;">&#40;</span>two, <span style="color: #cc66cc;">500</span><span style="color: #66cc66;">&#41;</span><br /> &nbsp; .<span style="color: #0066CC;">add</span><span style="color: #66cc66;">&#40;</span>three, <span style="color: #cc66cc;">1000</span><span style="color: #66cc66;">&#41;</span><br /> &nbsp; .<span style="color: #0066CC;">play</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">3</span>, onChainComplete<span style="color: #66cc66;">&#41;</span>; &nbsp;<span style="color: #808080; font-style: italic;">// play 3 times</span></p><p><span style="color: #808080; font-style: italic;">// some functions</span><br /> <span style="color: #000000; ">function</span> one<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span> <span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;one&quot;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#125;</span><br /> <span style="color: #000000; ">function</span> two<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span> <span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;two&quot;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#125;</span><br /> <span style="color: #000000; ">function</span> three<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span> <span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;three&quot;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#125;</span><br /> <span style="color: #000000; ">function</span> onChainComplete<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span> <br /> &nbsp; &nbsp; <span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;done.&quot;</span><span style="color: #66cc66;">&#41;</span>; <br /> &nbsp; &nbsp; myChain.<span style="color: #006600;">destroy</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br /> <span style="color: #66cc66;">&#125;</span><br /> <span style="color: #808080; font-style: italic;">/* trace output:<br /> one two three one two three one two three done.<br /> */</span> <br /> &nbsp;</div><p>Animated chain example</p><div class="bbCSH" style="font-family: monospace;"> <span style="color: #000000; ">var</span> myChain:Chain = <span style="color: #000000; ">new</span> Chain<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>; &nbsp; &nbsp;<br /> myChain<br /> &nbsp; &nbsp;.<span style="color: #006600;">animate</span><span style="color: #66cc66;">&#40;</span>myMc, <span style="color: #66cc66;">&#123;</span>x:<span style="color: #cc66cc;">300</span>,y:<span style="color: #cc66cc;">100</span><span style="color: #66cc66;">&#125;</span>, <span style="color: #cc66cc;">700</span>, Easing.<span style="color: #006600;">elasticInOut</span><span style="color: #66cc66;">&#41;</span><br /> &nbsp; &nbsp;.<span style="color: #006600;">wait</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">200</span><span style="color: #66cc66;">&#41;</span><br /> &nbsp; &nbsp;.<span style="color: #006600;">animate</span><span style="color: #66cc66;">&#40;</span>myMc2, <span style="color: #66cc66;">&#123;</span>rotation:<span style="color: #cc66cc;">100</span>, alpha:<span style="color: #cc66cc;">0.5</span><span style="color: #66cc66;">&#125;</span>, <span style="color: #cc66cc;">500</span>, Easing.<span style="color: #006600;">quartIn</span><span style="color: #66cc66;">&#41;</span><br /> &nbsp; &nbsp;.<span style="color: #006600;">wait</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">200</span><span style="color: #66cc66;">&#41;</span><br /> &nbsp; &nbsp;.<span style="color: #0066CC;">play</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">2</span>, onChainComplete<span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// play 2 times</span></p><p><span style="color: #000000; ">function</span> onChainComplete<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span> <br /> &nbsp; &nbsp;<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;done.&quot;</span><span style="color: #66cc66;">&#41;</span>;<br /> &nbsp; &nbsp; myChain.<span style="color: #006600;">destroy</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br /> <span style="color: #66cc66;">&#125;</span><br /> &nbsp;</div><p>As you can see, you can still chain things, it animates multiple movieclips, and it can be played multiple times. It is very readable, which I really like. It works also in reversed order too, so you can reverse the full animation (pointless but cool)!</p><p>If you don&#8217;t want to animate, but instantly apply settings to a displayObject,<br /> you can use <em>apply(mc, {vars})</em>, of couse this equals <em>animate(mc,{vars},0)</em> but it is more elegant.</p><p>To control the Chain, you have can use:<br /> <em>stop()</em> Stop playing, use doContinue to play futher from current point<br /> <em>doContinue() </em> Continue playing after a stop<br /> <em>reset() </em> Reset indexes, start from first point. If reversed, start at last point</p><p>So how does it work? Well it is still based on the same old add() function. When you call the animate() function, internally, it is collecting the properties of the tween. Then it is calling the add() function, with a reference to a protected function <em>playTween()</em>, which is always the same. So when you play the chain, it just calling a local function every time, which handles the animation.</p><p>For now, I don&#8217;t have any special properties; no auto-alpha, no short-rotation, blurfilters or tinting etc. You can only animate properties of movieclips.</p><p>So how does tweening (animation over time) work? This is the formula I am using:</p><div class="bbCSH" style="font-family: monospace;"><span style="color: #0066CC;">_target</span><span style="color: #66cc66;">&#91;</span>property.<span style="color: #0066CC;">name</span><span style="color: #66cc66;">&#93;</span> = property.<span style="color: #006600;">startValue</span> + <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>property.<span style="color: #006600;">endValue</span> &#8211; property.<span style="color: #006600;">startValue</span><span style="color: #66cc66;">&#41;</span> * _easeFunc<span style="color: #66cc66;">&#40;</span>currentTime &#8211; _startTime<span style="color: #66cc66;">&#41;</span> / _duration<span style="color: #66cc66;">&#41;</span>;</div><p>This needs a little explanation. For example: When you add {x:100} as object to Chain for animation, &#8220;<em>x</em>&#8221; is the <em>property.name</em>, and <em>100</em> is the <em>property.endValue</em>. I am also getting current &#8220;<em>x</em>&#8221; value of the displayobject (which should move); this is the <em>property.startvalue</em>. Imaging this is currently 75.<br /> If you would write down the values into the formula, it looks like this:</p><div class="bbCSH" style="font-family: monospace;">myMc<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;x&quot;</span><span style="color: #66cc66;">&#93;</span> = <span style="color: #cc66cc;">75</span> + <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">100</span> &#8211; <span style="color: #cc66cc;">75</span><span style="color: #66cc66;">&#41;</span> * _easeFunc<span style="color: #66cc66;">&#40;</span>currentTime &#8211; _startTime<span style="color: #66cc66;">&#41;</span> / _duration<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</div><p>Now, as you can see, this is more readable. The only thing that is still vague is the ease function. Now I have to say this was a bit hard for me to understand at the first time, but it became very clear when I did understand what is does. I have read the book &#8216;Making things move&#8217; from Keith Peters (aka bit-101), which is a great reference and eye opening book for programmatic animation, trigonometry and other cool stuff. There is a chapter where animation over time is explained, which was really helpful creating the tween engine, and this time calculation. Now the time calculating returns a ratio, which is needed for the easing functions. I also studied some famous other tween engines; most of them use basically the same formula. By the way, the easing equations are used from Robert Penner.</p><p>Now, I really wanted to reverse the animations too. After some headache, the solutions was very simple. The time calculation is a value between 0-1, so I did 1 minus the time ratio <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;">_target</span><span style="color: #66cc66;">&#91;</span>property.<span style="color: #0066CC;">name</span><span style="color: #66cc66;">&#93;</span> = property.<span style="color: #006600;">startValue</span> + <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>property.<span style="color: #006600;">endValue</span> &#8211; property.<span style="color: #006600;">startValue</span><span style="color: #66cc66;">&#41;</span> * _easeFunc<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span> &#8211; <span style="color: #66cc66;">&#40;</span>currentTime &#8211; _startTime<span style="color: #66cc66;">&#41;</span> / _duration<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</div><p>Another problem I had to deal with was the fact that animating 1 thing at a time is very boring. So I invented the animateAsync(), which works almost exactly like the animate(), only you could pass the duration and the delay as separate values. So you can move a clip for 700 milliseconds, and call the next chained item after 75 milliseconds. To compare: when using the animate() function, if you move an item for 700 milliseconds, it will take 700 milliseconds when calling the next chained item. This could cause strange behaviors and isn&#8217;t easy to manage (especially when you want to wait till all animations are done), but I its a nice feature I think. Again, even the async&#8217;s can be played in reversed order.</p><div class="bbCSH" style="font-family: monospace;"> <span style="color: #000000; ">var</span> myChain:Chain = <span style="color: #000000; ">new</span> Chain<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>; &nbsp; &nbsp;<br /> myChain<br /> &nbsp; &nbsp;.<span style="color: #006600;">animateAsync</span><span style="color: #66cc66;">&#40;</span>myMc, <span style="color: #66cc66;">&#123;</span>x:<span style="color: #cc66cc;">300</span>,y:<span style="color: #cc66cc;">100</span><span style="color: #66cc66;">&#125;</span>, <span style="color: #cc66cc;">700</span>, <span style="color: #cc66cc;">10</span>, Easing.<span style="color: #006600;">elasticInOut</span><span style="color: #66cc66;">&#41;</span><br /> &nbsp; &nbsp;.<span style="color: #006600;">animate</span><span style="color: #66cc66;">&#40;</span>myMc2, <span style="color: #66cc66;">&#123;</span>rotation:<span style="color: #cc66cc;">100</span>, alpha:<span style="color: #cc66cc;">0.5</span><span style="color: #66cc66;">&#125;</span>, <span style="color: #cc66cc;">500</span>, Easing.<span style="color: #006600;">quartIn</span><span style="color: #66cc66;">&#41;</span><br /> &nbsp; &nbsp;.<span style="color: #006600;">wait</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">200</span><span style="color: #66cc66;">&#41;</span><br /> &nbsp; &nbsp;.<span style="color: #0066CC;">play</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">2</span>, onChainComplete<span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// play 2 times</span></p><p><span style="color: #000000; ">function</span> onChainComplete<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span> <br /> &nbsp; &nbsp;<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;done.&quot;</span><span style="color: #66cc66;">&#41;</span>;<br /> &nbsp; &nbsp; myChain.<span style="color: #006600;">destroy</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br /> <span style="color: #66cc66;">&#125;</span><br /> &nbsp;</div><p>To clear all references, you should call the myChain.destroy(); function. This clears the references to all added functions, and clears the internal tween list.</p><p>update 02/11/2010:<br /> - added show/hide/rotate function<br /> - added shortRotation option<br /> - added scale (no need to define scaleX and scaleY separately)<br /> - optimized chain; chain items are stored by duration inside dictionary to save lots instances (where duration is the same)<br /> - added Chain.create for fast creation of an instance<br /> - updated apply function (extra param; apply on start)<br /> - several coding style fixes<br /> - added destroyOnComplete to animate() function</p><p>So if you are interested in the code, you can find it inside my googlecode:<br /> <strong>&raquo; <a href="http://code.google.com/p/stroep/source/browse#svn%2Ftrunk%2Fflashsources%2Fchain%2Fnl%2Fstroep%2Fchain">Chain Tween sources</a></strong></p><p><a href="http://blog.stroep.nl/?flattrss_redirect&amp;id=1043&amp;md5=f663ad2802c382bb217cb5b6a30e08b3" 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/2010/10/chain-tween/feed/</wfw:commentRss> <slash:comments>1</slash:comments> </item> <item><title>Remove eventListeners automatically  in AS3</title><link>http://blog.stroep.nl/2010/09/remove-eventlisteners-in-as3/</link> <comments>http://blog.stroep.nl/2010/09/remove-eventlisteners-in-as3/#comments</comments> <pubDate>Tue, 28 Sep 2010 18:05:51 +0000</pubDate> <dc:creator>Mark Knol</dc:creator> <category><![CDATA[actionscript]]></category> <category><![CDATA[Code snippets]]></category> <category><![CDATA[as3]]></category> <category><![CDATA[events]]></category> <category><![CDATA[flash]]></category><guid isPermaLink="false">http://blog.stroep.nl/?p=991</guid> <description><![CDATA[<img src="http://blog.stroep.nl/wp-content/snippet.jpg" alt="" title="snippet" width="100" height="100" class="alignleft size-full wp-image-588">I hate removing listeners in AS3. So I created a work-around to autoremove them :) I know there are already some solutions for this, but some of them requires a complete new event system and it is just fun to create your own sources. This class overrides the default behavior of the <em>addEventListener</em> function. It stores references of the listeners inside a Dictionary. The class it selfs listens to the REMOVED_FROM_STAGE event, and when it is removed, then it removes all stored events. ]]></description> <content:encoded><![CDATA[<p><img src="http://blog.stroep.nl/wp-content/snippet.jpg" alt="" title="snippet" width="100" height="100" class="alignleft size-full wp-image-588">I hate removing listeners in AS3. So I created a work-around to autoremove them <img src='http://blog.stroep.nl/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> I know there are already some solutions for this, but some of them requires a complete new event system and it is just fun to create your own sources. This class overrides the default behavior of the <em>addEventListener</em> function. It stores references of the listeners inside a Dictionary. The class it selfs listens to the REMOVED_FROM_STAGE event, and when it is removed, then it removes all stored events.</p><blockquote><p> UPDATE 11-10-2010: This code below is a bit outdated. <br/>You&#8217;d better use <a href="http://code.google.com/p/stroep/source/browse/#svn%2Ftrunk%2Fflashsources%2Fflashflowfactory%20framework%2Fnl%2Fstroep%2Fflashflowfactory%2Fdisplay">this EventManagedSprite</a> and <a href="http://code.google.com/p/stroep/source/browse/#svn%2Ftrunk%2Fflashsources%2Fflashflowfactory%20framework%2Fnl%2Fstroep%2Fflashflowfactory%2Futils%253Fstate%253Dclosed">this EventRemover</a>. BTW: This is all included in <a href="http://code.google.com/p/stroep/wiki/FlashFlowFactory">FlashFlowFactory</a>. This lightweight framework helps you to easily setup a flash website.</p></blockquote><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; <span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">utils</span>.<span style="color: #006600;">Dictionary</span>;<br /> &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">/**<br /> &nbsp; &nbsp; &nbsp;* Simple event managing sprite which automatically removes eventlisteners when the sprite is removed from stage. This class should be extended.<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> EventManagedSprite <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> eventsList:<span style="color: #808080; font-style: italic;">/*Array*/</span>Dictionary = <span style="color: #000000; ">new</span> Dictionary<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br /> &nbsp; &nbsp; &nbsp; &nbsp; <br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">public</span> <span style="color: #000000; ">function</span> EventManagedSprite<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; <span style="color: #0066CC;">super</span>.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">REMOVED_FROM_STAGE</span>, onRemoveEventManagedSprite<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> onRemoveEventManagedSprite<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; <span style="color: #0066CC;">super</span>.<span style="color: #006600;">removeEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">REMOVED_FROM_STAGE</span>, onRemoveEventManagedSprite<span style="color: #66cc66;">&#41;</span>;<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span><span style="color: #000000; ">var</span> <span style="color: #0066CC;">type</span>:<span style="color: #0066CC;">String</span> <span style="color: #b1b100;">in</span> eventsList<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: #000000; ">var</span> events:<span style="color: #0066CC;">Array</span> = eventsList<span style="color: #66cc66;">&#91;</span><span style="color: #0066CC;">type</span><span style="color: #66cc66;">&#93;</span> as <span style="color: #0066CC;">Array</span>;<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &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> = <span style="color: #cc66cc;">0</span>; i &lt; events.<span style="color: #0066CC;">length</span>; i++<span style="color: #66cc66;">&#41;</span> <br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; ">var</span> eventObject:EventObject = events<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>;<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">super</span>.<span style="color: #006600;">removeEventListener</span><span style="color: #66cc66;">&#40;</span> <span style="color: #0066CC;">type</span>, eventObject.<span style="color: #006600;">listener</span>, eventObject.<span style="color: #006600;">useCapture</span> <span style="color: #66cc66;">&#41;</span>;<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">//trace(&quot;Auto removed listener &quot;, type, eventObject.listener, &quot;from&quot;, this);</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; eventObject.<span style="color: #006600;">listener</span> = <span style="color: #000000; ">null</span>;<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; eventObject = <span style="color: #000000; ">null</span>;<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>events.<span style="color: #0066CC;">length</span> == <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> <br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">delete</span> eventsList<span style="color: #66cc66;">&#91;</span><span style="color: #0066CC;">type</span><span style="color: #66cc66;">&#93;</span>;<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</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; override <span style="color: #b1b100;">public</span> <span style="color: #000000; ">function</span> addEventListener<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">type</span>:<span style="color: #0066CC;">String</span>, listener:<span style="color: #000000; ">Function</span>, useCapture:<span style="color: #0066CC;">Boolean</span> = <span style="color: #000000; ">false</span>, priority:<span style="color: #0066CC;">int</span> = <span style="color: #cc66cc;">0</span>, useWeakReference:<span style="color: #0066CC;">Boolean</span> = <span style="color: #000000; ">false</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;">super</span>.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">type</span>, listener, useCapture, priority, useWeakReference<span style="color: #66cc66;">&#41;</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>!eventsList<span style="color: #66cc66;">&#91;</span> <span style="color: #0066CC;">type</span> <span style="color: #66cc66;">&#93;</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; eventsList<span style="color: #66cc66;">&#91;</span> <span style="color: #0066CC;">type</span> <span style="color: #66cc66;">&#93;</span> = <span style="color: #66cc66;">&#91;</span> <span style="color: #000000; ">new</span> EventObject<span style="color: #66cc66;">&#40;</span> listener, useCapture <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#93;</span>;<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">else</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; eventsList<span style="color: #66cc66;">&#91;</span> <span style="color: #0066CC;">type</span> <span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">push</span><span style="color: #66cc66;">&#40;</span> <span style="color: #000000; ">new</span> EventObject<span style="color: #66cc66;">&#40;</span> listener, useCapture <span style="color: #66cc66;">&#41;</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></p><p>final internal <span style="color: #000000; ">class</span> EventObject<br /> <span style="color: #66cc66;">&#123;</span><br /> &nbsp; &nbsp; <span style="color: #b1b100;">public</span> <span style="color: #000000; ">var</span> listener:<span style="color: #000000; ">Function</span><br /> &nbsp; &nbsp; <span style="color: #b1b100;">public</span> <span style="color: #000000; ">var</span> useCapture:<span style="color: #0066CC;">Boolean</span><br /> &nbsp; &nbsp; <br /> &nbsp; &nbsp; <span style="color: #b1b100;">public</span> final <span style="color: #000000; ">function</span> EventObject <span style="color: #66cc66;">&#40;</span> listener:<span style="color: #000000; ">Function</span>, useCapture:<span style="color: #0066CC;">Boolean</span> <span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span><br /> &nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">this</span>.<span style="color: #006600;">listener</span> = listener;<br /> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">this</span>.<span style="color: #006600;">useCapture</span> = useCapture;<br /> &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br /> <span style="color: #66cc66;">&#125;</span></div><p>Make sure every class you add listeners extends this class to make the most out of it. Otherwise you just need to remove the listeners yourself. Let me know what you think about it.</p><p>Disclaimer: This class does not detect the eventlisteners on the children of the class. These children should extend the EventManagedSprite class too, otherwise you should remove it manually.</p><p><a href="http://blog.stroep.nl/?flattrss_redirect&amp;id=991&amp;md5=b7031677e27933368da6d52257caeb76" 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/2010/09/remove-eventlisteners-in-as3/feed/</wfw:commentRss> <slash:comments>11</slash:comments> </item> <item><title>Quick way to create an auto-increment index AS3</title><link>http://blog.stroep.nl/2010/08/auto-increment-as3-class/</link> <comments>http://blog.stroep.nl/2010/08/auto-increment-as3-class/#comments</comments> <pubDate>Sun, 22 Aug 2010 13:39:16 +0000</pubDate> <dc:creator>Mark Knol</dc:creator> <category><![CDATA[actionscript]]></category> <category><![CDATA[Code snippets]]></category> <category><![CDATA[as3]]></category> <category><![CDATA[class]]></category> <category><![CDATA[snippet]]></category><guid isPermaLink="false">http://blog.stroep.nl/?p=977</guid> <description><![CDATA[<img src="http://blog.stroep.nl/wp-content/snippet.jpg" alt="" title="snippet" width="100" height="100" class="alignleft size-full wp-image-588">Quick post here. I'd like to share this very quick way to create an automatic ID or index to your class instances. Mostly I pass an index as parameter to the class instances, or I use a public var to set the index. Using this way it is very easy to create an automatically filled index, since you have to set this up once and never worry again :)]]></description> <content:encoded><![CDATA[<p><img src="http://blog.stroep.nl/wp-content/snippet.jpg" alt="" title="snippet" width="100" height="100" class="alignleft size-full wp-image-588">Quick post here. I&#8217;d like to share this very quick way to create an automatic ID or index to your class instances. Mostly I pass an index as parameter to the class instance, or I use a public var to set the index. Using this way it is very easy to create an automatically filled index, since you have to set this up once and never worry again <img src='http://blog.stroep.nl/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /></p><p>Take a look at this code:</p><div class="bbCSH" style="font-family: monospace;"> <span style="color: #0066CC;">package</span><br /> <span style="color: #66cc66;">&#123;</span><br /> &nbsp; <span style="color: #b1b100;">public</span> <span style="color: #000000; ">class</span> MyObject<br /> &nbsp; <span style="color: #66cc66;">&#123;</span><br /> &nbsp; &nbsp; <span style="color: #b1b100;">private</span> <span style="color: #0066CC;">static</span> <span style="color: #000000; ">var</span> global_index:<span style="color: #0066CC;">int</span> = <span style="color: #cc66cc;">0</span>;<br /> &nbsp; &nbsp; <span style="color: #b1b100;">public</span> const <span style="color: #0066CC;">INDEX</span>:<span style="color: #0066CC;">int</span> = global_index ++;<br /> &nbsp; <span style="color: #66cc66;">&#125;</span><br /> <span style="color: #66cc66;">&#125;</span><br /> &nbsp;</div><p>As you see, I have created a static variable global_index, which is always the same to all MyObject classes. I also created a public constant &#8216;INDEX&#8217;, which would be unique in every instance. When the MyObject instance is created the index will be set to the global_index, and the global_index will increase by one. So that&#8217;s basically the trick to create the auto-increment index for your AS3 class.</p><div class="bbCSH" style="font-family: monospace;">update: Changed global_index to a private static + removed constructor</div><p><a href="http://blog.stroep.nl/?flattrss_redirect&amp;id=977&amp;md5=84ec0bda1ad30fd8af7b85c6c931d79c" 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/2010/08/auto-increment-as3-class/feed/</wfw:commentRss> <slash:comments>2</slash:comments> </item> </channel> </rss>
