<?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; chain</title> <atom:link href="http://blog.stroep.nl/tag/chain/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>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>Chain: Delayed function calling [updated]</title><link>http://blog.stroep.nl/2009/11/delayed-function-calling-chain/</link> <comments>http://blog.stroep.nl/2009/11/delayed-function-calling-chain/#comments</comments> <pubDate>Sat, 14 Nov 2009 12:00:36 +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[class]]></category> <category><![CDATA[googlecode]]></category><guid isPermaLink="false">http://blog.stroep.nl/?p=824</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" />I created a useful util-class to make delayed function calling easy. You can make a chain of functions, by adding them with a delay in milliseconds. This chain can be executed multiple times, even in reversed order. ]]></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" />I created a useful util-class to make delayed function calling easy. You can make a chain of functions, by adding them with a delay in milliseconds. This chain can be executed multiple times, even in reversed order.</p><p>Let&#8217;s take a look at a simple example of Chain.<br clear="both"/><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: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span> Event.<span style="color: #006600;">COMPLETE</span>, onComplete<span style="color: #66cc66;">&#41;</span>;<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>.<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>.<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>.<span style="color: #0066CC;">play</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span>;</p><p><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> onComplete<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> <span style="color: #66cc66;">&#123;</span> <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> <span style="color: #66cc66;">&#125;</span></p><p><span style="color: #808080; font-style: italic;">/* trace output:<br /> one two three one two three done.<br /> */</span><br /> &nbsp;</div><p>What is happening here? At the first line we are instantiating the class, and the second line represents what chain does. There is a public function called &#8216;add&#8217;, which is an important part of the class. <em>add(one, 2000)</em> means: execute a function called &#8216;one&#8217; after 2000 milliseconds. <em>add(two,500).add(three,1000)</em> are functions that are called after one is finished, with other delays. You can create your own rhythm/sequence/pattern. At the end we see <em>play(2)</em>, which means: Execute the sequence of functions defined before, and repeat them 2 times. After that, dispatch event COMPLETE.</p><p>So that&#8217;s basically it. A cool part is you can play it reversed, using <em>playReversed()</em>. I am inspired by jQuery (which has nothing to do with this) to enable the ability to stick functions, but thats optional.</p><div class="bbCSH" style="font-family: monospace;"> myChain.<span style="color: #006600;">playReversed</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span>;</p><p><span style="color: #808080; font-style: italic;">/* trace output:<br /> three two one three two one<br /> */</span><br /> &nbsp;</div><p>Of course you can pauze/continue when you are playing, by calling <em>stop() / doContinue()</em>, and you can determine if it is currently playing using the <em>isPlaying</em>-getter.</p><p>These are all the public functions.</p><div class="bbCSH" style="font-family: monospace;"> <span style="color: #808080; font-style: italic;">/// Constructor</span><br /> <span style="color: #b1b100;">public</span> <span style="color: #000000; ">function</span> Chain<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span></p><p><span style="color: #808080; font-style: italic;">/// Adds a function at a specified interval (in milliseconds).</span><br /> <span style="color: #b1b100;">public</span> <span style="color: #000000; ">function</span> <span style="color: #0066CC;">add</span><span style="color: #66cc66;">&#40;</span>func:<span style="color: #000000; ">Function</span>, delay:<span style="color: #0066CC;">Number</span> = <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>:Chain</p><p><span style="color: #808080; font-style: italic;">/// Start playing the sequence and calling functions</span><br /> <span style="color: #b1b100;">public</span> <span style="color: #000000; ">function</span> <span style="color: #0066CC;">play</span><span style="color: #66cc66;">&#40;</span>repeatCount:<span style="color: #0066CC;">int</span> = <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span></p><p><span style="color: #808080; font-style: italic;">/// Start playing the sequence reversed</span><br /> <span style="color: #b1b100;">public</span> <span style="color: #000000; ">function</span> playReversed<span style="color: #66cc66;">&#40;</span>repeatCount:<span style="color: #0066CC;">int</span> = <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span></p><p><span style="color: #808080; font-style: italic;">/// Clears sequence list. Data will be removed.</span><br /> <span style="color: #b1b100;">public</span> <span style="color: #000000; ">function</span> <span style="color: #0066CC;">clear</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:Chain</p><p><span style="color: #808080; font-style: italic;">/// Stop playing, use doContinue to play futher from current point</span><br /> <span style="color: #b1b100;">public</span> <span style="color: #000000; ">function</span> <span style="color: #0066CC;">stop</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span></p><p><span style="color: #808080; font-style: italic;">/// Continue playing after a stop</span><br /> <span style="color: #b1b100;">public</span> <span style="color: #000000; ">function</span> doContinue<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:Chain</p><p><span style="color: #808080; font-style: italic;">/// Reset indexes</span><br /> <span style="color: #b1b100;">public</span> <span style="color: #000000; ">function</span> reset<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span></p><p><span style="color: #808080; font-style: italic;">/// Returns the string representation of the Chain private vars.</span><br /> <span style="color: #b1b100;">public</span> <span style="color: #000000; ">function</span> <span style="color: #0066CC;">toString</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">String</span></p><p><span style="color: #808080; font-style: italic;">/// Return chain is playing, stopped or completed</span><br /> <span style="color: #b1b100;">public</span> <span style="color: #000000; ">function</span> <span style="color: #0066CC;">get</span> isPlaying<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Boolean</span><br /> &nbsp;</div><p>Hope you like it, let me know if you like to see extra/other related features.</p><p><strong>Download: <a href="http://code.google.com/p/stroep/source/browse/#svn%2Ftrunk%2Fflashsources%2Fchain%2Fnl%2Fstroep%2Fchain">Chain</a></strong> (googlecode)</p><p><strong>Updates</strong><br /> - Private functions are protected<br /> - <em>add</em>-functions is now add(function, delay), where function is required.<br /> - Class now extends EventDispatcher and <em>play/playReversed</em> dispatches <em>Event.COMPLETE</em> after playing sequence, instead of calling onComplete function<br /> - Cleaned up code</p><p><a href="http://blog.stroep.nl/?flattrss_redirect&amp;id=824&amp;md5=71d5697f23fd8fd96a893254237ce37e" 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/2009/11/delayed-function-calling-chain/feed/</wfw:commentRss> <slash:comments>6</slash:comments> </item> </channel> </rss>
