<?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"
	>

<channel>
	<title>webroo.org &#187; Actionscript</title>
	<atom:link href="http://webroo.org/category/actionscript/feed/" rel="self" type="application/rss+xml" />
	<link>http://webroo.org</link>
	<description>Matt Sweetman :: Flex &#38; Actionscript Development</description>
	<pubDate>Sun, 17 Jan 2010 18:24:02 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6.3</generator>
	<language>en</language>
			<item>
		<title>HTML 5 Canvas vs Flash</title>
		<link>http://webroo.org/2010/01/17/html-5-canvas-vs-flash/</link>
		<comments>http://webroo.org/2010/01/17/html-5-canvas-vs-flash/#comments</comments>
		<pubDate>Sun, 17 Jan 2010 16:11:26 +0000</pubDate>
		<dc:creator>Matt Sweetman</dc:creator>
		
		<category><![CDATA[Actionscript]]></category>

		<category><![CDATA[Flash]]></category>

		<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://webroo.org/?p=136</guid>
		<description><![CDATA[Over the holidays I gave myself a break from Actionscript and took a look at the HTML 5 Canvas object. It&#8217;s been a while since I programmed any javascript, and AJAX technologies seem to be the popular choice for web apps these days (goodbye Flex!). I decided to port over an old Actionscript 1 experiment [...]]]></description>
			<content:encoded><![CDATA[<p>Over the holidays I gave myself a break from Actionscript and took a look at the HTML 5 Canvas object. It&#8217;s been a while since I programmed any javascript, and AJAX technologies seem to be the popular choice for web apps these days (goodbye Flex!). I decided to port over an old Actionscript 1 experiment to see how the two technologies performed alongside each other, the results can be seen below.</p>
<p><img src="http://webroo.org/images/snow1.jpg" width="130" height="130" alt="Snowfall image" /><a href="http://webroo.org/media/snowfall/canvassnowfall.html">Canvas Snowfall</a></p>
<p><a href="http://webroo.org/media/snowfall/flashsnowfall.html">Flash Snowfall</a></p>
<p>You&#8217;ll need a modern browser to view the canvas version (eg: Firefox/Chrome/Opera/etc). View the source of the Canvas version for the javascript.</p>
<p>As you can see they look pretty much identical, the differences primarily lie in performance. The Flash version can handle more than twice the number of snowflakes before the frame rate drops. This may be down to the a fundamental difference between Canvas and Flash: Canvas is a rasterised bitmap object, whereas Flash is a vector animation tool. When you draw shapes in Canvas you&#8217;re drawing directly onto a bitmap, the shape can no longer be moved or scaled and is &#8216;baked&#8217; into the image. In some ways it&#8217;s similar to the BitmapData object in Flash. Because of this animation is far easier to accomplish in Flash and I had to adapt parts of the original code to work with the Canvas concept.</p>
<p><span id="more-136"></span>Javascript and AS1 share a lot in similarity as programming languages so porting the code was relatively easy. The Canvas object also has a similar API to the Graphics object in Flash. I&#8217;ll admit Javascript is not my favourite language, by a long shot, the lack of strict typing is difficult to return to after years of AS3, and in more complex scripts nested closures can get messy. It is however fast to program, which can come in very handy when prototyping and experimenting with ideas.</p>
<p>It may be worth investigating <a href="http://processingjs.org/">Processing</a>, a framework for the Canvas object written by John Resig, the guy behind JQuery.</p>
]]></content:encoded>
			<wfw:commentRss>http://webroo.org/2010/01/17/html-5-canvas-vs-flash/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Overlapping hit areas for sibling sprites</title>
		<link>http://webroo.org/2009/12/01/overlapping-hit-areas-for-sibling-sprites/</link>
		<comments>http://webroo.org/2009/12/01/overlapping-hit-areas-for-sibling-sprites/#comments</comments>
		<pubDate>Tue, 01 Dec 2009 14:50:27 +0000</pubDate>
		<dc:creator>Matt Sweetman</dc:creator>
		
		<category><![CDATA[Actionscript]]></category>

		<category><![CDATA[Flash]]></category>

		<guid isPermaLink="false">http://webroo.org/?p=124</guid>
		<description><![CDATA[I have a recurring problem with buttons. And when I mean buttons I mean anything that has a mouse handler and inherits from InteractiveObject, which includes Sprite and SimpleButton. The problem is when two or more of these objects are siblings (i.e. in the same container, not nested) and they overlap, then they block each [...]]]></description>
			<content:encoded><![CDATA[<p>I have a recurring problem with buttons. And when I mean buttons I mean anything that has a mouse handler and inherits from <span style="font-family: courier new">InteractiveObject</span>, which includes <span style="font-family: courier new">Sprite</span> and <span style="font-family: courier new">SimpleButton</span>. The problem is when two or more of these objects are siblings (i.e. in the same container, not nested) and they overlap, then they block each other. The top-most one will always receive mouse interaction and anything beneath is blocked.</p>
<p><img src="http://webroo.org/images/layers.jpg" alt="Blocked hit area diagram" /></p>
<p>What I would like is both Sprites to dispatch mouse events when the user interacts. Sometimes you can get round it by nesting buttons and using ROLL_OVER and MOUSE_OVER events, but this isn&#8217;t always practical, especially if there are a lot of overlapping buttons. So I&#8217;ve come up with another solution.</p>
<p>The idea is to create a &#8216;fake&#8217; hit area using the Rectangle object. Rectangle has a method called <span style="font-family: courier new">containsPoint</span>, which you pass a <span style="font-family: courier new">Point</span> object and it returns true or false depending on whether the coordinates fall inside the specified dimensions. So if you pass the mouse coordinates you can determine whether the mouse is inside the &#8216;fake&#8217; hit area. Also, because Rectangle isn&#8217;t a display object it can&#8217;t block mouse interaction, and so you can have multiple overlapping hit areas that all trigger when clicked.</p>
<p><span id="more-124"></span>The <span style="font-family: courier new">containsPoint</span> method has to be continually polled, so it needs to be called everytime the mouse moves. I&#8217;ve written some code that wraps this functionality up for ease of use. Take a look at the class below to see how the insides work.</p>
<p>Download: <a href="http://webroo.org/media/source/HitArea.as">HitArea.as</a></p>
<p>In the example below I&#8217;ve set up two hit areas and overlap them by 100px. The hit areas are invisible, so they won&#8217;t show up on the stage, but when you click on the overlapping section the CLICK event will fire twice, once for each hit area. There are also <span style="font-family: courier new">HitArea.ROLL_OVER</span> and <span style="font-family: courier new">HitArea.ROLL_OUT</span> events, which can be useful.</p>
<div class="codesnip-container" >
<div class="codesnip">hitArea1 = <span class="kw3">new</span> <span class="kw3">HitArea</span><span class="br0">&#40;</span><span class="nu0">200</span>, <span class="nu0">200</span><span class="br0">&#41;</span>; <span class="co1">// Dimensions are passed in the constructor</span><br />
addChild<span class="br0">&#40;</span>hitArea1<span class="br0">&#41;</span>;<br />
hitArea1.<span class="me1">addEventListener</span><span class="br0">&#40;</span><span class="kw3">HitArea</span>.<span class="me1">CLICK</span>, hitAreaClick<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
hitArea2 = <span class="kw3">new</span> <span class="kw3">HitArea</span><span class="br0">&#40;</span><span class="nu0">200</span>, <span class="nu0">200</span><span class="br0">&#41;</span>;<br />
addChild<span class="br0">&#40;</span>hitArea2<span class="br0">&#41;</span>;<br />
hitArea2.<span class="me1">x</span> = <span class="nu0">100</span>; <span class="co1">// overlap the first hit area by 100 px</span><br />
hitArea2.<span class="me1">addEventListener</span><span class="br0">&#40;</span><span class="kw3">HitArea</span>.<span class="me1">CLICK</span>, hitAreaClick<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
<span class="kw3">private</span> <span class="kw1">function</span> hitAreaClick<span class="br0">&#40;</span>event:Event<span class="br0">&#41;</span>:<span class="kw3">void</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// will fire twice, even on overlapping area</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">trace</span><span class="br0">&#40;</span><span class="st0">&#8220;click: &#8220;</span> + event.<span class="me1">target</span>.<span class="kw3">name</span><span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span></div>
</div>
<p>Unfortunately there are two downsides to this method: firstly it&#8217;s slightly more CPU intensive (it needs to constantly run code whenever the mouse moves). Secondly you can&#8217;t use the hand cursor with the hit area. This is the most problematic from a user-experience point of view. Unfortunately to get a hand cursor you need to set <span style="font-family: courier new">buttonMode=true</span> on the Sprite, and the moment you do that it turns it into a real button and we come back to the original problem.</p>
]]></content:encoded>
			<wfw:commentRss>http://webroo.org/2009/12/01/overlapping-hit-areas-for-sibling-sprites/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Modulo and the negative divisor</title>
		<link>http://webroo.org/2009/10/24/modulo-and-the-negative-divisor/</link>
		<comments>http://webroo.org/2009/10/24/modulo-and-the-negative-divisor/#comments</comments>
		<pubDate>Sat, 24 Oct 2009 12:41:03 +0000</pubDate>
		<dc:creator>Matt Sweetman</dc:creator>
		
		<category><![CDATA[Actionscript]]></category>

		<guid isPermaLink="false">http://webroo.org/?p=111</guid>
		<description><![CDATA[I never had reason to doubt the modulo operator, but it turns out it&#8217;s been hiding a little secret from me all these years: it works differently in other programming langauges. I discovered this while converting a Python program to Flash. In Python the result of the modulo operation has the same sign as the [...]]]></description>
			<content:encoded><![CDATA[<p>I never had reason to doubt the modulo operator, but it turns out it&#8217;s been hiding a little secret from me all these years: it works differently in other programming langauges. I discovered this while converting a Python program to Flash. In Python the result of the modulo operation has the same sign as the right-hand number (divisor), but in Actionscript it has the same sign as the left-hand number (dividend). There&#8217;s a wikipedia article on the <a href="http://en.wikipedia.org/wiki/Modulo_operation">modulo operation</a> that explains it in more detail, and has a list of languages and their relevant implementation.</p>
<p>If you need to retain the sign of the divisor in your result (i.e. python-style) here&#8217;s a quick Actionscript method to achieve it:</p>
<div class="codesnip-container" >
<div class="codesnip"><span class="kw3">public</span> <span class="kw1">function</span> mod<span class="br0">&#40;</span>a:Number, b:Number<span class="br0">&#41;</span>:Number<br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">return</span> a - <span class="br0">&#40;</span>b * <span class="kw3">Math</span>.<span class="kw3">floor</span><span class="br0">&#40;</span>a/b<span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
<span class="kw3">trace</span><span class="br0">&#40;</span><span class="nu0">100</span> % -<span class="nu0">30</span><span class="br0">&#41;</span>; <span class="co1">// output: 10</span><br />
<span class="kw3">trace</span><span class="br0">&#40;</span>mod<span class="br0">&#40;</span><span class="nu0">100</span>, -<span class="nu0">30</span><span class="br0">&#41;</span><span class="br0">&#41;</span>; <span class="co1">// output: -20 </span><br />
&nbsp;</div>
</div>
<p>Admittedly this is the first time in 10 years of actionscript programming that it&#8217;s caused me a problem, so take from that what you will.</p>
]]></content:encoded>
			<wfw:commentRss>http://webroo.org/2009/10/24/modulo-and-the-negative-divisor/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Time sync for remote server</title>
		<link>http://webroo.org/2009/08/11/time-sync-for-remote-server/</link>
		<comments>http://webroo.org/2009/08/11/time-sync-for-remote-server/#comments</comments>
		<pubDate>Tue, 11 Aug 2009 13:09:42 +0000</pubDate>
		<dc:creator>Matt Sweetman</dc:creator>
		
		<category><![CDATA[Actionscript]]></category>

		<guid isPermaLink="false">http://webroo.org/?p=74</guid>
		<description><![CDATA[Here&#8217;s a quick little utility I used in my last project: a simple class that syncs with a time source. It can be used to ensure your application runs on server time rather than the user&#8217;s system clock. You sync once with the remote server at the start, and then Flash runs an internal timer [...]]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s a quick little utility I used in my last project: a simple class that syncs with a time source. It can be used to ensure your application runs on server time rather than the user&#8217;s system clock. You sync once with the remote server at the start, and then Flash runs an internal timer to calculate the time elapsed since you last synced. This can prevent multiple server calls everytime you need a time value.</p>

<object	type="application/x-shockwave-flash"
			data="/media/swf/timesyncexample.swf"
			width="300"
			height="25">
	<param name="movie" value="/media/swf/timesyncexample.swf" />
</object>
<p>Inside it uses the getTimer() function, which is good at keeping sync and is unaffected by system clock changes, even mid-application. It&#8217;s not a particularly difficult piece of code. I made the class a singleton, but you could use it differently if you wanted.</p>
<p><span id="more-74"></span></p>
<div class="codesnip-container" >
<div class="codesnip"><span class="kw2">package</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">import</span> flash.<span class="me1">utils</span>.<span class="kw3">getTimer</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="coMULTI">/**<br />
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; * TimeSync maintains a seperate time value from the system clock.<br />
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; */</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">public</span> <span class="kw2">class</span> TimeSync<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">private</span> <span class="kw3">static</span> <span class="kw1">var</span> _instance:TimeSync;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">public</span> <span class="kw3">static</span> <span class="kw1">function</span> getInstance<span class="br0">&#40;</span><span class="br0">&#41;</span>:TimeSync<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">if</span> <span class="br0">&#40;</span>_instance == <span class="kw3">null</span><span class="br0">&#41;</span> _instance = <span class="kw3">new</span> TimeSync<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">return</span> _instance; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="coMULTI">/**<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; * Internal time-stamp value to sync to.<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; * Must be set using sync().<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; */</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">protected</span> <span class="kw1">var</span> _syncStamp:Number;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="coMULTI">/**<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; * Time-stamp for the internal flash getTimer().<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; */</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">protected</span> <span class="kw1">var</span> _timerStamp:Number;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">public</span> <span class="kw1">function</span> TimeSync<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="coMULTI">/**<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; * The time elapsed since the last sync, in millseconds.<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; */</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">public</span> <span class="kw1">function</span> <span class="kw3">get</span> delta<span class="br0">&#40;</span><span class="br0">&#41;</span>:Number<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">return</span> <span class="br0">&#40;</span><span class="kw3">getTimer</span><span class="br0">&#40;</span><span class="br0">&#41;</span> - _timerStamp<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="coMULTI">/**<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; * The synchronized time value, in milliseconds. Pass this into<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; * a new Date object to get formatted time: new Date(time);<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; */</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">public</span> <span class="kw1">function</span> <span class="kw3">get</span> <span class="kw3">time</span><span class="br0">&#40;</span><span class="br0">&#41;</span>:Number<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">return</span> <span class="br0">&#40;</span>_syncStamp + delta<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="coMULTI">/**<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; * Sync the timer to a new time value.<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; * @param time Unix time value in milliseconds<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; */</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">public</span> <span class="kw1">function</span> sync<span class="br0">&#40;</span><span class="kw3">time</span>:Number<span class="br0">&#41;</span>:<span class="kw3">void</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _syncStamp = <span class="kw3">time</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _timerStamp = <span class="kw3">getTimer</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></div>
</div>
<p>Download: <a href="http://webroo.org/media/source/TimeSync.as">TimeSync.as</a></p>
<p>To use it just call the sync() method and set the time value you want to sync to. Then everytime you need the synced time value use the .time property to retrieve it. You can also pass the time value into a new Date object to format it.</p>
<div class="codesnip-container" >
<div class="codesnip"><span class="co1">// Example</span><br />
<span class="kw1">var</span> timeSync:TimeSync = TimeSync.<span class="me1">getInstance</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
timeSync.<span class="me1">sync</span><span class="br0">&#40;</span><span class="nu0">987654321098</span><span class="br0">&#41;</span>; <span class="co1">// set unix time value, in milliseconds</span><br />
<span class="kw3">trace</span><span class="br0">&#40;</span>timeSync.<span class="kw3">time</span><span class="br0">&#41;</span>; <span class="co1">// 987654321098</span><br />
<span class="kw3">trace</span><span class="br0">&#40;</span><span class="kw3">new</span> <span class="kw3">Date</span><span class="br0">&#40;</span>timeSync.<span class="kw3">time</span><span class="br0">&#41;</span><span class="br0">&#41;</span>; <span class="co1">// Thu Apr 19 05:25:21 GMT+0100 2001 </span><br />
&nbsp;</div>
</div>
<p><strong>Remote sync example</strong></p>
<p>To get it syncing remotely you just need to retrieve a unix time-stamp from a server. A simple bit of php code can do this:</p>
<div class="codesnip-container" >
<div class="codesnip"><span class="kw2">&lt;?php</span> <a href="http://www.php.net/echo"><span class="kw3">echo</span></a> <span class="st0">&#8220;time=&#8221;</span>.<a href="http://www.php.net/time"><span class="kw3">time</span></a><span class="br0">&#40;</span><span class="br0">&#41;</span>; <span class="kw2">?&gt;</span></div>
</div>
<p>Then use URLLoader in Flash to retrieve the value. Remember to multiply the value to milliseconds! Unix time is usually returned in seconds but Flash works in milliseconds.</p>
<div class="codesnip-container" >
<div class="codesnip"><span class="kw3">public</span> <span class="kw1">function</span> syncServerTime<span class="br0">&#40;</span><span class="br0">&#41;</span>:<span class="kw3">void</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">var</span> request:URLRequest = <span class="kw3">new</span> URLRequest<span class="br0">&#40;</span><span class="st0">&#8220;http://webroo.org/time.php&#8221;</span><span class="br0">&#41;</span>; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">var</span> loader:URLLoader = <span class="kw3">new</span> URLLoader<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; loader.<span class="me1">dataFormat</span> = URLLoaderDataFormat.<span class="me1">VARIABLES</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; loader.<span class="me1">addEventListener</span><span class="br0">&#40;</span>Event.<span class="me1">COMPLETE</span>, loaderCompleteHandler<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; loader.<span class="me1">load</span><span class="br0">&#40;</span>request<span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span></p>
<p><span class="kw3">public</span> <span class="kw1">function</span> loaderCompleteHandler<span class="br0">&#40;</span>event:Event<span class="br0">&#41;</span>:<span class="kw3">void</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">var</span> <span class="kw3">time</span>:Number = <span class="br0">&#40;</span>event.<span class="me1">target</span> as URLLoader<span class="br0">&#41;</span>.<span class="me1">data</span>.<span class="kw3">time</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; TimeSync.<span class="me1">getInstance</span><span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="me1">sync</span><span class="br0">&#40;</span><span class="kw3">time</span> * <span class="nu0">1000</span><span class="br0">&#41;</span>; <span class="co1">// Multiply unix time to ms</span><br />
<span class="br0">&#125;</span></div>
</div>
<p>Download: <a href="http://webroo.org/media/source/TimeSyncExample.as">TimeSyncExample.as</a></p>
]]></content:encoded>
			<wfw:commentRss>http://webroo.org/2009/08/11/time-sync-for-remote-server/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
