<?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>JonnyReeves.co.uk</title>
	<atom:link href="http://www.jonnyreeves.co.uk/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.jonnyreeves.co.uk</link>
	<description>Actionscript, Flash, PHP and stuff</description>
	<lastBuildDate>Mon, 22 Feb 2010 20:32:09 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Logging in ActionScript 3</title>
		<link>http://www.jonnyreeves.co.uk/2010/02/logging-in-actionscript-3/</link>
		<comments>http://www.jonnyreeves.co.uk/2010/02/logging-in-actionscript-3/#comments</comments>
		<pubDate>Mon, 22 Feb 2010 20:27:36 +0000</pubDate>
		<dc:creator>Jonny</dc:creator>
				<category><![CDATA[ActionScript 2]]></category>
		<category><![CDATA[ActionScript 3]]></category>
		<category><![CDATA[best practices]]></category>
		<category><![CDATA[logging]]></category>

		<guid isPermaLink="false">http://www.jonnyreeves.co.uk/?p=169</guid>
		<description><![CDATA[Logging is a fundamental part of any mid to large scale application, your application needs to be able to talk to other developers and Q&#038;A engineers to help diagnose problems and gain insight into what&#8217;s going on under the hood.  Jesse Warden recently wrote an excellent article on logging, however, my own implementation of [...]]]></description>
			<content:encoded><![CDATA[<p>Logging is a fundamental part of any mid to large scale application, your application needs to be able to talk to other developers and Q&#038;A engineers to help diagnose problems and gain insight into what&#8217;s going on under the hood.  Jesse Warden recently wrote <a href="http://jessewarden.com/2010/02/consulting-chronicles-3-preventing-fire-drills-crises-by-removing-land-mines-and-using-diagnostic-tools.html">an excellent article on logging</a>, however, my own implementation of logging AS3 apps differs so much that I thought it warranted a blog post.</p>
<h3>The Problems</h3>
<p>One of the most important things in OOP is to ensure that you avoid tight coupling; Jesse&#8217;s approach of using a static logger leaves your code scattered with a dependency on it.  </p>
<p>Another point that Jesse makes is how important it is to include the name of the enclosing class when writing a log message,</p>
<blockquote><p>&#8230; all log messages start with &#8220;ClassName::methodName&#8221; where ClassName is the name of the class you are in, and methodName is the current method the log message is in.  It seems a major pain at first, but I guarantee you when you start removing them 2 months later, you know EXACTLY where to find it vs. that one trace that stays there for weeks because no one found where the bastard is.
</p></blockquote>
<p>I couldn&#8217;t agree more with this, having come from working on a massive application with thousands of log messages, not knowing where a message was originating from would have been a nightmare.  However, Jesse&#8217;s approach is very manual and relies on your entire team being disciplined; not to mention the fact that as soon as you start refactoring that &#8220;ClassName::methodName&#8221; identifier is quickly going to get out of sync.</p>
<h3>A Commons Solution</h3>
<p>I feel that there is a simple solution to both the above problems in the form of the <a href="http://code.google.com/p/as3-commons/">AS3 Commons Logging Framework</a>.  The framework provides an abstraction of logging which means your code is not dependant on any one implementation, plus it has the added benefit of automatically adding the name of the Class when writing a log message; winner!</p>
<p>Usage is pretty straight forward, but requires a little more setup than Jesse&#8217;s static helper approach; first of all your request a static ILogger instance from the LoggerFactory at the top of your class and then you are free to use it throughout the Class to write log messages, for example:</p>
<div class="dean_ch" style="white-space: nowrap; overflow: scroll;">
package uk.<span class="me1">co</span>.<span class="me1">jonnyreeves</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">public</span> <span class="kw2">class</span> MyClass<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// Request an ILogger instance for this Class.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">public</span> <span class="kw3">static</span> const LOGGER : ILogger = LogFactory.<span class="me1">getClassLogger</span><span class="br0">&#40;</span>MyClass<span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">public</span> <span class="kw2">function</span> MyClass<span class="br0">&#40;</span><span class="br0">&#41;</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; <span class="co1">// Write a log message using the Logger instance.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; LOGGER.<span class="me1">info</span><span class="br0">&#40;</span><span class="st0">&quot;Created instance of MyClass&quot;</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><br />
&nbsp;</div>
<p>Using the TraceLogger implementation which the AS3 Commons Logger will default to, you would receive the following in your <a href="http://yourpalmark.com/2005/07/01/trace-from-the-browser-using-standard-trace/">Flash TraceLog</a>:</p>
<pre>
Sun Feb 21 10:33:29 GMT+0000 2010 [INFO]  uk.co.jonnyreeves.MyClass - Created instance of MyClass
</pre>
<p>The beauty of this approach is that the actual implementation of the ILogger interface is left to the LoggerFactory which means it can be switched at runtime.  Another neat benfit is that the name of the Logger is taken from the encolosing Class which created it (via <a href="http://code.google.com/p/as3-commons/source/browse/tags/as3-commons-logging/1.1/src/main/actionscript/org/as3commons/logging/LoggerFactory.as">LoggerFactory.getClassLogger()</a>).  This mean that is you rename the Class as part of a refactoring effort, all the log messages contained in said class will automatically update.</p>
<h3>Getting More Specific</h3>
<p>So I&#8217;ve mentioned that your can decide the ILogger implementation at runtime, let&#8217;s see an example of that in action.  My current Logging implementation of choice is the excellent DeMonsters Monster Debugger which reminds me a lot of the AS2 classic, LumnicBox in that it allows you to fully inspect the Object Graph at run time.</p>
<p>The first thing we need is to create an implementation of LogFactory which will return our ILogger instances upon request:</p>
<div class="dean_ch" style="white-space: nowrap; overflow: scroll;">
package uk.<span class="me1">co</span>.<span class="me1">jonnyreeves</span>.<span class="me1">logging</span>.<span class="me1">util</span>.<span class="me1">monsterdebugger</span> <br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">import</span> nl.<span class="me1">demonsters</span>.<span class="me1">debugger</span>.<span class="me1">MonsterDebugger</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">import</span> org.<span class="me1">as3commons</span>.<span class="me1">logging</span>.<span class="me1">ILogger</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">import</span> org.<span class="me1">as3commons</span>.<span class="me1">logging</span>.<span class="me1">ILoggerFactory</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="coMULTI">/**<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* Provides a bridge between the AS3Commons Logging Framework and MonsterDebugger.<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* @author John Reeves<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">public</span> <span class="kw2">class</span> MonsterDebuggerLoggerFactory <span class="kw3">implements</span> ILoggerFactory <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</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;* Initialises MonsterDebugger<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* @param target&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; The Base Object you wisht to start graphing from (usually you will supply the<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Display Root DisplayObject).<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* &nbsp; &nbsp; &nbsp;&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; &nbsp;* @param clearConsole&nbsp; Clears MonsterDebugger&#8217;s Trace Console when the initial connection is made.<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="kw2">function</span> MonsterDebuggerLoggerFactory<span class="br0">&#40;</span><span class="kw3">target</span> : <span class="kw3">Object</span>, clearConsole : <span class="kw3">Boolean</span> = <span class="kw2">true</span><span class="br0">&#41;</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; <span class="kw2">new</span> MonsterDebugger<span class="br0">&#40;</span><span class="kw3">target</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>clearConsole<span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &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; &nbsp; &nbsp; &nbsp; &nbsp; MonsterDebugger.<span class="me1">clearTraces</span><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="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="coMULTI">/**<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* Factory Method, used to return an instance of ILogger to the AS3Commons Logging Framework.<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="kw2">function</span> getLogger<span class="br0">&#40;</span><span class="kw3">name</span> : <span class="kw3">String</span><span class="br0">&#41;</span> : ILogger<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="kw1">return</span> <span class="kw2">new</span> MonsterDebuggerLogger<span class="br0">&#40;</span><span class="kw3">name</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><br />
&nbsp;</div>
<p>Now all that&#8217;s left to do is to create the MonsterDebuggerLogger implementation of the ILogger interface; this will re-write all the LOGGER.info(), etc calls through to MonsterDebugger.</p>
<div class="dean_ch" style="white-space: nowrap; overflow: scroll;">
package uk.co.jonnyreeves.logging.util.monsterdebugger <br />
{<br />
&nbsp; &nbsp; &nbsp; &nbsp; import org.as3commons.logging.util.MessageUtil;<br />
&nbsp; &nbsp; &nbsp; &nbsp; import nl.demonsters.debugger.MonsterDebugger;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; import org.as3commons.logging.LogLevel;<br />
&nbsp; &nbsp; &nbsp; &nbsp; import org.as3commons.logging.impl.AbstractLogger;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; /**<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* Provides a bridge between the AS3 Commons Logging implementation and Monster Debugger; it&#8217;s not perfect but it <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* works for the most part.<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* The Target of the log message will always be the name of the logger.<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* The Object of the log message will vary depending on the parameters passed. &nbsp;If the message string is empty<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* then the params Array will be logged. &nbsp;If the message string is not empty, the params Array will be used to<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* tokenise the message string.<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* Usage:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; LOGGER.debug(&quot;Hello World&quot;);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Outputs message: &quot;(String) Hello World&quot;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; LOGGER.debug(&quot;Hello {0} {1}, &quot;Jonny&quot;, &quot;Reeves&quot;);&nbsp; &nbsp; &nbsp; &nbsp; // Outputs message: &quot;(String) Hello Jonny Reeves&quot;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; LOGGER.debug(&quot;&quot;, [ 1, 2, 3 ]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Outputs message: &quot;(Array)&#8230;&quot; (which can then be inspected)<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* Incorrect usage:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; LOGGER.debug(&quot;Some Array&quot;, [1, 2, 3]);&nbsp; // Outputs message: &quot;(String) Some Array&quot; (which can&#8217;t be inspected)<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* @author John Reeves<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*/<br />
&nbsp; &nbsp; &nbsp; &nbsp; public class MonsterDebuggerLogger extends AbstractLogger <br />
&nbsp; &nbsp; &nbsp; &nbsp; {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public var level : int = LogLevel.DEBUG;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public function MonsterDebuggerLogger(name : String) <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; super(name);<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; override protected function log(level : uint, message : String, params : Array) : void<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (level &gt;= this.level)<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var target : Object = name;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var object : Object = getLogObject(message, params);<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var colour : uint = getLogMessageColour(level);<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; MonsterDebugger.trace(target, object, colour);<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; private function getLogObject(message : String, params : Array) : Object <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (message == &quot;&quot;)<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // If the user only supplied a single param to log, we remove the params array which is enclosing it.<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return (params.length == 1) ? params.pop() : params;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return MessageUtil.toString(message, params);<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; private function getLogMessageColour(level : int) : uint <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var colour : int;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; switch (level)<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case LogLevel.FATAL:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case LogLevel.ERROR:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; colour = MonsterDebugger.COLOR_ERROR;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &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; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case LogLevel.WARN:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; colour = MonsterDebugger.COLOR_WARNING;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;<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; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; default:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; colour = MonsterDebugger.COLOR_NORMAL;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return colour;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; override public function get debugEnabled() : Boolean<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return LogLevel.DEBUG &gt;= level;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; override public function get errorEnabled() : Boolean<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return LogLevel.ERROR &gt;= level;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; override public function get infoEnabled() : Boolean<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return LogLevel.INFO &gt;= level;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; override public function get warnEnabled() : Boolean<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return LogLevel.WARN &gt;= level;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; override public function get fatalEnabled() : Boolean<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return LogLevel.FATAL &gt;= level;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br />
&nbsp; &nbsp; &nbsp; &nbsp; }<br />
}<br />
&nbsp;</div>
<p>The code here is fairly self documenting with the protected log() method proxying all the logging requests through to MonsterDebugger.  Because the creators of the AS3Commons Logging Project specified the message param to have a datatype of String, we are forced to do a little bit of a workaround when we want to dump Objects out to the MonsterDebugger console.  There is an <a href="http://code.google.com/p/as3-commons/issues/detail?id=3">open ticket</a> on the AS3Commons project page should you wish to request a change.</p>
<p>The last piece of the puzzle is to swap the AS3Commons LoggingFactory implementation, this is very straight forward and requires a single line of code during your application&#8217;s startup routine (preferably before any logging takes place).</p>
<div class="dean_ch" style="white-space: nowrap; overflow: scroll;">
package &nbsp;<br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">import</span> uk.<span class="me1">co</span>.<span class="me1">jonnyreeves</span>.<span class="me1">logging</span>.<span class="me1">util</span>.<span class="me1">MonsterDebuggerLoggerFactory</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">import</span> flash.<span class="me1">display</span>.<span class="me1">Sprite</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="coMULTI">/**<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* Application Entry Point.<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* @author John Reeves<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">public</span> <span class="kw2">class</span> MyApp <span class="kw3">extends</span> Sprite <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">public</span> <span class="kw2">function</span> MyApp<span class="br0">&#40;</span><span class="br0">&#41;</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; LoggerFactory.<span class="me1">loggerFactory</span> = <span class="kw2">new</span> MonsterDebuggerLoggerFactory<span class="br0">&#40;</span><span class="kw3">this</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><br />
&nbsp;</div>
<p>Once this is done you are free to start using the LoggingFactory as per the example further up the page.  Your log messages should now show up in the MonsterDebugger logging console.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jonnyreeves.co.uk/2010/02/logging-in-actionscript-3/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Error 2048 and Flash Policy File Logging</title>
		<link>http://www.jonnyreeves.co.uk/2010/02/error-2048-and-flash-policy-file-logging/</link>
		<comments>http://www.jonnyreeves.co.uk/2010/02/error-2048-and-flash-policy-file-logging/#comments</comments>
		<pubDate>Wed, 17 Feb 2010 14:54:31 +0000</pubDate>
		<dc:creator>Jonny</dc:creator>
				<category><![CDATA[ActionScript 3]]></category>

		<guid isPermaLink="false">http://www.jonnyreeves.co.uk/?p=167</guid>
		<description><![CDATA[Whenever you are working with Flash or Flex applications which make use of 3rd party feeds or data sources (such as XML or JSON), you need to be aware of how the Flash Player deals with Cross Domain security issues.
The easiest way to debug a 2048 Security Error is to enable Policy File logging in [...]]]></description>
			<content:encoded><![CDATA[<p>Whenever you are working with Flash or Flex applications which make use of 3rd party feeds or data sources (such as XML or JSON), you need to be aware of how the Flash Player deals with Cross Domain security issues.</p>
<p>The easiest way to debug a 2048 Security Error is to enable Policy File logging in your mm.cfg; Adobe have <a href="http://www.adobe.com/devnet/flashplayer/articles/fplayer9_security_05.html">a guide posted over on their site</a> which explains how to get setup.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jonnyreeves.co.uk/2010/02/error-2048-and-flash-policy-file-logging/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>FlexPMD and ANT &#8211; RuleSetFactory Error</title>
		<link>http://www.jonnyreeves.co.uk/2010/02/flexpmd-and-ant-rulesetfactory-error/</link>
		<comments>http://www.jonnyreeves.co.uk/2010/02/flexpmd-and-ant-rulesetfactory-error/#comments</comments>
		<pubDate>Tue, 09 Feb 2010 20:58:19 +0000</pubDate>
		<dc:creator>Jonny</dc:creator>
				<category><![CDATA[ActionScript 3]]></category>
		<category><![CDATA[ant]]></category>
		<category><![CDATA[flexpmd]]></category>

		<guid isPermaLink="false">http://www.jonnyreeves.co.uk/?p=163</guid>
		<description><![CDATA[Just a quick post to save someone a few minutes of headscratching!  I&#8217;ve just finished integrating FlexPMD, Adobe&#8217;s latest opensource offering into my ant build scripts following their documentation when I hit this error:

java.lang.NoClassDefFoundError: net/sourceforge/pmd/RuleSetFactory

Looking at my taskdef, everything appeared to be in order, it wasn&#8217;t until I searched the Adobe JIRA and managed [...]]]></description>
			<content:encoded><![CDATA[<p>Just a quick post to save someone a few minutes of headscratching!  I&#8217;ve just finished integrating FlexPMD, Adobe&#8217;s latest opensource offering into my ant build scripts following <a href="http://opensource.adobe.com/wiki/display/flexpmd/How+to+invoke+FlexPMD">their documentation</a> when I hit this error:</p>
<pre>
java.lang.NoClassDefFoundError: net/sourceforge/pmd/RuleSetFactory
</pre>
<p>Looking at my taskdef, everything appeared to be in order, it wasn&#8217;t until I searched the Adobe JIRA and managed to find <a href="https://bugs.adobe.com/jira/browse/FLEXPMD-106">this ticket</a>.  What&#8217;s even more annoying is that the ticket is marked as Closed!</p>
<p>I would update the wiki entry on the adobe site, but when I try and log in the site stacktraces&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jonnyreeves.co.uk/2010/02/flexpmd-and-ant-rulesetfactory-error/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Automatically Generating a Maven POM file with ANT</title>
		<link>http://www.jonnyreeves.co.uk/2010/01/automatically-generating-a-maven-pom-file-with-ant/</link>
		<comments>http://www.jonnyreeves.co.uk/2010/01/automatically-generating-a-maven-pom-file-with-ant/#comments</comments>
		<pubDate>Tue, 26 Jan 2010 18:20:34 +0000</pubDate>
		<dc:creator>Jonny</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.jonnyreeves.co.uk/?p=151</guid>
		<description><![CDATA[This one tripped me up for a while and doesn&#8217;t seem to be mentioned in the Maven ANT Tasks Documentation.  I was working on a build process which makes use of Maven soley for deploying and versioning releases and ANT for the rest of the build.  One thing I wanted to eliminate was [...]]]></description>
			<content:encoded><![CDATA[<p>This one tripped me up for a while and doesn&#8217;t seem to be mentioned in the <a href="http://maven.apache.org/ant-tasks/reference.html">Maven ANT Tasks Documentation</a>.  I was working on a build process which makes use of Maven soley for deploying and versioning releases and ANT for the rest of the build.  One thing I wanted to eliminate was the large amount of POM files which were scattered around the project, all of which were basically the same and could most certainly be auto-generated.</p>
<p>The Maven ANT Tasks <a href="http://maven.apache.org/ant-tasks/examples/write-pom.html">indicate that the &lt;pom /&gt; task can be used to create an &#8220;in-memory&#8221; pom</a> which can then be used in the &lt;deploy /&gt; task.  With that in mind, I wrote the following:</p>
<div class="dean_ch" style="white-space: nowrap; overflow: scroll;">
<span class="sc3"><span class="re1">&lt;artifact:pom</span> <span class="re0">id</span>=<span class="st0">&quot;maven.project&quot;</span> <span class="re0">groupId</span>=<span class="st0">&quot;com.example&quot;</span> <span class="re0">version</span>=<span class="st0">&quot;${build.version}&quot;</span> <span class="re0">artifactId</span>=<span class="st0">&quot;${project.name}&quot;</span> <span class="re0">packaging</span>=<span class="st0">&quot;zip&quot;</span><span class="re2">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;remoteRepository</span> <span class="re0">id</span>=<span class="st0">&quot;releases&quot;</span> <span class="re0">url</span>=<span class="st0">&quot;http://maven:8081/nexus/content/repositories/releases&quot;</span> <span class="re2">/&gt;</span></span><br />
<span class="sc3"><span class="re1">&lt;/artifact:pom<span class="re2">&gt;</span></span></span><br />
<span class="sc3"><span class="re1">&lt;artifact:install-provider</span> <span class="re0">artifactId</span>=<span class="st0">&quot;wagon-http-lightweight&quot;</span> <span class="re0">version</span>=<span class="st0">&quot;1.0-beta-2&quot;</span> <span class="re2">/&gt;</span></span><br />
<span class="sc3"><span class="re1">&lt;artifact:deploy</span> <span class="re0">file</span>=<span class="st0">&quot;deploy/${project.name}.zip&quot;</span><span class="re2">&gt;</span></span><br />
&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;pom</span> <span class="re0">refid</span>=<span class="st0">&quot;maven.project&quot;</span> <span class="re2">/&gt;</span></span> <br />
<span class="sc3"><span class="re1">&lt;/artifact:deploy<span class="re2">&gt;</span></span></span><br />
&nbsp;</div>
<p>However, when this ran, the build failed with the following error: &#8220;<strong>A distributionManagement element or remoteRepository element is required to deploy</strong>&#8220;.  Very confusing, especially as I specified the &lt;remoteRepository /&gt; node as a child of the &lt;pom /&gt;.  I concluded that this was not a problem with the &lt;deploy /&gt; task as when I dump the contents of &#8220;maven.pom&#8221; out to disk via &lt;writepom /&gt; I could see that, sure enough, the &lt;repositories /&gt; node was not being created in the POM file!  This is where I hit a brick wall and I can only presume it&#8217;s a bug with the current Maven ANT Tasks (2.1.0).</p>
<p>With that in mind, I tried modifying the &lt;deploy /&gt; task, adding the &lt;remoteRepository /&gt; node to that instead &#8211; again, no joy, same error.</p>
<p>In the end I got it to work with a little bit of a workaround involving temp files &#8211; this works fine for me but it&#8217;s not ideal, hopefully someone out there has encountered this one before:</p>
<div class="dean_ch" style="white-space: nowrap; overflow: scroll;">
<span class="sc3"><span class="coMULTI">&lt;!&#8211; &nbsp;Generate a Dynamic POM file &#8211;&gt;</span></span><br />
<span class="sc3"><span class="re1">&lt;artifact:pom</span> <span class="re0">id</span>=<span class="st0">&quot;pom.tmp.ref&quot;</span> <span class="re0">groupId</span>=<span class="st0">&quot;com.example&quot;</span> <span class="re0">version</span>=<span class="st0">&quot;${build.version}&quot;</span> <span class="re0">artifactId</span>=<span class="st0">&quot;${project.name}&quot;</span> <span class="re0">packaging</span>=<span class="st0">&quot;zip&quot;</span> <span class="re2">/&gt;</span></span></p>
<p><span class="sc3"><span class="coMULTI">&lt;!&#8211; &nbsp;Write the generated POM file out to a temporary file on disc &#8211;&gt;</span></span><br />
<span class="sc3"><span class="re1">&lt;tempfile</span> <span class="re0">property</span>=<span class="st0">&quot;pom.tmp.file&quot;</span> <span class="re2">/&gt;</span></span><br />
<span class="sc3"><span class="re1">&lt;artifact:writepom</span> <span class="re0">pomRefId</span>=<span class="st0">&quot;pom.tmp.ref&quot;</span> <span class="re0">file</span>=<span class="st0">&quot;${pom.tmp.file}&quot;</span> <span class="re2">/&gt;</span></span></p>
<p><span class="sc3"><span class="coMULTI">&lt;!&#8211; &nbsp;Read the temporary file back in as the POM reference &#8211;&gt;</span></span><br />
<span class="sc3"><span class="re1">&lt;artifact:pom</span> <span class="re0">id</span>=<span class="st0">&quot;maven.project&quot;</span> <span class="re0">file</span>=<span class="st0">&quot;${pom.tmp.file}&quot;</span> <span class="re2">/&gt;</span></span></p>
<p><span class="sc3"><span class="coMULTI">&lt;!&#8211; We can now deploy, but we have to include a reference to the repository &#8211;&gt;</span></span><br />
<span class="sc3"><span class="re1">&lt;artifact:install-provider</span> <span class="re0">artifactId</span>=<span class="st0">&quot;wagon-http-lightweight&quot;</span> <span class="re0">version</span>=<span class="st0">&quot;1.0-beta-2&quot;</span> <span class="re2">/&gt;</span></span><br />
<span class="sc3"><span class="re1">&lt;artifact:deploy</span> <span class="re0">file</span>=<span class="st0">&quot;deploy/${project.name}.zip&quot;</span><span class="re2">&gt;</span></span><br />
&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;pom</span> <span class="re0">refid</span>=<span class="st0">&quot;maven.project&quot;</span> <span class="re2">/&gt;</span></span> <br />
&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;remoteRepository</span> <span class="re0">id</span>=<span class="st0">&quot;releases&quot;</span> <span class="re0">url</span>=<span class="st0">&quot;http://maven:8081/nexus/content/repositories/releases&quot;</span> <span class="re2">/&gt;</span></span><br />
<span class="sc3"><span class="re1">&lt;/artifact:deploy<span class="re2">&gt;</span></span></span><br />
&nbsp;</div>
]]></content:encoded>
			<wfw:commentRss>http://www.jonnyreeves.co.uk/2010/01/automatically-generating-a-maven-pom-file-with-ant/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Sorting Values Stored in a Dictionary</title>
		<link>http://www.jonnyreeves.co.uk/2009/06/sorting-values-stored-in-a-dictionary/</link>
		<comments>http://www.jonnyreeves.co.uk/2009/06/sorting-values-stored-in-a-dictionary/#comments</comments>
		<pubDate>Mon, 15 Jun 2009 06:59:11 +0000</pubDate>
		<dc:creator>Jonny</dc:creator>
				<category><![CDATA[ActionScript 3]]></category>
		<category><![CDATA[dictionary]]></category>
		<category><![CDATA[sort]]></category>

		<guid isPermaLink="false">http://www.jonnyreeves.co.uk/?p=93</guid>
		<description><![CDATA[Looking through my incoming seaches from Google I&#8217;ve noticed that a lot of visitors to my site are trying to figure out how to sort values stored in a Dictionary.  The short and simple answer is that you can&#8217;t as Dictionary&#8217;s in AS3 are meant to represent un-ordered HashMaps, however, there&#8217;s nothing to stop [...]]]></description>
			<content:encoded><![CDATA[<p>Looking through my incoming seaches from Google I&#8217;ve noticed that a lot of visitors to my site are trying to figure out how to sort values stored in a Dictionary.  The short and simple answer is that you can&#8217;t as Dictionary&#8217;s in AS3 are meant to represent un-ordered HashMaps, however, there&#8217;s nothing to stop you moving the un-ordered data out of the Dictionary and into an Array:</p>
<div class="dean_ch" style="white-space: nowrap; overflow: scroll;">
package <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">import</span> flash.<span class="me1">display</span>.<span class="me1">Sprite</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">import</span> flash.<span class="me1">utils</span>.<span class="me1">Dictionary</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="coMULTI">/**<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* @author Jonny Reeves<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">public</span> <span class="kw2">class</span> DictionarySort <span class="kw3">extends</span> Sprite <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">function</span> DictionarySort<span class="br0">&#40;</span><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; <span class="kw2">var</span> myDictionary : Dictionary = <span class="kw2">new</span> Dictionary<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// Provide some values to sort.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; myDictionary<span class="br0">&#91;</span><span class="nu0">500</span><span class="br0">&#93;</span> = <span class="st0">&quot;Five Hundred&quot;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; myDictionary<span class="br0">&#91;</span><span class="nu0">55</span><span class="br0">&#93;</span> = <span class="st0">&quot;Fifty Five&quot;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; myDictionary<span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span> = <span class="st0">&quot;One&quot;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; myDictionary<span class="br0">&#91;</span><span class="nu0">231</span><span class="br0">&#93;</span> = <span class="st0">&quot;Two Hundred and Thirty One&quot;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">var</span> myKeys : <span class="kw3">Array</span> = <span class="kw3">this</span>.<span class="me1">extractKeysFrom</span><span class="br0">&#40;</span>myDictionary<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">trace</span><span class="br0">&#40;</span><span class="st0">&quot;myKeys Array before sort: &quot;</span> + myKeys<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// Apply a Numeric sort to the Array.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; myKeys.<span class="kw3">sort</span><span class="br0">&#40;</span><span class="kw3">Array</span>.<span class="me1">NUMERIC</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">trace</span><span class="br0">&#40;</span><span class="st0">&quot;myKeys Array after sort: &quot;</span> + myKeys<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// You can now use the extracted Keys to read from the Dictionary in</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// the required order.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">for</span> each <span class="br0">&#40;</span><span class="kw2">var</span> thisKey : * <span class="kw1">in</span> myKeys<span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &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; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// Will trace out the number followed by the english words.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">trace</span><span class="br0">&#40;</span>thisKey + <span class="st0">&quot; = &quot;</span> + myDictionary<span class="br0">&#91;</span>thisKey<span class="br0">&#93;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</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;* Helper method to extract the Keys from a Dictionary Object and return<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* them in an Array.<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">private</span> <span class="kw2">function</span> extractKeysFrom<span class="br0">&#40;</span>source : Dictionary<span class="br0">&#41;</span> : <span class="kw3">Array</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; <span class="kw2">var</span> output : <span class="kw3">Array</span> = <span class="br0">&#91;</span><span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// Note that Dictionary&#8217;s Keys are untyped as they can contain</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// any value.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">for</span> <span class="br0">&#40;</span><span class="kw2">var</span> prop : * <span class="kw1">in</span> source<span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &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; &nbsp; &nbsp; &nbsp; &nbsp; output.<span class="kw3">push</span><span class="br0">&#40;</span>prop<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &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; <span class="kw1">return</span> output;<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><br />
&nbsp;</div>
]]></content:encoded>
			<wfw:commentRss>http://www.jonnyreeves.co.uk/2009/06/sorting-values-stored-in-a-dictionary/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Sorting values in ActionScript 3</title>
		<link>http://www.jonnyreeves.co.uk/2009/05/sorting-values-in-actionscript-3/</link>
		<comments>http://www.jonnyreeves.co.uk/2009/05/sorting-values-in-actionscript-3/#comments</comments>
		<pubDate>Sat, 23 May 2009 15:05:51 +0000</pubDate>
		<dc:creator>Jonny</dc:creator>
				<category><![CDATA[ActionScript 3]]></category>

		<guid isPermaLink="false">http://www.jonnyreeves.co.uk/?p=91</guid>
		<description><![CDATA[One of the next big projects at work will involve displaying a list of data to the user, this list will be generated server side and sent down as un-ordered XML, it is up to the client to sort the data and display it to the user.  The system will need to be able [...]]]></description>
			<content:encoded><![CDATA[<p>One of the next big projects at work will involve displaying a list of data to the user, this list will be generated server side and sent down as un-ordered XML, it is up to the client to sort the data and display it to the user.  The system will need to be able to sort a large amount of data (at present, the server could be sending down up to 10,000 records) and we needed to find out if this sorting would be fiesable &#8211; to find out, I set up a quick little test script which provides a couple of interesting findings:</p>
<div class="dean_ch" style="white-space: nowrap; overflow: scroll;">
package <br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">import</span> flash.<span class="me1">display</span>.<span class="me1">Sprite</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">import</span> flash.<span class="me1">utils</span>.<span class="me1">Dictionary</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">import</span> flash.<span class="me1">utils</span>.<span class="kw3">getTimer</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="coMULTI">/**<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* @author Jonny<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">public</span> <span class="kw2">class</span> Sorting <span class="kw3">extends</span> Sprite<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">public</span> <span class="kw2">function</span> Sorting<span class="br0">&#40;</span><span class="br0">&#41;</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; <span class="kw2">var</span> source1 : <span class="kw3">Array</span> = <span class="kw3">this</span>.<span class="me1">generateViaIndexOf</span><span class="br0">&#40;</span><span class="nu0">10000</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">var</span> source2 : <span class="kw3">Array</span> = <span class="kw3">this</span>.<span class="me1">generateViaDictionary</span><span class="br0">&#40;</span><span class="nu0">10000</span>, <span class="kw2">false</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">var</span> source3 : <span class="kw3">Array</span> = <span class="kw3">this</span>.<span class="me1">generateViaDictionary</span><span class="br0">&#40;</span><span class="nu0">10000</span>, <span class="kw2">true</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">this</span>.<span class="kw3">sort</span><span class="br0">&#40;</span>source1, <span class="kw3">Array</span>.<span class="me1">NUMERIC</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">this</span>.<span class="kw3">sort</span><span class="br0">&#40;</span>source2, <span class="kw3">Array</span>.<span class="me1">NUMERIC</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">this</span>.<span class="kw3">sort</span><span class="br0">&#40;</span>source3, <span class="kw3">Array</span>.<span class="me1">NUMERIC</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; debugOutput<span class="br0">&#40;</span><span class="st0">&quot;indexOf&quot;</span>, source1<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; debugOutput<span class="br0">&#40;</span><span class="st0">&quot;nonCast&quot;</span>, source2<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; debugOutput<span class="br0">&#40;</span><span class="st0">&quot;cast&quot;</span>, source3<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="kw3">private</span> <span class="kw2">function</span> debugOutput<span class="br0">&#40;</span><span class="kw3">name</span> : <span class="kw3">String</span>, values : <span class="kw3">Array</span><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; <span class="kw3">trace</span><span class="br0">&#40;</span><span class="kw3">name</span> + <span class="st0">&quot;[0] = &quot;</span> + values<span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span> + <span class="st0">&quot;, &quot;</span> + <span class="kw3">name</span> + <span class="st0">&quot;[1] = &quot;</span> + values<span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">private</span> <span class="kw2">function</span> <span class="kw3">sort</span><span class="br0">&#40;</span>source : <span class="kw3">Array</span>, sortMethod : uint<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; <span class="kw2">var</span> startTime : uint = <span class="kw3">getTimer</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; source.<span class="kw3">sort</span><span class="br0">&#40;</span>sortMethod<span class="br0">&#41;</span>;&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">trace</span><span class="br0">&#40;</span><span class="st0">&quot;Array sorted in &quot;</span> + <span class="br0">&#40;</span><span class="kw3">getTimer</span><span class="br0">&#40;</span><span class="br0">&#41;</span> &#8211; startTime<span class="br0">&#41;</span> + <span class="st0">&quot;ms.&quot;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">private</span> <span class="kw2">function</span> generateViaDictionary<span class="br0">&#40;</span><span class="kw3">length</span> : <span class="kw3">int</span>, castOutput : <span class="kw3">Boolean</span><span class="br0">&#41;</span> : <span class="kw3">Array</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; <span class="kw2">var</span> startTime : <span class="kw3">int</span> = <span class="kw3">getTimer</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">var</span> pushed : <span class="kw3">Boolean</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">var</span> dictionary : Dictionary = <span class="kw2">new</span> Dictionary<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="kw1">while</span> <span class="br0">&#40;</span>length&#8211;<span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &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; &nbsp; &nbsp; &nbsp; &nbsp; pushed = <span class="kw2">false</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">while</span> <span class="br0">&#40;</span>!pushed<span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &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; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">var</span> thisRand : uint = <span class="kw3">Math</span>.<span class="kw3">ceil</span><span class="br0">&#40;</span><span class="kw3">Math</span>.<span class="kw3">random</span><span class="br0">&#40;</span><span class="br0">&#41;</span> * <span class="nu0">100000000</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>dictionary<span class="br0">&#91;</span>thisRand<span class="br0">&#93;</span> == <span class="kw3">undefined</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &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; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dictionary<span class="br0">&#91;</span>thisRand<span class="br0">&#93;</span> = <span class="kw2">true</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pushed = <span class="kw2">true</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &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; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &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; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">var</span> output : <span class="kw3">Array</span> = <span class="br0">&#91;</span><span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">for</span> <span class="br0">&#40;</span><span class="kw2">var</span> prop : <span class="kw3">String</span> <span class="kw1">in</span> dictionary<span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &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; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>castOutput<span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &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; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; output.<span class="kw3">push</span><span class="br0">&#40;</span><span class="kw3">parseInt</span><span class="br0">&#40;</span>prop<span class="br0">&#41;</span><span class="br0">&#41;</span>;&nbsp; &nbsp; &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; &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; <span class="kw1">else</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &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; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; output.<span class="kw3">push</span><span class="br0">&#40;</span>prop<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &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; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">trace</span><span class="br0">&#40;</span><span class="st0">&quot;Source Array containing &quot;</span> + output.<span class="kw3">length</span> + <span class="st0">&quot; items created in &quot;</span> + <span class="br0">&#40;</span><span class="kw3">getTimer</span><span class="br0">&#40;</span><span class="br0">&#41;</span> &#8211; startTime<span class="br0">&#41;</span> + <span class="st0">&quot;ms.&quot;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> output;<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="kw3">private</span> <span class="kw2">function</span> generateViaIndexOf<span class="br0">&#40;</span><span class="kw3">length</span> : <span class="kw3">int</span><span class="br0">&#41;</span> : <span class="kw3">Array</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; <span class="kw2">var</span> startTime : uint = <span class="kw3">getTimer</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">var</span> pushed : <span class="kw3">Boolean</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">var</span> output : <span class="kw3">Array</span> = <span class="br0">&#91;</span><span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">while</span> <span class="br0">&#40;</span>length&#8211;<span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &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; &nbsp; &nbsp; &nbsp; &nbsp; pushed = <span class="kw2">false</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">while</span> <span class="br0">&#40;</span>!pushed<span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &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; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">var</span> thisRand : uint = <span class="kw3">Math</span>.<span class="kw3">ceil</span><span class="br0">&#40;</span><span class="kw3">Math</span>.<span class="kw3">random</span><span class="br0">&#40;</span><span class="br0">&#41;</span> * <span class="nu0">100000000</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>output.<span class="kw3">indexOf</span><span class="br0">&#40;</span>thisRand<span class="br0">&#41;</span> == <span class="nu0">-1</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &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; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; output.<span class="kw3">push</span><span class="br0">&#40;</span>thisRand<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pushed = <span class="kw2">true</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &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; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &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; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">trace</span><span class="br0">&#40;</span><span class="st0">&quot;Source Array containing &quot;</span> + output.<span class="kw3">length</span> + <span class="st0">&quot; items created in &quot;</span> + <span class="br0">&#40;</span><span class="kw3">getTimer</span><span class="br0">&#40;</span><span class="br0">&#41;</span> &#8211; startTime<span class="br0">&#41;</span> + <span class="st0">&quot;ms.&quot;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> output;<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><br />
&nbsp;</div>
<p>When run, this code produces the following output:</p>
<pre>
Source Array containing 10000 items created in 3082ms.
Source Array containing 10000 items created in 71ms.
Source Array containing 10000 items created in 102ms.
Array sorted in 19ms.
Array sorted in 312ms.
Array sorted in 17ms.
indexOf[0] = 4532, indexOf[1] = 14806
nonCast[0] = 3553, nonCast[1] = 6646
cast[0] = 10432, cast[1] = 22513
</pre>
<p>We can immediatley draw the conclusion that performing Array.indexOf to determine if a value is unique in a given array is woefully inefficient, the use of a Dictionary (serving as a lookup table) is much better.  The other interesting point is that Array.sort(Array.NUMERIC) works quicker if the source values are cast to integers (instead of the Strings a for&#8230;var&#8230;in loop produces).</p>
<p>For those interested, my final stratergy will be to parse the data returned from the Service layer into strongly typed Objects and then store these in a Dictionary mapping User.id => User Object, then when I need to perform a sort I can simply dump the keys from the Dictionary into an Array.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jonnyreeves.co.uk/2009/05/sorting-values-in-actionscript-3/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using Dictionary Objects to Map Classes and Instances</title>
		<link>http://www.jonnyreeves.co.uk/2009/03/using-dictionary-objects-to-map-classes-and-instances/</link>
		<comments>http://www.jonnyreeves.co.uk/2009/03/using-dictionary-objects-to-map-classes-and-instances/#comments</comments>
		<pubDate>Thu, 19 Mar 2009 23:05:09 +0000</pubDate>
		<dc:creator>Jonny</dc:creator>
				<category><![CDATA[ActionScript 3]]></category>

		<guid isPermaLink="false">http://www.jonnyreeves.co.uk/?p=75</guid>
		<description><![CDATA[This is a neat little ActionScript 3 trick which I&#8217;ve been using more and more of recently that I thought I would share.  The basic premise is that it allows you to use a dictionary which maps Classes to other data, the twist is that you can supply either the Class definition or an instance [...]]]></description>
			<content:encoded><![CDATA[<p>This is a neat little ActionScript 3 trick which I&#8217;ve been using more and more of recently that I thought I would share.  The basic premise is that it allows you to use a dictionary which maps Classes to other data, the twist is that you can supply either the Class definition or an instance of the Class as the key.</p>
<p>The most obvious example I have come across for this is when creating Asset Mappings.  Let&#8217;s say I have a generic Model-esq class, ie: a Car and I know that each instance of Car (Ferrari, Porsche, etc) has an assosiated asset (ie: a SWF file).  These assets are stored on the server and use the <em>name</em> property as part of that assets filename, for example:</p>
<ul>
<li>Ferrari maps to /flash/assets/cars/ferrari.swf</li>
<li>Porsche maps to /flash/assets/cars/porsche.swf</li>
</ul>
<p>Now, it&#8217;s clear that the &#8220;/flash/assets/cars/&#8221; part of the URL is common to all instances of the Car class and we can leverage this fact when building URLs with the help of a URLManager Class and a common interface that all Model objects which contain Asset Paths share:</p>
<p><strong>Car Class</strong></p>
<div class="dean_ch" style="white-space: nowrap; overflow: scroll;">
package uk.<span class="me1">co</span>.<span class="me1">jonnyreeves</span>.<span class="me1">classmapping</span> <span class="br0">&#123;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="coMULTI">/**<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* Example Model Class for a Car<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">public</span> <span class="kw2">class</span> Car <span class="kw3">implements</span> IContainsAssetPath<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="coMULTI">/**<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* The name of this Car.<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">private</span> <span class="kw2">var</span> <span class="kw3">name</span> : <span class="kw3">String</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <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;* Constructor.<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* @param name&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; The name of this car.<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="kw2">function</span> Car<span class="br0">&#40;</span><span class="kw3">name</span> : <span class="kw3">String</span><span class="br0">&#41;</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; <span class="kw3">this</span>.<span class="kw3">name</span> = <span class="kw3">name</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;* Returns the name of this car.<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="kw2">function</span> getName<span class="br0">&#40;</span><span class="br0">&#41;</span> : <span class="kw3">String</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; <span class="kw1">return</span> <span class="kw3">this</span>.<span class="kw3">name</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;* Return the filename for this Car&#8217;s Asset SWF.<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="kw2">function</span> getAssetPath<span class="br0">&#40;</span><span class="br0">&#41;</span> : <span class="kw3">String</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; <span class="kw1">return</span> <span class="kw3">this</span>.<span class="kw3">name</span> + <span class="st0">&quot;.swf&quot;</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><br />
&nbsp;</div>
<p><b>IContainsAssetPath interface</b></p>
<div class="dean_ch" style="white-space: nowrap; overflow: scroll;">
package uk.<span class="me1">co</span>.<span class="me1">jonnyreeves</span>.<span class="me1">classmapping</span> <span class="br0">&#123;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="coMULTI">/**<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* Common Interface for all Models with Assets<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">public</span> <span class="kw3">interface</span> IContainsAssetPath <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="coMULTI">/**<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* Returns the final stub path to this asset.<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">function</span> getAssetPath<span class="br0">&#40;</span><span class="br0">&#41;</span> : <span class="kw3">String</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span><br />
&nbsp;</div>
<p>Finally, the URLManager which will help build the URLs for all Models which implement IContainAssetPath</p>
<p><b>URLManager Class</b></p>
<div class="dean_ch" style="white-space: nowrap; overflow: scroll;">
package uk.<span class="me1">co</span>.<span class="me1">jonnyreeves</span>.<span class="me1">classmapping</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="me1">Dictionary</span>;&nbsp; </p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="coMULTI">/**<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* Helper for building URLs from Car Objects.<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">public</span> <span class="kw2">class</span> URLManager <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="coMULTI">/**<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* Dictionary which will contain mappings between Class Definitions<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* and URL Stubs used to build asset paths.<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">private</span> <span class="kw3">static</span> <span class="kw2">var</span> objectMappings : Dictionary = <span class="kw2">new</span> Dictionary<span class="br0">&#40;</span><span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="coMULTI">/**<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* Singleton Instance.<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">private</span> <span class="kw3">static</span> <span class="kw2">var</span> instance : URLManager;</p>
<p>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="coMULTI">/**<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* Singleton Accessor Method.<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="kw3">static</span> <span class="kw2">function</span> getInstance<span class="br0">&#40;</span><span class="br0">&#41;</span> : URLManager<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="kw1">if</span> <span class="br0">&#40;</span>instance == <span class="kw2">null</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &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; &nbsp; &nbsp; &nbsp; &nbsp; instance = <span class="kw2">new</span> URLManager<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="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> instance;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="coMULTI">/**<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* Private Constructor for Singleton Pattern.<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="kw2">function</span> URLManager<span class="br0">&#40;</span><span class="br0">&#41;</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; createObjectMappings<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="coMULTI">/**<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* Populate the ObjectMappings Dictionary.<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*/</span>&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">private</span> <span class="kw2">function</span> createObjectMappings<span class="br0">&#40;</span><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; <span class="co1">// Here we create relationships between the Class definitions and their</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// URL Stubs. &nbsp;We use the constructor object to create a common relation</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// between ClassDefinitions and instances of those Class Definitions.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; objectMappings<span class="br0">&#91;</span>Car.<span class="kw3">prototype</span>.<span class="kw3">constructor</span><span class="br0">&#93;</span> = <span class="st0">&quot;/flash/assets/cars/&quot;</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;* Retrieve the AssetURL for the supplied instance of the IContainsAssetPath interface<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="kw2">function</span> getAssetURL<span class="br0">&#40;</span>source : IContainsAssetPath<span class="br0">&#41;</span> : <span class="kw3">String</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; <span class="co1">// Here we can extract the constructor object from the incoming IContainsAssetPath</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// implementation. &nbsp;This will be the same object as the one we stored in </span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// this.createObjectMappings();</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">var</span> sourceConstructor : <span class="kw3">Object</span> = <span class="br0">&#40;</span>source as <span class="kw3">Object</span><span class="br0">&#41;</span>.<span class="kw3">constructor</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// Check to see if a mapping exists for this Class Definition.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>objectMappings<span class="br0">&#91;</span>sourceConstructor<span class="br0">&#93;</span> == <span class="kw3">undefined</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &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; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">throw</span> <span class="kw2">new</span> <span class="kw3">Error</span><span class="br0">&#40;</span><span class="st0">&quot;Asset Mapping does not exist for object: &quot;</span> + source<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &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; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// We can now build the complete URL.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> objectMappings<span class="br0">&#91;</span>sourceConstructor<span class="br0">&#93;</span> + source.<span class="me1">getAssetPath</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><br />
&nbsp;</div>
<p>The technique relies on the fact that the Class Definition&#8217;s prototype will yield the same constructor object as the instance of that Class Definition.  This has come in incredibly handy when I only have the Class Definitions avaliable to me whilst writing code but need to create mappings to them at run-time.  For those that want a complete the example, <a href="http://www.jonnyreeves.co.uk/wp-content/uploads/2009/03/classmapping-source.zip">here is the source</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jonnyreeves.co.uk/2009/03/using-dictionary-objects-to-map-classes-and-instances/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MoshiMonsters.com Homepage Redesign</title>
		<link>http://www.jonnyreeves.co.uk/2009/03/moshimonsterscom-homepage-redesign/</link>
		<comments>http://www.jonnyreeves.co.uk/2009/03/moshimonsterscom-homepage-redesign/#comments</comments>
		<pubDate>Tue, 10 Mar 2009 21:07:06 +0000</pubDate>
		<dc:creator>Jonny</dc:creator>
				<category><![CDATA[ActionScript 3]]></category>
		<category><![CDATA[Personal]]></category>

		<guid isPermaLink="false">http://www.jonnyreeves.co.uk/?p=69</guid>
		<description><![CDATA[As some of you may know, I am currently working for MindCandy as an ActionScript 3 developer hacking away on Moshi Monsters &#8211; a safe, online world for kids to learn and play.  This week is a very proud one for myself as the homepage redesign which I worked on went live &#8211; I thought [...]]]></description>
			<content:encoded><![CDATA[<p>As some of you may know, I am currently working for <a href="http://mindcandy.com/">MindCandy</a> as an ActionScript 3 developer hacking away on <a href="http://moshimonsters.com/">Moshi Monsters</a> &#8211; a safe, online world for kids to learn and play.  This week is a very proud one for myself as the homepage redesign which I worked on went live &#8211; I thought I would take the opportunity to explain some of the problems faced and the solutions used.</p>
<p style="text-align: left;">
<p style="text-align: center;"><a href="http://moshimonsters.com/"><img class="size-full wp-image-70 aligncenter" title="MoshiMonsters.com Homepage, March 2009" src="http://www.jonnyreeves.co.uk/wp-content/uploads/2009/03/moshi-homepage.jpg" alt="MoshiMonsters.com Homepage, March 2009" width="500" height="280" /></a></p>
<p style="text-align: left;">First off I need to give full credit where it&#8217;s due; to our amazing designers.  I got to work with three such talented chaps on this project, <a href="http://mindcandy.com/people/#trev">Trevor</a>, <a href="http://mindcandy.com/people/#benp">Ben P</a>. and our latest hire, Axel.  These guys spent a good week illustrating the concept for the new homepage and creating the animations and elements which I wired up to make the finished product.</p>
<p style="text-align: left;">The page splits into two parts, the main body and the navigation is delivered in normal HTML with CSS styling and the central content including the monsters and ocean background are delivered using Flash.  One of the challenges to over-come was the fact the page had to scale horizontally to allow the user to resize their browser, this was achieved by using the ActionScript <a href="http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/display/Stage.html#scaleMode">StageScale.NO_SCALE</a> setting and using <a href="http://code.google.com/p/swfobject/">SWFObject</a> to set the embed width to 100%.  By <a href="http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/display/Stage.html#event:resize">registering an Event.RESIZE  listener to the Stage</a>, I am able to adjust the width property of the background gradients&#8217; (which make up the ocean) DisplayObject.  It&#8217;s awesome to see the site still look great even when stretched across two 24&#8243; monitors!</p>
<p style="text-align: left;">One of the key points of the new homepage&#8217;s <a href="http://en.wikipedia.org/wiki/Acceptance_testing">Acceptance Test</a> was that it weighed around the same as the static HTML version it was replacing.  Using <a href="http://getfirebug.com/">firebug</a>, I was able to tell that the original homepage weighed ~360kb; it wasn&#8217;t going to be easy providing all this interactivity.  We managed to come in at ~430kb which wasn&#8217;t too far off!  We made savings filesize savings on both the HTML and Flash by a combination of <a href="http://www.alistapart.com/articles/sprites/">CSS Spriting</a> and optimisation of all animations and MovieClips (the majority of the Flash optimisations were done by our Animator Ben P. so you will have to ask him for his tips and tricks!).  We didn&#8217;t go down the route of trying to optimise the ActionScript to reduce filesize as legibility is more important to us than a couple of kilobytes at the CDN.</p>
<p style="text-align: left;">Finally, the last goal of the new site was that it displayed on the user screen as quickly as possible &#8211; the last thing we wan the user to see is a massive white hole in the middle of our page whilst it loads!  Pre-Loaders in ActionScript 3 are not quite as intuitive as they used to be in AS2, but thanks to <a href="http://www.bit-101.com/blog/?p=946">Keith over at Bit-101, they are reliviley straight forward once you know what you&#8217;re doing</a>!  During the Preloader we render the background gradients in actionscript which helps the Flash element fit seamlessly with the HTML and CSS while the remaining 200kb loads.  Once the app has finished loading, the user can start interacting with the buttons, watch the tour video, etc.  At this point, we send off a background call to load in the monster animations and sound effects.  The monster anims are all contained in a single swf which weighs in around 130kb &#8211; we chose to use a single swf to reduce the number of requests on the web server (infact the entire page now renders in under 15 requests to our own server, down from well over 30!).  The monster animations SWF contains linkages for all the various animations &#8211; these are loaded into the <a href="http://help.adobe.com/en_US/AS3LCR/Flash_10.0/flash/system/ApplicationDomain.html">Current ApplicationDomain</a> and then initialised via<a href="http://thillerson.wordpress.com/2007/03/01/runtime-class-instantiation-in-actionscript-30/"> getDefinitionByName</a>.</p>
<p style="text-align: left;">If you are already a Moshi user then I hope you enjoy the new MoshiMonsters homepage and all the new features that our team keeps working on &#8211; and if you&#8217;re not yet a member, well get on over there and adopt one today!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jonnyreeves.co.uk/2009/03/moshimonsterscom-homepage-redesign/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Steaming HD Content to the Playstation 3 with Windows Media Player 11</title>
		<link>http://www.jonnyreeves.co.uk/2009/01/steaming-hd-content-to-the-playstation-3-with-windows-media-player-11/</link>
		<comments>http://www.jonnyreeves.co.uk/2009/01/steaming-hd-content-to-the-playstation-3-with-windows-media-player-11/#comments</comments>
		<pubDate>Sat, 31 Jan 2009 15:54:00 +0000</pubDate>
		<dc:creator>Jonny</dc:creator>
				<category><![CDATA[Personal]]></category>
		<category><![CDATA[homeplug]]></category>
		<category><![CDATA[media playback]]></category>
		<category><![CDATA[mkv]]></category>
		<category><![CDATA[mp4]]></category>
		<category><![CDATA[playstation 3]]></category>
		<category><![CDATA[ps3]]></category>
		<category><![CDATA[wireless]]></category>
		<category><![CDATA[xmbc]]></category>

		<guid isPermaLink="false">http://www.jonnyreeves.co.uk/?p=66</guid>
		<description><![CDATA[This month I took the plunge and upgraded my entire Media Setup in the front room by replacing my ancient Goodmans 32&#8243; CRT TV with a Samsung 37&#8243; 720p LCD.  As with most major upgrades, this started a chain of events which resulted in me replacing my trust soft-modded Xbox running XBMC with a shiny [...]]]></description>
			<content:encoded><![CDATA[<p>This month I took the plunge and upgraded my entire Media Setup in the front room by replacing my ancient Goodmans 32&#8243; CRT TV with a Samsung 37&#8243; 720p LCD.  As with most major upgrades, this started a chain of events which resulted in me replacing my trust soft-modded Xbox running XBMC with a shiny new Playstation 3 to take advantage of the HDMI Output, HD Playback and enhanced upscaling which the system offers over the Xbox. (The compression artifacts on Xvids became incredibly distracting on the LCD screen and the Xbox is simply not powerful enough to play back HD.)</p>
<p>The first step in my journey was to setup a <a href="http://blog.us.playstation.com/2007/12/27/playstation-tips-4-dlna-media-server/">DLNA Media Server</a> to push my media to the Playstation.  I must admit that I was pretty disappointed that I couldn&#8217;t just set up SMB shares as I could on XBMC &#8211; but then the entire Playstation 3 experience isn&#8217;t terribly configurable (I guess Sony recon most people aren&#8217;t interested in tinkering!).  After a bit of reading on the <a href="http://avsforum.com">AVSForums</a>, I realised that my two options for the DNLA Media Server on Windows Vista x64 were Windows Media Player 11 and <a href="http://tversity.com/">TVerstity</a>.  Havnig a bit of a distruct of Windows Media Player, I opted to download and install <a href="http://tversity.com/">TVersity </a>from their site &#8211; however, during the install I was instructed to turn off UAC (Vista&#8217;s User Account Control security model).  I know a lot of people hate UAC but I see it as a serious step forward for Window&#8217;s security model and if programmers aren&#8217;t willing to write their software to cope and comply with it then I&#8217;m not interested in running it &#8211; so <a href="http://tversity.com/">TVersity </a>was ruled out leaving Windows Media Player as the only option.</p>
<p>Setting up DNLA Media Sharing on WMP11 (Windows Media Player 11) is straight forward &#8211; I was also able to add additional folders outside of the &#8220;My Videos&#8221; folder in my User Account folder visible.  Once this was done (and my PS3 was granted access), My computer showed up in the list of avaliable sources on the Playstation&#8217;s &#8216;Home&#8217; screen and all of my Xvid files were immediatley made avaliable and played back without hassle (Complete with excellent upscaling and filtering with the Playstation 3 offers which smooths out nearly all compression artifacts without making the image appear out of focus).  However, all was not smooth as every 10 minutes or so the video stream would break up between key frames due to network dropouts &#8211; a problem which I had not experienced on the Xbox.  I presumed that the dropouts were due to the PS3 not using a large enough cache for the video stream; wireless networks are inherently dodgy and prone to dropouts &#8211; I could live with this for the time being, but I knew it would have to be addressed at some point.</p>
<p>I was fairly happy at this point &#8211; Xvids were playing back and looking better than on the Xbox, but it was time to try out some HD content and start playing back some video encoded using the h264 codec.</p>
<p>Using <a href="http://handbrake.fr/">Handbrake</a>, I converted a set of DVD VOBs into an MP4 file using the Playstation 3 preset.  The resulting MP4 file looked amazing on the computer via Quicktime and I was looking forward to being able to archive all my DVDs in this new, higher quality format.  I put the MP4 file into the &#8220;My Videos&#8221; file and then jumped onto the PS3.  When I first looked, I couldn&#8217;t see the MP4 file, so I restarted the PS3 &#8211; still no MP4 file &#8211; this is when I started to get a bit upset.</p>
<p>After a bit of googling I discovered that Windows Media Player 11 does not, by default, support the &#8220;discovery&#8221; of h264.  I was surprised by this as I had already installed the <a href="http://www.cccp-project.net/">CCCP (Combined Community Codec Pack)</a> and the files could play back via WMP11 on the PC.  Luckily there is a <a href="http://a8t8.spaces.live.com/blog/cns!2518DD508BB713E8!156.entry">small registry hack</a> (compatible with both Vista 32 and 64) which enables the discovery of h264 content.  Once the patch is installed and your computer is restarted, the PS3 will be able to see the mp4 files &#8211; excellent!  I dived onto the couch and hit the Play button the Remote &#8211; only to be bitterly disappointed.</p>
<p>Although the video played back (and looked fantastic compared to the Xvids before it) &#8211; the dropout made it un-watchable.  The video was stuttering all over the place!  A quick check via the &#8216;Select&#8217; button showed that the video was trying to steam 5mbps over the wireless &#8211; seeing as 802.11g only offers an ideal bandwidth of 54mpbs (which is pretty much theoretical) I knew that asking it for 5mbps (plus the overhead from the DLNA services) was pushing it too far!  As my house doesn&#8217;t have CAT-5 running through the walls, my own hope was to purchase a set of <a href="http://en.wikipedia.org/wiki/HomePlug">HomePlug network devices</a> to run the network data over my home&#8217;s powerlines.  I&#8217;ve always been a bit dubious of HomePlug&#8217;s and had heard negative things about them but seeing as I didn&#8217;t have many optins left, I opted for a pair of <a href="http://www.scan.co.uk/Product.aspx?WebProductId=947922">Linksys PLE200&#8217;s from Scan.co.uk</a> (I was quite suprised that they were only £49 for a pack of 2, I expected to pay twice that much).  These devices touted that they were capable of stream HD Content over a network and could manage 200mbps (Which is a lovely bit of marketing seeing as they only have 100mbps interfaces &#8211; the 200mbps mentioned on the box is referring to the devices running in full duplex mode &#8211; and seeing as I only have a 100mbps router, it all becomes a bit of a moot point).</p>
<p>The Homeplugs arrived the next day and I set about installing them.  The box told me to install the management software before plugging them in.  I dutifly inserted the CD and ran the software only to be greeted by an error dialogue reading: &#8220;Failed to initialize Networks Interface &#8211; exiting&#8221;.  I guess the software that ships with the devices isn&#8217;t compatible with Vista x64 then.  Throwing caution to the window I proceeded to plug the devices in, wiring one into my router and the other into the back of the PS3.  I then headed to the Linksys website and began downloading the <a href="http://www.linksysbycisco.com/US/en/support/PLE200/download">latest drivers for the PLE200</a>.  The download was coming in at a miserable 60kbps so I switch my attention back over the PS3 and went to see if the devices would work without the managment software (after all they are network devices &#8211; what setup do the really need once they gain a DHCP lease from the router?).  To my suprise, the PS3 managed to gain a lease and sucessfully connect to the Internet &#8211; score one for Homeplugs!  Seeing as I live in a bungalow I don&#8217;t really see the point of enabling encryption on the devices (which I&#8217;m pretty sure would just limit the amout of avaliable bandwidth anyway due to the overhead) so I am yet to install the latest managment software (and to be honest, I don&#8217;t really want to install another bit of crapware on this box anyway!).</p>
<p>With the Homeplugs setup and my PS3 back on the network, I tried to watch the mp4 file again &#8211; to my delight it started playing back smoothly; a smile almost crept onto my face!</p>
<p>So, now that I had x264&#8217;s playing back, my last hurdle was to get true 720p HD content in the MKV container to play back on the PS3.  I knew already that the PS3 (nor WMP11) supports the MKV container (which is a great shame, as it&#8217;s the best replacement for the aging AVI contaner out there), so I jumped back onto Google and came across a guide which detailed <a href="http://hubpages.com/hub/How-To-Play-MKV-Files-On-Playstation-3">How to Play MKV Files on the Playstation 3</a>.  As MKV is just a container, your first step is to swap out the contained Video and Audio streams and place them into a container the PS3 can accept (VOB).  The piece of software which performs this magic mxing is called (immaginativley) <a href="http://www.mkv2vob.com/">MKV2VOB</a> and does exactly what is says on the tin with no loss of quality.</p>
<p>MKV2VOB takes about a minute to swap the containers of a 120 minute MKV movie, once the conversion was complete I was able to see the resuling .mpg file on the PS3 but it complained about &#8220;Corrupt Data&#8221;, a commenter on an earlier Blog Post pointed out that he had to change the file extension to be &#8220;.MP4&#8243; before the file would playback on the PS3 and sure enough this worked and I was able to watch the re-muxed MKV on the PS3 in glorious HD with an average datarate of almost 10mbps! &#8211; lovley stuff.  To make future transcoding easier, MKV2VOB has a setting in it&#8217;s Configuration tab to automatically rename the output file to .MP4 and then delete the source MKV &#8211; combined with a bit of scripting, it will probably be farily easy to get this conversion to happen automatically.</p>
<p>The only thing I need to figure out now is:</p>
<ul>
<li>How to get thumbnails to appear on the PS3 when browsing the Video&#8217;s served via WMP11</li>
<li>How to change to way the folders are presented on the PS3 as all my media is currently buried under a massive folder hierarchy.  (Videos -&gt; Folders -&gt; Videos -&gt; TV Shows!)</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.jonnyreeves.co.uk/2009/01/steaming-hd-content-to-the-playstation-3-with-windows-media-player-11/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Dead Space Review.</title>
		<link>http://www.jonnyreeves.co.uk/2008/12/dead-space-review/</link>
		<comments>http://www.jonnyreeves.co.uk/2008/12/dead-space-review/#comments</comments>
		<pubDate>Thu, 04 Dec 2008 22:06:11 +0000</pubDate>
		<dc:creator>Jonny</dc:creator>
				<category><![CDATA[Game Reviews]]></category>
		<category><![CDATA[Personal]]></category>

		<guid isPermaLink="false">http://www.jonnyreeves.co.uk/?p=64</guid>
		<description><![CDATA[I&#8217;ve been doing a bit of reviewing over at dooyoo.co.uk in an attempt to raise a bit of pocket money in my down time.  I thought I&#8217;d cross post my thoughts on Dead Space.
It&#8217;s not often that a game will scare you senseless, but Dead Space is one of those rare gems which, if you [...]]]></description>
			<content:encoded><![CDATA[<p><em>I&#8217;ve been doing a bit of reviewing over at <a href="http://dooyoo.co.uk/">dooyoo.co.uk</a> in an attempt to raise a bit of pocket money in my down time.  I thought I&#8217;d cross post my thoughts on Dead Space.</em></p>
<p>It&#8217;s not often that a game will scare you senseless, but Dead Space is one of those rare gems which, if you allow it, will leave you with nightmarish visions, twitching in the corner!</p>
<p>Dead Space takes a slightly different spin on the usual First Person Shooter genre by positioning the camera just behind your avatar&#8217;s shoulder, in a similar vein to Gears of War. At first I didn&#8217;t think I&#8217;d like this angle, but after a few hours of gameplay I hardly even noticed the aspect shift; infact I&#8217;ll go as far as to say that I think it adds to the game, showing your character (Isaac) getting slammed to the floor, or dragged along by his leg by a monstrous tentacle, which brings me nicely onto the best aspect of dead space &#8211; the enemy!</p>
<p style="text-align: center;"><img class="aligncenter" style="vertical-align: baseline;" src="http://ps3media.ign.com/ps3/image/article/825/825859/dead-space-20071009030504067-000.jpg" alt="" width="480" height="270" /></p>
<p>The game&#8217;s opening cinematic gives nothing away &#8211; you and a small crew aboard a service ship are heading towards the USG Ishimura to repair a damaged comms array; from the moment you step foot off the shuttle you can fear the tension close in around you knowing full well that something is about to go hideously wrong. This is one game that really needs to be played in the dark with a good pair of headphones in order for you to get the most out of it; The audio is superb with a great score which puts the predictable dross of the Lost Soundtrack to shame. This is further heightened by whispering voices, malfunctioning equipment and eerie clanking noises emanating from the vents and ducts. The game has some excellent environment effects, the first time you experience a hull breach and are introduced to the vacuum of space is pretty intense.</p>
<p>The game continues the immersion by removing all HUD elements from the screen; this is replaced with a health meter running down your character&#8217;s spine and ammo readouts on the weapons themselves. The developers came up with a clever system for accessing the inventory; pressing the Tab key will project a hologram infront of your avatar which moves around in the game world. Activating the inventory does pause the game, so there&#8217;s no respite in the midst of an intense battle, again adding to the sense of urgency rather than taking away from it. New weapons, ammo and items can be purchased from the shops which are scattered around the game &#8211; I find it hard to believe that these would still be functional whilst the entire crew lies in pieces, but I guess the story writers ran out of good ideas!</p>
<p><a href="http://www.escapistmagazine.com/videos/view/zero-punctuation/333-Dead-Space">Some reviewers</a> have critiqued Dead Space for its unimaginative and repetitive scenery, however I think this is slightly unjust given the setting of the game (Spaceships, for all their intergalactic glory, are pretty grey affairs on the inside). The Game progresses you through multiple parts of the Ship with boss battles taking place around the usual landmarks (The sequence in the morgue is particularly memorable). However I will agree with others that the range of weapons is lacking &#8211; you&#8217;ll often find yourself falling back one one or two staple weapons, especially when the ammo starts getting sparse later in the game, but I guess we should be grateful that the developers didn&#8217;t fall back on the usual Doom formula of shotgun, rifle, rocket launcher.</p>
<p>Dead Space is another one of those game which feels like a console port &#8211; for example, you can only save you game at certain Save Points (at which you are also limited to four saves!) and there is no Quick Save (but again, this adds to the tension and makes you value your health!). The transition from gamepad to keyboard feels slightly strange and no matter how I remapped the keys I never felt I had that &#8220;Half-life level&#8221; of keybinding. There are also certain gameplay elements that don&#8217;t translate as well; one mission has you shooting at incoming asteroids, something which is quite tricky on a PS3 (or so I&#8217;m told), but it was just 5 minutes of tedium for me as I popped off rock after rock with the pinpoint precision a mouse offers you)</p>
<p>The graphics are one place where the PC version shines above its console brethren; I was able to run the game with everything on High setting at 1680&#215;1050 resolution on my 768mb Nvidia 9800GTS (far in excess of the 720p limit on the consoles). One small problem that I came across was the incredible mouse lag when I first started it up; this was quickly solved by turning off the VSync setting in the Graphics menu (and no, this did not produce any visual tearing for me). It should also be noted that you can further improve the graphics by enabling Anti-Aliasing in your Graphics Card settings as there are no AA Options in the Game.</p>
<p>To conclude, I really can&#8217;t fault Dead Space, it&#8217;s been a long time since I&#8217;ve played a game compelling enough to warrant 8 hour stints that last long into the night. Of course, this game is not without its faults, but it would definitely be in my Top 5 picks of 2008. This game would make an excellent present for any avid gamer, but I wouldn&#8217;t reccomend letting younger children play it due to the extreme graphic nature and the incredibly dark and twisted setting (I&#8217;ve woken up from a couple of warped dreams since playing this game!)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jonnyreeves.co.uk/2008/12/dead-space-review/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
