<?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>pixelbreaker &#187; Tips</title>
	<atom:link href="http://blog.pixelbreaker.com/category/flash/tips/feed" rel="self" type="application/rss+xml" />
	<link>http://blog.pixelbreaker.com</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Mon, 21 Dec 2009 12:58:33 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>AS3.0 Memory monitoring</title>
		<link>http://blog.pixelbreaker.com/flash/as30-memory-monitoring</link>
		<comments>http://blog.pixelbreaker.com/flash/as30-memory-monitoring#comments</comments>
		<pubDate>Fri, 09 Nov 2007 10:43:26 +0000</pubDate>
		<dc:creator>pixelbreaker</dc:creator>
				<category><![CDATA[ActionScript 3.0]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Tips]]></category>

		<guid isPermaLink="false">http://blog.pixelbreaker.com/flash/as30-memory-monitoring/</guid>
		<description><![CDATA[I just came across the property System.totalMemory. It&#8217;s very useful, especially for the engine I am currently working on, and dealing with AS3 garbage collection. It returns the memory currently being used by the Flash Player. It&#8217;s in bytes so it pays to clean it up slightly. [as] var mem:String = Number( System.totalMemory / 1024 [...]]]></description>
			<content:encoded><![CDATA[<p>I just came across the property <em>System.totalMemory</em>. It&#8217;s very useful, especially for the engine I am currently working on, and dealing with AS3 garbage collection.</p>
<p>It returns the memory currently being used by the Flash Player.  It&#8217;s in bytes so it pays to clean it up slightly.</p>
<p>[as]<br />
var mem:String = Number( System.totalMemory / 1024 / 1024 ).toFixed( 2 ) + &#8216;Mb&#8217;;<br />
trace( mem ); // eg traces &#8220;24.94Mb&#8221;<br />
[/as]</p>
<p>It seems this takes account of all instances of the flash player. For example, I have a logger/debug panel running in the sidebar of firefox, built in flex. As soon as I open this and it&#8217;s initialised, it uses an extra 5 &#8211; 10mb of ram. So be warned, if you see a massive increase in memory usage, check you don&#8217;t have loads of sites open with ad banners or other flash based stuff.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.pixelbreaker.com/flash/as30-memory-monitoring/feed</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>AS3.0 JIT vs. Interpreted</title>
		<link>http://blog.pixelbreaker.com/flash/as30-jit-vs-interpreted</link>
		<comments>http://blog.pixelbreaker.com/flash/as30-jit-vs-interpreted#comments</comments>
		<pubDate>Sun, 09 Sep 2007 12:10:54 +0000</pubDate>
		<dc:creator>pixelbreaker</dc:creator>
				<category><![CDATA[ActionScript 3.0]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Tips]]></category>

		<guid isPermaLink="false">http://blog.pixelbreaker.com/flash/as30-jit-vs-interpreted/</guid>
		<description><![CDATA[I&#8217;ve been developing an application recently that requires large numbers of classes to be initialised as soon as the application is loaded. It&#8217;s an isometric display engine, a la &#8220;Sim City&#8221;, with a world map of 800 x 600 tiles, and multiple layers ( terrain, objects, transport networks etc) there&#8217;s potential for over 100,000 instances!!! [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been developing an application recently that requires large numbers of classes to be initialised as soon as the application is loaded. It&#8217;s an isometric display engine, a la &#8220;Sim City&#8221;, with a world map of 800 x 600 tiles, and multiple layers ( terrain, objects, transport networks etc)  there&#8217;s potential for over 100,000 instances!!!</p>
<p>I found <a href="http://www.onflex.org/ACDS/AS3TuningInsideAVM2JIT.pdf">ActionScript 3.0 and AVM2: Performance Tuning</a> by Gary Grossman, which covers many aspects of how AVM2 is different from AVM1. The part I was interested in was about $init and $cinit (class constructor) functions being interpreted and everything else being JIT.</p>
<p>The initial setup of the engine runs over a timed loop (to stop script timeouts), and this was typically taking 20 to 30 seconds. After reading the above PDF, it became clear that class constructors are interpreted, not JIT compiled, so all I had to do was move the code out of the constructor, into an init() function, and call if after the constructor, this shaved a huge amount of time off the initial build of the landscape.</p>
<p>Some initial tests, with a for loop running from 0 to 10000 showed that in the constructor it took about 350ms, and called via another method, it took only 240ms. It is not always faster, but 90% of the time it&#8217;s faster.</p>
<p><span id="more-58"></span><br />
here&#8217;s my test example<br />
[as]<br />
public class Class1<br />
{<br />
	import flash.utils.getTimer;</p>
<p>	public function Class1()<br />
	{<br />
		var t:int = getTimer();<br />
		var a:Array = [];<br />
		var i:int;<br />
		for( i=0; i< 1000000; i++)<br />
		{<br />
			var x:int = i*i;<br />
			a.push( x );<br />
		}<br />
		trace( &#8216;constructor: &#8216; + (getTimer() &#8211; t) );<br />
	}</p>
<p>	public function init():void<br />
	{<br />
		var t:int = getTimer();<br />
		var a:Array = [];<br />
		var i:int;<br />
		for( i=0; i< 1000000; i++)<br />
		{<br />
			var x:int = i*i;<br />
			a.push( x );<br />
		}<br />
		trace( &#8216;init method: &#8216; + (getTimer() &#8211; t) );<br />
	}<br />
}<br />
[/as]</p>
<p>in the .fla I simply add:<br />
[as]<br />
var c1:Class1 = new Class1();<br />
c1.init();<br />
[/as]</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.pixelbreaker.com/flash/as30-jit-vs-interpreted/feed</wfw:commentRss>
		<slash:comments>19</slash:comments>
		</item>
		<item>
		<title>AS3.0 Better Singletons</title>
		<link>http://blog.pixelbreaker.com/flash/as30-better-singletons</link>
		<comments>http://blog.pixelbreaker.com/flash/as30-better-singletons#comments</comments>
		<pubDate>Thu, 16 Aug 2007 20:49:10 +0000</pubDate>
		<dc:creator>pixelbreaker</dc:creator>
				<category><![CDATA[ActionScript 3.0]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Tips]]></category>

		<guid isPermaLink="false">http://blog.pixelbreaker.com/flash/as30-better-singletons/</guid>
		<description><![CDATA[I&#8217;ve found a better more robust way of enforcing singletons in AS3.0 [as] package { public class Singleton { public static var instance:Singleton; public static function getInstance():Singleton { if( instance == null ) instance = new Singleton( new SingletonEnforcer() ); return instance; } public function Singleton( pvt:SingletonEnforcer ) { // init class } } } [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve found a better more robust way of enforcing singletons in AS3.0</p>
<p>[as]<br />
package<br />
{<br />
	public class Singleton<br />
	{<br />
		public static var instance:Singleton;</p>
<p>		public static function getInstance():Singleton<br />
		{<br />
			if( instance == null ) instance = new Singleton( new SingletonEnforcer() );<br />
			return instance;<br />
		}</p>
<p>		public function Singleton( pvt:SingletonEnforcer )<br />
		{<br />
			// init class<br />
		}<br />
	}<br />
}</p>
<p>internal class SingletonEnforcer{}<br />
[/as]<br />
<strong>Note:</strong> The class &#8220;SingletonEnforcer&#8221; is actually placed within the same .as file as the Singleton class, but placed outside the package{} parenthesis, therefore, the class &#8220;SingletonEnforcer&#8221; can only be accessed from within this .as file, so if the Singleton&#8217;s constructor is called from anywhere else, you&#8217;ll get an error.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.pixelbreaker.com/flash/as30-better-singletons/feed</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>AS3.0 Reversed for loop</title>
		<link>http://blog.pixelbreaker.com/flash/as30-reversed-for-loop</link>
		<comments>http://blog.pixelbreaker.com/flash/as30-reversed-for-loop#comments</comments>
		<pubDate>Thu, 16 Aug 2007 11:18:58 +0000</pubDate>
		<dc:creator>pixelbreaker</dc:creator>
				<category><![CDATA[ActionScript 3.0]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Tips]]></category>

		<guid isPermaLink="false">http://blog.pixelbreaker.com/flash/as3-tip-reversed-for-loops/</guid>
		<description><![CDATA[Today I came across an easy mistake to make when new to AS3. I set up a for loop, to count backwards through an Array. But for some strange reason the loop was never terminating! It was simple, here&#8217;s the original code [as] var i:uint; for( i=arr.length-1; i>=0; i&#8211; ) { } [/as] The mistake [...]]]></description>
			<content:encoded><![CDATA[<p>Today I came across an easy mistake to make when new to AS3.</p>
<p>I set up a for loop, to count backwards through an Array. But for some strange reason the loop was never terminating! It was simple, here&#8217;s the original code</p>
<p>[as]<br />
var i:uint;<br />
for( i=arr.length-1; i>=0; i&#8211; )<br />
{<br />
}<br />
[/as]</p>
<p>The mistake was simple, I had set i as a <em>uint</em>, so it could never go below 0, so just had to make i an <em>int</em>. It sounds stupid once you realise it, but it wasted enough of my time!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.pixelbreaker.com/flash/as30-reversed-for-loop/feed</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Looping within a range</title>
		<link>http://blog.pixelbreaker.com/flash/looping-within-a-range</link>
		<comments>http://blog.pixelbreaker.com/flash/looping-within-a-range#comments</comments>
		<pubDate>Thu, 26 Jul 2007 15:32:28 +0000</pubDate>
		<dc:creator>pixelbreaker</dc:creator>
				<category><![CDATA[ActionScript 2.0]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Tips]]></category>

		<guid isPermaLink="false">http://blog.pixelbreaker.com/flash/looping-within-a-range/</guid>
		<description><![CDATA[Something I have to do very often is looping a number within a range. This could be for a carousel navigation, a rotating banner or image slideshow, etc. The concept is simple, have a range, say 1 &#8211; 20, and simply call next() / previous() on it and return the new number, if it goes [...]]]></description>
			<content:encoded><![CDATA[<p>Something I have to do very often is looping a number within a range. This could be for a carousel navigation, a rotating banner or image slideshow, etc. The concept is simple, have a range, say 1 &#8211; 20, and simply call next() / previous() on it and return the new number, if it goes above 20, return to 1, if below 1, return to 20.<br />
<span id="more-55"></span><br />
The AS2.0 class is as follows<br />
[as]<br />
class com.pixelbreaker.number.LoopRange<br />
{<br />
	public var min:Number;<br />
	public var max:Number;</p>
<p>	private var _current:Number;</p>
<p>	function LoopRange( min:Number, max:Number, start:Number )<br />
	{<br />
		this.min = min;<br />
		this.max = max;<br />
		_current = start || min;<br />
	}</p>
<p>	public function next():Number<br />
	{<br />
		_current = _current+1 <= max? _current+1 : min;<br />
		return _current;<br />
	}</p>
<p>	public function previous():Number<br />
	{<br />
		_current = _current-1 >= min? _current-1 : max;<br />
		return _current;<br />
	}</p>
<p>	public function set current( value:Number ):Void<br />
	{<br />
		if( value >= min &#038;&#038; value <= max ) _current = value;<br />
		else throw new GenericError( &#8216;number &#8216; + value + &#8216; is out of range [ ' + min + ', ' + max + ' ]&#8216; );<br />
	}</p>
<p>	public function get current():Number<br />
	{<br />
		return _current;<br />
	}<br />
}<br />
[/as]</p>
<p>To implement this on an array you would do the following</p>
<p>[as]<br />
import com.pixelbreaker.number.LoopRange;</p>
<p>var slideShowImages:Array = [];<br />
for( var i:Number = 0; i<xmlNode.childNodes.length; i++ )<br />
{<br />
    slideShowImages.push( xmlNode.childNodes[ i ].attributes.src );<br />
}</p>
<p>var loopRange:LoopRange = new LoopRange( 0, slideShowImages.length-1 );</p>
<p>// to load the next image:<br />
myMovieClip.loadMovie( slideShowImages[ loopRange.next() ] );</p>
<p>[/as]</p>
<p>I&#8217;ll add the AS3 class soon, it&#8217;s barely any different</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.pixelbreaker.com/flash/looping-within-a-range/feed</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>RSS/Atom syndication made easy</title>
		<link>http://blog.pixelbreaker.com/flash/rssatom-syndication-made-easy</link>
		<comments>http://blog.pixelbreaker.com/flash/rssatom-syndication-made-easy#comments</comments>
		<pubDate>Mon, 16 Jul 2007 18:56:36 +0000</pubDate>
		<dc:creator>pixelbreaker</dc:creator>
				<category><![CDATA[ActionScript 3.0]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Tips]]></category>

		<guid isPermaLink="false">http://blog.pixelbreaker.com/uncategorized/rss-atom-syndication-made-easy/</guid>
		<description><![CDATA[A while ago adobe released their as3syndicationlib for easy access to RSS &#38; Atom feeds by converting either RSS 1.0/2.0 or Atom feeds into generic data accessible via the IFeed interface. These libraries also require adobe&#8217;s corelib. It literally took me 5 minutes to write a simple wrapper class to handle the loading of the [...]]]></description>
			<content:encoded><![CDATA[<p>A while ago adobe released their <a href="http://code.google.com/p/as3syndicationlib/" target="_blank">as3syndicationlib</a> for easy access to RSS &#38; Atom feeds by converting either RSS 1.0/2.0 or Atom feeds into generic data accessible via the <em>IFeed</em> interface. These libraries also require <a href="http://code.google.com/p/as3corelib/" target="_blank">adobe&#8217;s corelib</a>.</p>
<p>It literally took me 5 minutes to write a simple wrapper class to handle the loading of the feeds, and simply trace out the headlines of each item.<br />
<span id="more-46"></span></p>
<p>[as]<br />
package app.feeds<br />
{<br />
	import com.adobe.xml.syndication.generic.FeedFactory;<br />
	import com.adobe.xml.syndication.generic.IFeed;<br />
	import com.adobe.xml.syndication.generic.IItem;</p>
<p>	import flash.events.Event;<br />
	import flash.net.URLLoader;<br />
	import flash.net.URLRequest;</p>
<p>	public class FeedLoader<br />
	{<br />
		private var loader:URLLoader;</p>
<p>		public var feed:IFeed;</p>
<p>		public function FeedLoader( feedURL:String )<br />
		{<br />
			loader = new URLLoader();<br />
			loader.addEventListener( Event.COMPLETE, loadComplete );<br />
			loader.load( new URLRequest( feedURL ) );<br />
		}</p>
<p>		private function loadComplete( e:Event ):void<br />
		{<br />
			feed = FeedFactory.getFeedByXML( new XML( e.target.data ) );</p>
<p>			var i:uint;<br />
			for( i=0; i< feed.items.length; i++ )<br />
			{<br />
				trace( IItem( feed.items[ i ] ).title );<br />
			}<br />
		}<br />
	}<br />
}<br />
[/as]</p>
<p>To implement:<br />
[as]<br />
var feed1:FeedLoader = new FeedLoader( &#8216;http://www.guardian.co.uk/rss&#8217; );<br />
[/as]</p>
<p>If you&#8217;re using flash as I am, you may find that it spits the dummy when it tries to load mx.utils.StringUtil, you&#8217;ll need to add an ActionScript classpath pointing to the flex framework, which on my machine was in &#8220;/Applications/Adobe Flex Builder 3/sdks/2.0.1/frameworks/source&#8221;</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.pixelbreaker.com/flash/rssatom-syndication-made-easy/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>AS3.0 Abstracts</title>
		<link>http://blog.pixelbreaker.com/flash/as30-abstracts</link>
		<comments>http://blog.pixelbreaker.com/flash/as30-abstracts#comments</comments>
		<pubDate>Thu, 12 Jul 2007 22:47:14 +0000</pubDate>
		<dc:creator>pixelbreaker</dc:creator>
				<category><![CDATA[ActionScript 3.0]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Tips]]></category>

		<guid isPermaLink="false">http://blog.pixelbreaker.com/flash/as30-abstracts/</guid>
		<description><![CDATA[Another thing I needed was a strict abstract class in AS3, again, public only constructors, and the absence of the &#8220;abstract&#8221; keyword from ActionScript meant a workaround. [as] package { import flash.utils.getDefinitionByName; import flash.utils.getQualifiedClassName; public class AbstractExample { public function AbstractExample() { if( Class( getDefinitionByName( getQualifiedClassName( this ) ) ) == AbstractExample ) throw new [...]]]></description>
			<content:encoded><![CDATA[<p>Another thing I needed was a strict abstract class in AS3, again, public only constructors, and the absence of the &#8220;abstract&#8221; keyword from ActionScript meant a workaround.</p>
<p>[as]<br />
package<br />
{<br />
	import flash.utils.getDefinitionByName;<br />
	import flash.utils.getQualifiedClassName;</p>
<p>	public class AbstractExample<br />
	{<br />
		public function AbstractExample()<br />
		{<br />
			if( Class( getDefinitionByName( getQualifiedClassName( this ) ) ) == AbstractExample ) throw new Error( &#8216;AbstractExample must be extended&#8217; );</p>
<p>			// rest of constructor here&#8230;<br />
		}<br />
	}<br />
}<br />
[/as]</p>
<p>I like this solution best as it doesn&#8217;t rely on strings for comparison as in <a href="http://www.tink.ws/blog/stricter-abstracts/">Tink&#8217;s example</a>. His method is shorter, and easier for typing. It&#8217;s down to personal preference really.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.pixelbreaker.com/flash/as30-abstracts/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>AS3.0 Singletons</title>
		<link>http://blog.pixelbreaker.com/flash/as30-singletons</link>
		<comments>http://blog.pixelbreaker.com/flash/as30-singletons#comments</comments>
		<pubDate>Thu, 12 Jul 2007 22:35:47 +0000</pubDate>
		<dc:creator>pixelbreaker</dc:creator>
				<category><![CDATA[ActionScript 3.0]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Tips]]></category>

		<guid isPermaLink="false">http://blog.pixelbreaker.com/flash/as30-singletons/</guid>
		<description><![CDATA[While I&#8217;ve been getting up to speed with my AS3 over the past few months, there have been a few things which are really useful to know and took a bit of digging about to find. As class constructors in ActionScript 3.0 must be public, there&#8217;s not a really clean way to force a class [...]]]></description>
			<content:encoded><![CDATA[<p>While I&#8217;ve been getting up to speed with my AS3 over the past few months, there have been a few things which are really useful to know and took a bit of digging about to find.</p>
<p>As class constructors in ActionScript 3.0 must be public, there&#8217;s not a really clean way to force a class to be a singleton as there is in ActionScript 2.0 or Java say.</p>
<p>[as]<br />
package<br />
{<br />
	public class Singleton<br />
	{<br />
		public static var instance:Singleton;</p>
<p>		public static function getInstance():Singleton<br />
		{<br />
			if( instance == null ) instance = new Singleton( getInstance );<br />
			return instance;<br />
		}</p>
<p>		public function Singleton( caller:Function )<br />
		{<br />
			if( caller == Singleton.getInstance )<br />
			{<br />
				// instantiate class here<br />
			}<br />
			else<br />
			{<br />
				throw new Error( &#8216;Singleton is a singleton, use getInstance();&#8217; );<br />
			}<br />
		}<br />
	}<br />
}<br />
[/as]</p>
<p>It&#8217;s pretty simple, you can&#8217;t call <b>new Singleton()</b> because the compiler will be expecting an argument, and if you do pass an argument, it will throw an Error because the caller is not the Singleton.getInstance method.</p>
<p>So you can only access this class by calling <b>Singleton.getInstance()</b>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.pixelbreaker.com/flash/as30-singletons/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
