<?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, 07 Sep 2010 07:08:59 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.0.1</generator> <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[Code snippets]]></category> <category><![CDATA[actionscript]]></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/trunk/nl/stroep/chain">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> ]]></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>