<?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>mostly a codeBlog &#187; Flash</title>
	<atom:link href="http://www.rickigregersen.com/category/flash/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.rickigregersen.com</link>
	<description>whatever I am currently programming... and I truly digress</description>
	<lastBuildDate>Mon, 21 Jun 2010 15:40:35 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>SporeTree</title>
		<link>http://www.rickigregersen.com/2009/08/11/sporetree/</link>
		<comments>http://www.rickigregersen.com/2009/08/11/sporetree/#comments</comments>
		<pubDate>Tue, 11 Aug 2009 21:21:02 +0000</pubDate>
		<dc:creator>Ricki</dc:creator>
				<category><![CDATA[ActionScript 3.0]]></category>
		<category><![CDATA[ActionScript3]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[algorithm]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[Bitmap]]></category>
		<category><![CDATA[fun]]></category>
		<category><![CDATA[recursive]]></category>
		<category><![CDATA[spore]]></category>
		<category><![CDATA[tree]]></category>

		<guid isPermaLink="false">http://www.rickigregersen.com/?p=193</guid>
		<description><![CDATA[This time the spores turned into a tree. It is by no means perfect, the branches does not move upwards with the stem as it should, were this a real orange colored scary spore tree. This was just some &#8220;while Im doing nothing, let&#8217;s put whatever in the &#8216;fun&#8217; category&#8221; but if anyone would like [...]]]></description>
			<content:encoded><![CDATA[<p>This time the <a href="http://www.rickigregersen.com/?paged=2">spores</a> turned into a tree. It is by no means perfect, the branches does not move upwards with the stem as it should, were this a real orange colored scary spore tree.</p>
<p>This was just some &#8220;while Im doing nothing, let&#8217;s put whatever in the &#8216;fun&#8217; category&#8221; <img src='http://www.rickigregersen.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  but if anyone would like the code, throw me a comment and I&#8217;ll send it off. I is uncommented and messy.</p>
<p><object width="512" height="750" data="http://www.rickigregersen.com/wp-content/uploads/BitmapSporeGrowingTree.swf" type="application/x-shockwave-flash"><param name="quality" value="high" /><param name="scale" value="exactfit" /><param name="salign" value="l" /><param name="wmode" value="transparent" /><param name="src" value="http://www.rickigregersen.com/wp-content/uploads/BitmapSporeGrowingTree.swf" /></object></h4>
]]></content:encoded>
			<wfw:commentRss>http://www.rickigregersen.com/2009/08/11/sporetree/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Growing Homegrown Tentacles</title>
		<link>http://www.rickigregersen.com/2009/08/08/growing-homegrown-tentacles/</link>
		<comments>http://www.rickigregersen.com/2009/08/08/growing-homegrown-tentacles/#comments</comments>
		<pubDate>Fri, 07 Aug 2009 23:30:19 +0000</pubDate>
		<dc:creator>Ricki</dc:creator>
				<category><![CDATA[ActionScript 3.0]]></category>
		<category><![CDATA[ActionScript3]]></category>
		<category><![CDATA[Flash]]></category>

		<guid isPermaLink="false">http://www.rickigregersen.com/?p=183</guid>
		<description><![CDATA[Well, I just got time to look into how to get the tentacles growing, i.e. start out small and have all the joints gradually increase in size, instead of the joint getting smaller all the time. (think I just tried to explain the definition of growing by using the definition of growing??). Thanks again to [...]]]></description>
			<content:encoded><![CDATA[<p>Well, I just got time to look into how to get the tentacles growing, i.e. start out small and have all the joints gradually increase in size, instead of the joint getting smaller all the time. (think I just tried to explain the definition of growing by using the definition of growing??).</p>
<p>Thanks again to <a href="http://blog.soulwire.co.uk/">Justin</a>.</p>
<p><object width="512" height="512" data="http://www.rickigregersen.com/wp-content/uploads/BitmapSporeGrowing.swf" type="application/x-shockwave-flash"><param name="quality" value="high" /><param name="scale" value="exactfit" /><param name="salign" value="l" /><param name="wmode" value="transparent" /><param name="src" value="http://www.rickigregersen.com/wp-content/uploads/BitmapSporeGrowing.swf" /></object></h4>
<p>The spore Class turned out to look like this, it needs going over, but I was thinking on porting it to Processing and really get my tentacles on <img src='http://www.rickigregersen.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>The code:</p>
<pre lang="actionscript">
package
{
	import flash.display.Graphics;
	import flash.display.Sprite;
	import flash.geom.Point;
	import flash.utils.setTimeout;

	public class Spore extends Sprite
	{
		private static const LENGTH:int = 90;
		private var _posX:Vector.<Number>;
		private var _posY:Vector.<Number>;
		private var angStep:Number;
		private var posStep:Number;
		private var curl:Number;
		private var n:Number = 0;
		private var a:Number = Math.random() * Math.PI * 2;
		private var _index:int;
		private var _g:Graphics;

		public function Spore()
		{
			_posX = new Vector.<Number>( LENGTH, true );
			_posY = new Vector.<Number>( LENGTH, true );
			_g = graphics;
			this.generate( range( 0.01, 0.05 ), range( 2.0, 3.0 ), range( 0.2, 1.0 ) );
		}

		public function generate( aStep:Number, pStep:Number, c:Number ):void
		{
			_index = 0;
			angStep = aStep;
			posStep = pStep;
			curl = c;
			var loc:Point = new Point( 0, 0 );
			while( _index < LENGTH )
			{
				n += Math.random() * (angStep - -angStep) + -angStep;
				n *= 0.9 + curl * 0.1;
				a += n;
				loc.x += Math.cos( a ) * posStep;
				loc.y += Math.sin( a ) * posStep;
				_posX[ _index ] = loc.x;
				_posY[ _index ] = loc.y;
				_index++;
			}
			_index = 0;
		}
		public function step():void
		{
			_g.clear();
			_g.lineStyle( 0, 0x000000, 0.7 );
			var c:int = 0;
			var radius:Number;
			while( c < _index )
			{
				radius = _index / c;
				if( radius > 10 ) radius = 10;
				_g.beginFill( 0xFF6600, 0.4 );
				_g.drawCircle( _posX[ c ], _posY[ c ], radius );
				_g.endFill();
				c++;
			}
			_index++;
			if( _index >= LENGTH )
			{
				this.generate( range( 0.01, 0.05 ), range( 2.0, 3.0 ), range( 0.2, 1.0 ) );
			}
		}
		private function range(min:Number, max:Number = NaN):Number
		{
			if(isNaN(max)) {max = min; min = 0}
				return min + (Math.random() * (max - min));
		}
	}
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.rickigregersen.com/2009/08/08/growing-homegrown-tentacles/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>RandomWalk</title>
		<link>http://www.rickigregersen.com/2009/08/06/randomwalk/</link>
		<comments>http://www.rickigregersen.com/2009/08/06/randomwalk/#comments</comments>
		<pubDate>Thu, 06 Aug 2009 18:32:55 +0000</pubDate>
		<dc:creator>Ricki</dc:creator>
				<category><![CDATA[ActionScript 3.0]]></category>
		<category><![CDATA[ActionScript3]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Particle]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[algorithm]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[flash builder 4]]></category>

		<guid isPermaLink="false">http://www.rickigregersen.com/?p=180</guid>
		<description><![CDATA[Im looking into some of the random walk stuff out there and I stumbled upon www.actionsnippet.com and his RandomWalk experiments, great site by the way, bring coffee. The thing that was cool about his way of doing it was that he constrains the &#8220;walkers&#8221; to only move: up, down, left and right. Absolutely no diagonal [...]]]></description>
			<content:encoded><![CDATA[<p>Im looking into some of the random walk stuff out there and I stumbled upon <a href="http://www.actionsnippet.com">www.actionsnippet.com</a> and his RandomWalk experiments, great site by the way, bring coffee.<br />
The thing that was cool about his way of doing it was that he constrains the &#8220;walkers&#8221; to only move: up, down, left and right. Absolutely no diagonal movement. This produces a strange grid looking pattern.</p>
<p><object width="512" height="512" data="http://www.rickigregersen.com/wp-content/uploads/RandomWalk.swf" type="application/x-shockwave-flash"><param name="quality" value="high" /><param name="scale" value="exactfit" /><param name="salign" value="l" /><param name="wmode" value="transparent" /><param name="src" value="http://www.rickigregersen.com/wp-content/uploads/RandomWalk.swf" /></object></h4>
<p>The code:</p>
<div class="codecolorer-container objc default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br />47<br />48<br />49<br />50<br />51<br />52<br />53<br />54<br />55<br />56<br />57<br />58<br />59<br />60<br />61<br />62<br />63<br />64<br />65<br />66<br />67<br />68<br />69<br />70<br />71<br />72<br />73<br />74<br />75<br />76<br />77<br />78<br />79<br />80<br />81<br />82<br />83<br />84<br />85<br />86<br />87<br />88<br />89<br />90<br />91<br />92<br />93<br />94<br />95<br />96<br />97<br />98<br />99<br />100<br /></div></td><td><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #11740a; font-style: italic;">/*&lt;pre lang=&quot;actionscript&quot;&gt;*/</span><br />
<span style="color: #11740a; font-style: italic;">//playing around with something Zevan Rosser from http://actionsnippet.com/?p=1236 did</span><br />
<span style="color: #11740a; font-style: italic;">//on his blog</span><br />
<br />
<span style="display:block;background-color:#ffff66">package <span style="color: #002200;">&#123;</span><br /></span>&nbsp; &nbsp; <br />
&nbsp; &nbsp; import flash.display.<span style="color: #002200;">*</span>;<br />
<span style="display:block;background-color:#ffff66">&nbsp; &nbsp; import flash.events.<span style="color: #002200;">*</span>;<br /></span>&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #002200;">&#91;</span>SWF<span style="color: #002200;">&#40;</span> width<span style="color: #002200;">=</span><span style="color: #bf1d1a;">&quot;512&quot;</span>, height <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">&quot;512&quot;</span>, backgroundColor <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">&quot;0x222222&quot;</span>, frameRate <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">&quot;120&quot;</span> <span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; public class RandomWalk extends Sprite <br />
<span style="display:block;background-color:#ffff66">&nbsp; &nbsp; <span style="color: #002200;">&#123;</span><br /></span>&nbsp; &nbsp; &nbsp; &nbsp; private var _canvas<span style="color: #002200;">:</span>BitmapData;<br />
&nbsp; &nbsp; &nbsp; &nbsp; private var _count<span style="color: #002200;">:</span><span style="color: #a61390;">int</span> <span style="color: #002200;">=</span> <span style="color: #2400d9;">100</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; private var _movers<span style="color: #002200;">:</span>Vector.&lt;Mover&gt;;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; public function RandomWalk<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #002200;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var w<span style="color: #002200;">:</span>Number <span style="color: #002200;">=</span> <span style="color: #2400d9;">512</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var h<span style="color: #002200;">:</span>Number <span style="color: #002200;">=</span> <span style="color: #2400d9;">512</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; scaleX<span style="color: #002200;">=</span>scaleY <span style="color: #002200;">=</span> <span style="color: #2400d9;">0.25</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _canvas <span style="color: #002200;">=</span> new BitmapData<span style="color: #002200;">&#40;</span> w <span style="color: #002200;">*</span> <span style="color: #2400d9;">4</span>, h <span style="color: #002200;">*</span> <span style="color: #2400d9;">4</span>, <span style="color: #a61390;">false</span>, 0x222222 <span style="color: #002200;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; addChild<span style="color: #002200;">&#40;</span> new Bitmap<span style="color: #002200;">&#40;</span> _canvas <span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _movers <span style="color: #002200;">=</span> new Vector.&lt;Mover&gt;<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a61390;">for</span> <span style="color: #002200;">&#40;</span> var i<span style="color: #002200;">:</span><span style="color: #a61390;">int</span> <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>; i &lt; _count; i<span style="color: #002200;">++</span> <span style="color: #002200;">&#41;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #002200;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _movers<span style="color: #002200;">&#91;</span> i <span style="color: #002200;">&#93;</span> <span style="color: #002200;">=</span> new Mover<span style="color: #002200;">&#40;</span> _canvas, w <span style="color: #002200;">*</span> <span style="color: #2400d9;">1.5</span> <span style="color: #002200;">+</span> Math.random<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">*</span> w, h <span style="color: #002200;">*</span> <span style="color: #2400d9;">1.5</span> <span style="color: #002200;">+</span> Math.random<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">*</span> h <span style="color: #002200;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #002200;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; addEventListener<span style="color: #002200;">&#40;</span> Event.ENTER_FRAME, onRun <span style="color: #002200;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #002200;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; private function onRun<span style="color: #002200;">&#40;</span> evt<span style="color: #002200;">:</span>Event <span style="color: #002200;">&#41;</span><span style="color: #002200;">:</span><span style="color: #a61390;">void</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #002200;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a61390;">for</span> <span style="color: #002200;">&#40;</span>var i<span style="color: #002200;">:</span><span style="color: #a61390;">int</span> <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>; i &lt; <span style="color: #2400d9;">200</span>; i<span style="color: #002200;">++</span><span style="color: #002200;">&#41;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #002200;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a61390;">for</span> <span style="color: #002200;">&#40;</span>var j<span style="color: #002200;">:</span><span style="color: #a61390;">int</span> <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>; j &lt; _count; j<span style="color: #002200;">++</span><span style="color: #002200;">&#41;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #002200;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _movers<span style="color: #002200;">&#91;</span> j <span style="color: #002200;">&#93;</span>.run<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #002200;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #002200;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #002200;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #002200;">&#125;</span><br />
<span style="color: #002200;">&#125;</span><br />
<br />
import flash.display.BitmapData;<br />
class Mover <br />
<span style="color: #002200;">&#123;</span><br />
&nbsp; &nbsp; public var x<span style="color: #002200;">:</span>Number;<br />
&nbsp; &nbsp; public var y<span style="color: #002200;">:</span>Number;<br />
&nbsp; &nbsp; public var velX<span style="color: #002200;">:</span>Number;<br />
&nbsp; &nbsp; public var velY<span style="color: #002200;">:</span>Number;<br />
&nbsp; &nbsp; public var speed<span style="color: #002200;">:</span>Number;<br />
&nbsp; &nbsp; private var _canvas<span style="color: #002200;">:</span>BitmapData;<br />
&nbsp; &nbsp; private var _color<span style="color: #002200;">:</span>uint;<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; public function Mover<span style="color: #002200;">&#40;</span> canvas<span style="color: #002200;">:</span>BitmapData, xp<span style="color: #002200;">:</span>Number, yp<span style="color: #002200;">:</span>Number <span style="color: #002200;">&#41;</span> <br />
&nbsp; &nbsp; <span style="color: #002200;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; _color <span style="color: #002200;">=</span> 0xFF6600;<span style="color: #11740a; font-style: italic;">//Math.random() * 0xFFFFFF;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; _canvas <span style="color: #002200;">=</span> canvas;<br />
&nbsp; &nbsp; &nbsp; &nbsp; x <span style="color: #002200;">=</span> xp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; y <span style="color: #002200;">=</span> yp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; velX <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; velY <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; speed<span style="color: #002200;">=</span>Math.random<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">*</span> <span style="color: #2400d9;">5</span> <span style="color: #002200;">-</span> <span style="color: #2400d9;">2.5</span>;<br />
&nbsp; &nbsp; <span style="color: #002200;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; public function run<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">:</span><span style="color: #a61390;">void</span> <br />
&nbsp; &nbsp; <span style="color: #002200;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; x <span style="color: #002200;">+=</span> velX;<br />
&nbsp; &nbsp; &nbsp; &nbsp; y <span style="color: #002200;">+=</span> velY;<br />
&nbsp; &nbsp; &nbsp; &nbsp; _canvas.setPixel<span style="color: #002200;">&#40;</span> x, y, _color <span style="color: #002200;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; var dir<span style="color: #002200;">:</span>Number <span style="color: #002200;">=</span> <span style="color: #a61390;">int</span><span style="color: #002200;">&#40;</span> Math.random<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">*</span> <span style="color: #2400d9;">4</span> <span style="color: #002200;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>dir <span style="color: #002200;">==</span> <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #002200;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; velX <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; velY <span style="color: #002200;">=-</span> speed;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #002200;">&#125;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a61390;">else</span> <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>dir <span style="color: #002200;">==</span> <span style="color: #2400d9;">1</span><span style="color: #002200;">&#41;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #002200;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; velX <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; velY <span style="color: #002200;">=</span> speed;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #002200;">&#125;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a61390;">else</span> <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>dir <span style="color: #002200;">==</span> <span style="color: #2400d9;">2</span><span style="color: #002200;">&#41;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #002200;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; velX <span style="color: #002200;">=-</span> speed;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; velY <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #002200;">&#125;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a61390;">else</span> <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>dir <span style="color: #002200;">==</span> <span style="color: #2400d9;">3</span><span style="color: #002200;">&#41;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #002200;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; velX <span style="color: #002200;">=</span> speed;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; velY <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #002200;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #002200;">&#125;</span><br />
<span style="color: #002200;">&#125;</span><br />
<br />
<span style="color: #11740a; font-style: italic;">/*&lt;/pre&gt;*/</span></div></td></tr></tbody></table></div>
]]></content:encoded>
			<wfw:commentRss>http://www.rickigregersen.com/2009/08/06/randomwalk/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Homegrown Tentacles</title>
		<link>http://www.rickigregersen.com/2009/08/06/homegrown-tentacles/</link>
		<comments>http://www.rickigregersen.com/2009/08/06/homegrown-tentacles/#comments</comments>
		<pubDate>Thu, 06 Aug 2009 18:03:40 +0000</pubDate>
		<dc:creator>Ricki</dc:creator>
				<category><![CDATA[ActionScript 3.0]]></category>
		<category><![CDATA[ActionScript3]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Particle]]></category>
		<category><![CDATA[algorithm]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[Bitmap]]></category>
		<category><![CDATA[Filters]]></category>
		<category><![CDATA[flash builder 4]]></category>
		<category><![CDATA[fun]]></category>

		<guid isPermaLink="false">http://www.rickigregersen.com/?p=173</guid>
		<description><![CDATA[I had been scurrying about over on the Soulwire blog, again, and looking at the tentacles. I did some experiments of my own, but the tentacles never came out right, not organic enough. I wrote Justin and asked him how he did it and he was really cool about helping out with a code example [...]]]></description>
			<content:encoded><![CDATA[<p>I had been scurrying about over on the Soulwire blog, again, and looking at the tentacles. I did some experiments of<br />
my own, but the tentacles never came out right, not organic enough. I wrote Justin and asked him how he did it and he was really cool about helping out with a code example of how he did that part.</p>
<p>The Soulwire Tentacles was an experiment in PixelBender speed and usability, so he makes it look as if the tentacles are really growing, i.e. starts out small and each &#8220;joint&#8221; grows as the tentacles grows. Mine is just a look into the random-organic way they grow. Also I was trying to see what kinds of speed I would get by using the Bitmap approach.</p>
<p>Here are the <a href="http://blog.soulwire.co.uk/flash/actionscript-3/pixel-bender-growing-tentacles/">Soulwire Tentacles</a></p>
<p>And here are my Tentacles&#8230; that came out wrong&#8230; <img src='http://www.rickigregersen.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p><object width="512" height="512" data="http://www.rickigregersen.com/wp-content/uploads/BitmapSpore.swf" type="application/x-shockwave-flash"><param name="quality" value="high" /><param name="scale" value="exactfit" /><param name="salign" value="l" /><param name="wmode" value="transparent" /><param name="src" value="http://www.rickigregersen.com/wp-content/uploads/BitmapSpore.swf" /></object></h4>
<p>The code:</p>
<pre lang="actionscript">
/**
*
 * @BitmapSpore.as	:	inspired by http://blog.soulwire.co.uk/flash/actionscript-3/pixel-bender-growing-tentacles/
 * @Date  08/06-09
 * @Ricki Gregersen
*/

package
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.filters.BlurFilter;
	import flash.geom.ColorTransform;
	import flash.geom.Point;

	[SWF( width="512", height = "512", backgroundColor = "0x222222", frameRate = "30" )]

	public class BitmapSpore extends Sprite
	{
		private static var fading : ColorTransform = new ColorTransform( 1, 1, 1, 0.99999 );
		private static var _blur : BlurFilter = new BlurFilter( 1, 1, 2 );

		private var _bmd:BitmapData;
		private var _bmp:Bitmap;
		private var _container:Sprite;

		public function BitmapSpore()
		{
			_bmd = new BitmapData( 512, 512, true, 0x222222 );
			_bmp = new Bitmap( _bmd );
			addChild( _bmp );
			var s:Spore;
			addChild( _container = new Sprite() );
			for( var i:int = 0; i < 500; i++ )
			{
				s = new Spore( 256, 256, range( 0.01, 0.05 ), range( 2.0, 3.0 ), range( 0.2, 1.0 ));
				s.scaleX = s.scaleY = Math.random() * 2;
				_container.addChild( s );
			}
			addEventListener( Event.ENTER_FRAME, update );
		}

		private function update( event:Event ):void
		{
			var n:int = _container.numChildren;
			var s:Spore;

			for( var i:int = 0; i < n; i++ )
			{
				s = Spore( _container.getChildAt( i ));
				s.move();
				s.x = s.loc.x;
				s.y = s.loc.y;
			}
			_bmp.filters = [  _blur ];
			_bmd.colorTransform( _bmd.rect, fading );
			_bmd.draw( _container );
		}

		private function range(min:Number, max:Number = NaN):Number
		{
			if(isNaN(max)) {max = min; min = 0}
				return min + (Math.random() * (max - min));
		} 

	}
}
	import flash.geom.Point;
	import flash.display.Shape;

	class Spore extends Shape
	{
		public var loc:Point;

		private var angStep:Number;
		private var posStep:Number;
		private var curl:Number;
		private var n:Number = 0;
		private var a:Number = Math.random() * Math.PI * 2;

		function Spore( xPos:Number, yPos:Number, aStep:Number, pStep:Number, c:Number )
		{
			loc = new Point( xPos, yPos );
			angStep = aStep;
			posStep = pStep;
			curl = c;
			graphics.lineStyle( 0, 0x222222, 0.7 );
			graphics.beginFill( 0xFF6600, 0.7 );
			graphics.drawCircle( 0, 0, 5 );
			graphics.endFill();
		}
		public function move():void
		{
			n += Math.random() * (angStep - -angStep) + -angStep;
			n *= 0.9 + curl * 0.1;
			a += n;
			loc.x += Math.cos( a ) * posStep;
			loc.y += Math.sin( a ) * posStep;
			this.scaleX = this.scaleY *= 0.98;
			var s:Number = this.scaleX = this.scaleY;
			if( s < 0.1 )
			{
				loc.x = loc.y = 256;
				this.scaleX = this.scaleY = Math.random() * 2;
			}
		}
	}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.rickigregersen.com/2009/08/06/homegrown-tentacles/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Perlin Noise used to drive vector flow field</title>
		<link>http://www.rickigregersen.com/2009/06/20/perlin-noise-used-to-drive-vector-flow-field/</link>
		<comments>http://www.rickigregersen.com/2009/06/20/perlin-noise-used-to-drive-vector-flow-field/#comments</comments>
		<pubDate>Sat, 20 Jun 2009 12:07:12 +0000</pubDate>
		<dc:creator>Ricki</dc:creator>
				<category><![CDATA[ActionScript 3.0]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[flash builder 4]]></category>
		<category><![CDATA[flow field]]></category>
		<category><![CDATA[noise]]></category>
		<category><![CDATA[Perlin]]></category>
		<category><![CDATA[vectors]]></category>

		<guid isPermaLink="false">http://www.rickigregersen.com/?p=166</guid>
		<description><![CDATA[This post is essentially the same as <a href="http://www.rickigregersen.com/?p=159#content">this one</a>, only difference is that a BitmapData.perlinNoise() is used instead of the BitmapData.draw(video) to produce a background that influences the flow field.]]></description>
			<content:encoded><![CDATA[<p>This post is essentially the same as <a href="http://www.rickigregersen.com/?p=159#content">this one</a>, only difference is that a BitmapData.perlinNoise() is used instead of the BitmapData.draw(video) to produce a background that influences the flow field.</p>
<p>I have not posted the code to this one because the two are so similar, but if anyone want me to, just leave a comment and I&#8217;ll get right on posting it:) or send it to you.</p>
<p><object width="490" height="550" data="http://www.rickigregersen.com/wp-content/uploads/VectorFlowField02.swf" type="application/x-shockwave-flash"><param name="quality" value="medium" /><param name="scale" value="exactfit" /><param name="salign" value="l" /><param name="wmode" value="transparent" /><param name="src" value="http://www.rickigregersen.com/wp-content/uploads/VectorFlowField02.swf" /></object></h4>
]]></content:encoded>
			<wfw:commentRss>http://www.rickigregersen.com/2009/06/20/perlin-noise-used-to-drive-vector-flow-field/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>More camera flow field fun</title>
		<link>http://www.rickigregersen.com/2009/06/16/more-camera-flow-field-fun/</link>
		<comments>http://www.rickigregersen.com/2009/06/16/more-camera-flow-field-fun/#comments</comments>
		<pubDate>Tue, 16 Jun 2009 20:10:06 +0000</pubDate>
		<dc:creator>Ricki</dc:creator>
				<category><![CDATA[ActionScript 3.0]]></category>
		<category><![CDATA[ActionScript3]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[flow field]]></category>
		<category><![CDATA[vectors]]></category>
		<category><![CDATA[web cam]]></category>

		<guid isPermaLink="false">http://www.rickigregersen.com/?p=159</guid>
		<description><![CDATA[Apparently I have not finished my little Flow Field craze, In one of my math books I saw a picture of a flow field as it is depicted in math terms, as a little grid of arrows. I thought it could be fun to play around with this idea. The code is really sloppy this [...]]]></description>
			<content:encoded><![CDATA[<p>Apparently I have not finished my little Flow Field craze, In one of my math books I saw a picture of a flow field<br />
as it is depicted in math terms, as a little grid of arrows. I thought it could be fun to play around with this idea.</p>
<p>The code is really sloppy this time, only took an hour or two, so I&#8217;ll post the example here along with the project (A Flash Builder Flash Project SDK 4.0 to be precise, will run in the Flash IDE, no problem).</p>
<h3>This is your brain, and this is your brain on vectors!</h3>
<h4>(Warning, this could potentially push your browser to the limit if you are to eager with the sliders. So please save any unfinished pulitzer price article you are currently working on in Google docs, I really don&#8217;t want to piss of anyone <img src='http://www.rickigregersen.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  )<br />
<object width="490" height="550" data="http://www.rickigregersen.com/wp-content/uploads/VectorFlowField.swf" type="application/x-shockwave-flash"><param name="quality" value="medium" /><param name="scale" value="exactfit" /><param name="salign" value="l" /><param name="wmode" value="transparent" /><param name="src" value="http://www.rickigregersen.com/wp-content/uploads/VectorFlowField.swf" /></object></h4>
<p><a href="http://rickigregersen.com/wp-content/uploads/flowfieldAndMinimalComps.zip">Source is here, I included Bit-101&#8242;s minimalComps sine Im using that library, they are great for these things!</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.rickigregersen.com/2009/06/16/more-camera-flow-field-fun/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ant build scripts part 01 (Gumbo Flex SDK 4.0.0)</title>
		<link>http://www.rickigregersen.com/2009/06/14/ant-build-scripts-part-01-gumbo-flex-sdk-400/</link>
		<comments>http://www.rickigregersen.com/2009/06/14/ant-build-scripts-part-01-gumbo-flex-sdk-400/#comments</comments>
		<pubDate>Sun, 14 Jun 2009 20:16:47 +0000</pubDate>
		<dc:creator>Ricki</dc:creator>
				<category><![CDATA[Flash]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Productivity]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[adt.jar]]></category>
		<category><![CDATA[ant]]></category>
		<category><![CDATA[flash builder 4]]></category>
		<category><![CDATA[gumbo]]></category>
		<category><![CDATA[macro]]></category>
		<category><![CDATA[mxmlc]]></category>
		<category><![CDATA[sdk]]></category>

		<guid isPermaLink="false">http://www.rickigregersen.com/?p=137</guid>
		<description><![CDATA[(Warning: lengthy and boring&#8230; and subsequently extremely useful) I have been in the process of building an Ant script for the main project/customer at work. It is an Air project that consists of 9 applications, each in their own project space. They get compiled into modules, they have their own external assets and so forth. [...]]]></description>
			<content:encoded><![CDATA[<h3>(Warning: lengthy and boring&#8230; and subsequently extremely useful)</h3>
<p>I have been in the process of building an Ant script for the main project/customer at work.<br />
It is an Air project that consists of 9 applications, each in their own project space. They get compiled into modules, they have<br />
their own external assets and so forth. When we need to update/release these applications all the swf&#8217;s and assets are moved to a final package, which<br />
essentially is a MainApp.swf that loads in these 9 applications at runtime.</p>
<p>Because we needed a way to test and develop the individual modules, without relying on the Main.swf, or any other modules to be present, each project had<br />
a dummy Main, so checking out SubApp0 from SVN and running DummyMain.mxml inside it would result in the SubApp0.swf module being loaded. Modules can&#8217;t run by themselves.</p>
<p>There are two deployment cases, one where the customer needs an entire new MainApp.air file installed and a case where we only need an individual module recompiled and sent<br />
to the customers installation. e.g. SubApp1 has been updated and the SubApp1.swf module file is to be moved into the MainApps application directory, so that MainApp can load it. (This is done via a completely different system that tests for files on our server with newer timestamps and moves the files out to the customers clients).</p>
<p>It was becoming a bit of a burden keeping track of 9 modules/projects and their assets, so 6 months back I wrote an applescript that moved the assets and the modules<br />
into the main application, where I could ask Flex Builder to package them into an Air bundle. This however does not help my Windows colleagues, do any testing on the bundle or keep any track on versioning.<br />
So now it is time to get out the magnifying glass and burn some Ant. I don&#8217;t know how many parts this will be (2-4) but I am hoping on getting it up and running with both automatic Unit testing, automatic retrieval of latest code from SVN and some sort of reporting system(mail, ftp, red blinking lights on my phone?)&#8230; oh yeah and the dreaded asdoc.</p>
<p>I&#8217;ll be referring to the two cases mentioned above as the &#8220;deploy case&#8221;" i.e. only compile and collect, and the &#8220;release case&#8221; i.e. compile, collect, version and package as an Air bundle.</p>
<p>The first part is how to set up Gumbo/Flash Builder 4 (Flex builder/Eclipse has the same procedure) for Ant tasks, the structure of these tasks, the different files and lastly a small example that compiles three swf&#8217;s and their SWC&#8217;s using a macro and finally move these to a directory called &#8220;deploy&#8221;.</p>
<p>The second part will then look at a conceptually similar case as the one I described in the introduction, MainApp, SubApp0 and SubApp1 are all to be compiled, have their assets moved and everything bundled into an Air package.</p>
<p>I will see how far I get over the next couple of weeks, if I get SVN and Unit testing up and running I will post that as well.</p>
<blockquote><p>On with it!</p></blockquote>
<h2><strong>Prerequisite</strong></h2>
<p>Your Eclipse/Gumbo/Flash Builder/Flex Builder, from now on just called &#8220;Eclipse&#8221;, will need the the &#8220;Java Development Tools&#8221; installed. This can be found under &#8220;help&#8221; -&gt;&#8221;Software updates&#8221; -&gt; &#8220;Find and install&#8221;. The &#8220;Java Development Tools&#8221; should be on the list.</p>
<p>An Ant build script consists of, at least, a build.xml file. The build file holds a set of instructions wrapped in targets, it is in essences an xml file with<br />
commands and parameters to be run by the ant.jar &#8220;engine&#8221;.</p>
<p>For us to use Ant to compile flash files, Adobe must deliver command line tools we can point Ant at so it can compile and manipulate the raw classes, i.e. .as and .mxlm files. Luckily Adobe has made these for us:</p>
<ul>
<li>mxmlc.jar is the mxml compiler.</li>
<li>adt.jar is the air debug tool</li>
<li>adl is the air debug launcher (adl.exe on Windows)</li>
<li>asdoc is the actionscript documenter (asdoc.exe on Windows)</li>
</ul>
<p>There is some information on these command line tools at  <a href="http://help.adobe.com/en_US/AIR/1.5/devappshtml/WS5b3ccc516d4fbf351e63e3d118666ade46-7f73.html">Adobes livedocs</a> and the tools themselves you already have if you have an Flex/Flash Builder installation.<br />
They reside in the &#8220;sdks&#8221; folder, which is located somewhere like ../users/applications/Adobe Flash Builder/sdks/4.0.0/<br />
If you don&#8217;t have anything named like this anywhere, you maybe running Eclipse and haven&#8217;t installed the Flex builder plugin yet, this can be done from the Adobe downloads section.</p>
<p>Once we are ready we are going to start a new Air project called MainApp, so go into Eclipse, &#8220;File&#8221; -&gt; &#8220;New Flex Project&#8221; -&gt; make sure it is set to be an Air project and click &#8220;Finish&#8221;.<br />
Right click on the project in the navigator view (package view in Gumbo) select new and click &#8220;folder&#8221;, name the folder &#8220;build-files&#8221;, right click that new &#8220;build-files&#8221; folder and select new and click &#8220;file&#8221;, name the file &#8220;build.xml&#8221;. We are almost ready now.</p>
<p>We are going to copy the &#8220;mxmlc.jar&#8221; and &#8220;flexTasks.jar&#8221; file into our project. This is done so the project sdk version always fit the current project, it is also done<br />
to avoid referencing long different paths on e.g. Mac, Linux and Windows. <del datetime="2009-06-14T21:02:48+00:00">The &#8220;adt.jar&#8221; and &#8220;adl&#8221; files are not copied as these rely on other tools located in the<br />
sdk and moving these will make them behave irregular.</del><br />
So before we move on find these files, on Mac, in:</p>
<p>/Applications/Adobe\ Flash\ Builder\ Beta/sdks/4.0.0/ant/lib/flexTasks.jar<br />
/Applications/Adobe\ Flash\ Builder\ Beta/sdks/4.0.0/lib/mxmlc.jar</p>
<p>And copy them into the project we made in the &#8220;libs&#8221; folder.</p>
<p>Now we are ready and your project should look something like this<br />
<img src="http://www.rickigregersen.com/wp-content/uploads/navigator.png" alt="The navigator in flex (package explorer in Gumbo)" /></p>
<h2>Let&#8217;s try out Ant</h2>
<p>Open the &#8220;build.xml&#8221; file and type in these instructions:</p>
<p><xml></p>
<pre lang="XML" colla="+"><?xml version="1.0" encoding="ISO-8859-1"?>
<project name="MainApp" basedir=".">
<property name="MAIN" location="/Users/rickigregersen/Documents/Programming/FlashBuilder"/>

	<taskdef resource="flexTasks.tasks" classpath="${MAIN}/MainApp/libs/flexTasks.jar"/>

	<target name="clean" description="clean the project">
		<delete dir="${MAIN}/MainApp/deploy"/>
		<mkdir dir="${MAIN}/MainApp/deploy"/>
	</target>
</project></pre>
<p></xml></p>
<p>Notice the path property &#8220;MAIN&#8221; this should of course be changed to fit the path on your system. Select &#8220;Window&#8221; -&gt; &#8220;Other views&#8221; -&gt; &#8220;Ant&#8221; in Eclipse to open the Ant view. Drag the &#8220;build.xml&#8221; onto this window, if it is not already there, right click on the &#8220;clean&#8221; target and select &#8220;Run As&#8221; -&gt; &#8220;Ant Build&#8221;.  Now you should see that Ant has made a &#8220;deploy&#8221; folder in your &#8220;MainApp&#8221; project root.  Ok things to notice here: the &#8220;target&#8221; node holds the tasks we want to carry out, the &#8220;property&#8221; is a way to set an alias that will be used instead of a long path. In the amount of blog posts and manual I have been through the last couple of days it seems there are two conventions when typing these out. One is &#8220;ALL_CAPS&#8221; and the other is &#8220;source.dir&#8221;. I like using the ALL_CAPS, like constants in programming, for the paths.<br />
You can type them as you like, no problem. Notice that in the target, we reference a property name using ${MAIN}, we will utilize this a lot more, not just to make smaller build scripts, but also to make it possible to have this script running on other developers computers, who probably don&#8217;t have folders and sdks in the same location.  Now right click the &#8220;build-files&#8221; folder and select &#8220;New&#8221; -&gt; &#8220;File&#8221; name the file &#8220;build.properties&#8221; this file will hold all the aliases that we deem necessary.</p>
<pre lang="XML">	project.name=MainApp
	author=Ricki Gregersen
	project.version=0.1.0

	#System depend paths
	FLEX_ROOT=/Users/rickigregersen/Documents/Programming/FlashBuilder
	FLEX_HOME=/Applications/Adobe Flash Builder Beta/sdks/4.0.0
	AIR_HOME = /Applications/Adobe Flash Builder Beta/sdks/4.0.0/frameworks/libs/air</pre>
<p>Now change the &#8220;build.xml&#8221; file to look like this:</p>
<p><xml></p>
<pre lang="XML" colla="+"><?xml version="1.0" encoding="ISO-8859-1"?>
<project name="MainApp" basedir=".">
<property file="build.properties"/>
<property name="MAIN" location="${FLEX_ROOT}/MainApp"/>

		<taskdef resource="flexTasks.tasks" classpath="${MAIN}/libs/flexTasks.jar"/>

		<target name="clean" description="clean the project">
			<delete dir="${MAIN}/deploy"/>
			<mkdir dir="${MAIN}/deploy"/>
		</target>
</project>
</pre>
<p></xml></p>
<p>Notice the &#8220;build.properties&#8221; is included as a property file. So why not just put the ${MAIN} alias into the &#8220;build.properties&#8221; as well? We surely could, but the ${MAIN} alias is only valid for this particular &#8220;build.xml&#8221; and not a general path. So we leave it in here for a developer to see. The goal is for a developer to pull down this project from, say, subversion, edit the three aliases in the &#8220;build.properties&#8221; and then be able to build everything of the bat.  But we are not really doing anything interesting yet, Ant can make directories, delete directories, move files, replaces files and so on. Lets compile our MainApp. This time I made quite a few add-ons to the script, but I&#8217;ll explain them all:<br />
<xml></p>
<pre lang="XML" colla="+"><?xml version="1.0" encoding="ISO-8859-1"?>
<project name="MainApp" basedir=".">
<property file="build.properties"/>
<property name="MAIN" location="${FLEX_ROOT}/MainApp"/>
<property name="FLEX_TASK" location="${MAIN}/libs/flexTasks.jar"/>
<property name="MAIN_APP_MXML" location="${MAIN}/src/MainApp.mxml"/>
<property name="DEPLOY" location="${MAIN}/deploy"/>

		<taskdef resource="flexTasks.tasks" classpath="${FLEX_TASK}"/>

		<!-- =================================
	          target: clean
	         ================================= -->
		<target name="clean" description="clean the project">
			<delete dir="${DEPLOY}"/>
			<mkdir dir="${DEPLOY}"/>
		</target>
		<!-- =================================
	          target: compile
	         ================================= -->
	    <target name="compile" depends="clean" description="compile a mxml file into a swf file">
			<mxmlc file="${MAIN_APP_MXML}" output="${DEPLOY}/MainApp.swf" debug="false" optimize="true">
		<!--	<compiler.library-path dir="${SWC1}/bin" append="true">
					<include name="*.swc"/>
					<exclude name="this_one_we_dont_need.swc"/>
				</compiler.library-path>
				<compiler.library-path dir="${SWC2}/bin" append="true">
					<include name="external_library.swc"/>
				</compiler.library-path>			-->
			</mxmlc>
	    </target>
	</project>
</pre>
<p></xml><br />
Ok this just got interesting, a few new aliases turned up, we covered how to do that already. A new target was included, yes this it how it goes.<br />
You can just add as many targets as you need inside the project node, notice that this new target &#8220;compile&#8221; also turned up in the Ant window in eclipse.<br />
You can now click either &#8220;clean&#8221; or &#8220;compile&#8221; and have that target run. The &#8220;compile&#8221; target has a new attribute called &#8220;depends&#8221;. This means exactly what it says: this task depends on the clean task being successfully run before it starts! This is great, we can now determine what tasks should run and in what order. Selecting &#8220;compile&#8221; in the Ant window and running it will make &#8220;clean&#8221; execute and when it is done, &#8220;compile&#8221; will run.<br />
This new compile target has an  tag. If you have ever used a terminal, this is the xml equivalent. The &#8220;grep&#8221; command in Unix has this description<br />
grep [options] PATTERN [FILE...] which means, type grep -any_options a_string in_a_file.txt, so when you give the terminal this command it understands how to search for the &#8220;a_string&#8221; in the &#8220;in_a_file.txt&#8221; with &#8220;-any_options&#8221; in mind. This is the same thing, the mxmlc.jar file is executed, with the attributes as parameters. This makes sense, it needs to know the location of the MAIN_APP_MXML file, which holds our code, it needs to know where to put it and the &#8220;debug&#8221; and &#8220;optimize&#8221; parameters are the options. You should now have a &#8220;MainApp.swf&#8221; in your &#8220;deploy&#8221; folder.<br />
I have included some code in the mxmlc tag, this is to show how external SWC files can be compiled into the swf. When the mxmlc runs the MainApp.mxml will raise a flag saying that a swc is needed, if you have a swc included in the library path og MainApp of course, this takes care of that.</p>
<p>Now it makes sense to have the &#8220;clean&#8221; target as we would like to ensure that only freshly compiled code ends up in the &#8220;deploy&#8221; folder.<br />
The last thing we will look at in this walk-through is to write a macro to bundle functionality together, compile and move two additional mxml files into the deploy folder.</p>
<p>A macro is a way of having Ant carry out a number of tasks in sequence. Before we get into to macroes, make two new projects called &#8220;SubApp0&#8243; and &#8220;SubApp1&#8243;, just like we made the &#8220;MainApp&#8221; project, like in the picture above. These projects will have nothing in their &#8220;libs&#8221; folder, they will not have &#8220;deploy&#8221; folder or any of the functionality/assets we added to the &#8220;MainApp&#8221;. I also made 2 new Library Projects called &#8220;AntSwc0&#8243; and &#8220;AntSwc1&#8243;, I then right clicked &#8220;MainApp&#8221;, &#8220;SubApp0&#8243; and &#8220;SubApp1&#8243; and selected &#8220;Properties&#8221; -&gt; &#8220;Flex Build Path&#8221; -&gt; &#8220;Library Path&#8221; and include the &#8220;AntSwc0&#8243; and &#8220;AntSwc1&#8243;. This is just to show this functionality, if you have no need for swcs, just omit compiler.library-path tag from the macro.</p>
<p>This is how the &#8220;build.xml&#8221; looks like now:</p>
<p><xml></p>
<pre lang="XML" colla="+"><?xml version="1.0" encoding="ISO-8859-1"?>
<project name="MainApp" basedir=".">
<property file="build.properties"/>
<property name="MAIN" location="${FLEX_ROOT}/MainApp"/>
<property name="FLEX_TASK" location="${MAIN}/libs/flexTasks.jar"/>
<property name="MAIN_APP_MXML" location="${MAIN}/src/MainApp.mxml"/>
<property name="MAIN_APP_SWF" location="MainApp.swf"/>
<property name="DEPLOY" location="${MAIN}/deploy"/>
<property name="SUB_APP0" location="${FLEX_ROOT}/SubApp0/src/SubApp0.mxml"/>
<property name="SUB_APP1" location="${FLEX_ROOT}/SubApp1/src/SubApp1.mxml"/>
<property name="SWC0" location="${FLEX_ROOT}/AntSwc0/bin"/>
<property name="SWC1" location="${FLEX_ROOT}/AntSwc1/bin"/>

	<taskdef resource="flexTasks.tasks" classpath="${FLEX_TASK}"/>
	<!-- = = = = = = = = = = = = = = = = =
          macrodef: compile
         = = = = = = = = = = = = = = = = = -->
    <macrodef name="compile">
    	<attribute name="source" />
    	<attribute name="target" default="DEPLOY" />
        <sequential>
			<mxmlc file="@{source}" output="@{target}" debug="false" optimize="true">
				<compiler.library-path dir="${SWC0}" append="true">
					<include name="*.swc"/>
				</compiler.library-path>
				<compiler.library-path dir="${SWC1}" append="true">
					<include name="*.swc"/>
				</compiler.library-path>
			</mxmlc>
        </sequential>
    </macrodef>
	<!-- =================================
          target: clean
         ================================= -->
	<target name="clean" description="clean the project">
		<delete dir="${DEPLOY}"/>
		<mkdir dir="${DEPLOY}"/>
	</target>
	<!-- =================================
          target: compile
         ================================= -->
    <target name="compile" depends="clean" description="compile a mxml file into a swf file">
    	<compile source="${SUB_APP0}" target="${DEPLOY}/SubApp0.swf"/>
    	<compile source="${SUB_APP1}" target="${DEPLOY}/SubApp1.swf"/>
    	<compile source="${MAIN_APP_MXML}" target="${DEPLOY}/MainApp.swf"/>
    </target>
	<!-- =================================
          target: run_all
         ================================= -->
    <target name="run_all" depends="compile, clean" description="will run all targets in the depends list"/>
</project>
</pre>
<p></xml></p>
<p>The macro takes the parameters you specify, here it is &#8220;source&#8221; and &#8220;target&#8221;, notice that in the  tag, these properties are<br />
accessed using @ instead of $, this is so the macro can distinguish between global aliases and local aliases. Everything you put between<br />
the  will be run by the macro.</p>
<p>I added a last target called &#8220;run_all&#8221;, you can see it is empty but the clever thing is that it depends on &#8220;clean&#8221; and &#8220;compile&#8221; to run before it will<br />
execute, so this essentially runs all.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.rickigregersen.com/2009/06/14/ant-build-scripts-part-01-gumbo-flex-sdk-400/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Evil &amp; Loud Perlin noise</title>
		<link>http://www.rickigregersen.com/2009/05/28/evil-loud-perlin-noise/</link>
		<comments>http://www.rickigregersen.com/2009/05/28/evil-loud-perlin-noise/#comments</comments>
		<pubDate>Thu, 28 May 2009 18:42:54 +0000</pubDate>
		<dc:creator>Ricki</dc:creator>
				<category><![CDATA[ActionScript 3.0]]></category>
		<category><![CDATA[ActionScript3]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[application]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[noise]]></category>
		<category><![CDATA[Perlin]]></category>

		<guid isPermaLink="false">http://www.rickigregersen.com/?p=122</guid>
		<description><![CDATA[In regards to my previous Perlin Flowfield post, it was a bit strange because I could simply not remember the basics about Perlin noise. Yes, as my colleague I falsely believed Perlin to be the evil brother of Merlin. I have previously used Perlin noise in Processing, it is a bit different in some of [...]]]></description>
			<content:encoded><![CDATA[<p>In regards to my previous <a href="http://www.rickigregersen.com/?p=93#content">Perlin Flowfield</a> post, it was a bit strange because I could simply not remember the basics about Perlin noise. Yes, as my colleague I falsely believed Perlin to be the evil brother of Merlin.</p>
<p>I have previously used Perlin noise in Processing, it is a bit different in some of the aspects of how you &#8220;access&#8221; it. In Processing you sort of get the values representing the noise iteration, whereas In Flash you actually build a noise Bitmap and read pixels from this to produce numerical noise. I Processing I guess you would do the opposite, i.e. building all the noise values into a bitmap to get something similar to the noise below.</p>
<p><object width="530" height="680" data="http://www.rickigregersen.com/wp-content/uploads/PerlinNoiseAdjust.swf" type="application/x-shockwave-flash"><param name="quality" value="medium" /><param name="scale" value="exactfit" /><param name="salign" value="l" /><param name="wmode" value="transparent" /><param name="src" value="http://www.rickigregersen.com/wp-content/uploads/PerlinNoiseAdjust.swf" /></object></p>
<p>So I build this little app to play around with the parameters and to see how much detail one can get away with, before the calculation load gets to heavy for practical use. The values that can be adjusted below are those that go into the <a href="http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/display/BitmapData.html#perlinNoise()">BitmapData.perlinNoise method</a> If you would like to put in more controls or whatever you feel like doing with this small app then: <a href="http://www.rickigregersen.com/wp-content/uploads/PerlinAdjuster.zip">Snatch the entire Flex project here (SDK 3.3)</a>, swf included.</p>
<p>I recalled that when dabbling with Perlin noise, a year ago, in Processing I found <a href="http://www.noisemachine.com/talk1/">Ken Perlin&#8217;s talk on&#8230; well, noise I guess <img src='http://www.rickigregersen.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </a> It is very interesting and even though words like <em>permutations</em> and <em>fractals</em> are used to some extend it is mainly an interesting story about this invention. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.rickigregersen.com/2009/05/28/evil-loud-perlin-noise/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Webcam Flow field in Flash</title>
		<link>http://www.rickigregersen.com/2009/05/25/webcam-flow-field-in-flash/</link>
		<comments>http://www.rickigregersen.com/2009/05/25/webcam-flow-field-in-flash/#comments</comments>
		<pubDate>Mon, 25 May 2009 21:02:48 +0000</pubDate>
		<dc:creator>Ricki</dc:creator>
				<category><![CDATA[ActionScript 3.0]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[Particle]]></category>

		<guid isPermaLink="false">http://www.rickigregersen.com/?p=93</guid>
		<description><![CDATA[I was lurking about over at Justin Windle&#8217;s blog Soulwire, wish my blog had an alter ego with a cool name.. He had been building some flow fields using Perlin noise, which my colleague think is Merlins evil brother for some reason. I was writing a vector library for a particle engine Im going to publish here [...]]]></description>
			<content:encoded><![CDATA[<p>I was lurking about over at Justin Windle&#8217;s blog <a href="http://blog.soulwire.co.uk/flash/actionscript-3/perlin-noise-flow-field/">Soulwire</a>, wish my blog had an alter ego with a cool name..</p>
<p><div id="attachment_96" class="wp-caption alignleft" style="width: 540px"><img class="size-medium wp-image-96" title="flowfield" src="http://www.rickigregersen.com/wp-content/uploads/flowfiledorange.png" alt="It was a bit to dark, the background is disturbing the image and so on.." width="530" height="400" /><p class="wp-caption-text">Now I know how Neo felt..</p></div><br />
<div id="attachment_96" class="wp-caption alignleft" style="width: 540px"><img class="size-medium wp-image-96" title="flowfield" src="http://www.rickigregersen.com/wp-content/uploads/flowfiled.png" alt="It was a bit to dark, the background is disturbing the image and so on.." width="530" height="400" /><p class="wp-caption-text">Which makes this Keanu Reeves..</p></div><br />
He had been building some flow fields using Perlin noise, which my colleague think is Merlins evil brother for some reason.</p>
<p>I was writing a vector library for a particle engine Im going to publish here some day and thought that implementing theSoulwire flow field would be nice. Justin has been so kind as to put out the source, which is awesome  when your code is that good and visually stunning. As I played around with the code I got an idea, I think it is inspired by something I saw on the <a href="http://www.processing.org">Processing site</a> the idea was to use the input from my webcam as the noise map, instead of the <del datetime="2009-05-25T20:12:34+00:00">M</del>Perlin noise. I made a quick mockup, that actually looks quite promising. It needs some sort of effect, maybe an IR webcam, a 5&#215;5 meter black wall to project it on, before it is ready, also the manipulation of the webcam image before it get&#8217;s read by the particles could give it a little extra. It is ticking along quite well on my Macbook Air, running the webcam and 5000 vectors.</p>
<p><object width="530" height="800" data="http://www.rickigregersen.com/wp-content/uploads/FlowfieldBumpmapWebcam.swf" type="application/x-shockwave-flash"><param name="quality" value="high" /><param name="scale" value="exactfit" /><param name="salign" value="l" /><param name="wmode" value="transparent" /><param name="src" value="http://www.rickigregersen.com/wp-content/uploads/flowfieldweb.swf" /></object></p>
<p>For now I&#8217;ll just put the source code up here:</p>
<p>(It is build using Eclipse (Flex Builder Plugin), so it extends UIComponent, not something I would recommend, but if someone can tell me how to add Sprites to the stage of an AIR app, I will surley change it)</p>
<pre lang="java">
package
{
	import com.rickigregersen.flow_bump.Arrow;

	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.filters.BlurFilter;
	import flash.filters.ColorMatrixFilter;
	import flash.geom.ColorTransform;
	import flash.media.Camera;
	import flash.media.Video;

	import mx.core.UIComponent;

	public class Main extends UIComponent
	{
		//theese are the values I had fun playing with, but you might want something a bit more crazy:)

		private static const TRAIL : Number = 0.95;
		private static const BOOST : int = 110;
		private static const VECTORS : int = 1000;
		private static const BLUR : int = 4;
		private static const SCALE : Number = 6.0;
		private static const BACKGROUND : uint = 0x000000;
		;

		private static var fading : ColorTransform = new ColorTransform( 1, 1, 1, TRAIL );
		private static var blurFilter : BlurFilter = new BlurFilter( BLUR, BLUR, 3 )
		private static var grayMatrix : Array = [                       0.3, 0.59, 0.11, 0, 0, 0.3, 0.59, 0.11, 0, 0, 0.3, 0.59, 0.11, 0, 0, 0, 0, 0, 1, 0 ];
		private var grayFilter : ColorMatrixFilter = new ColorMatrixFilter( grayMatrix );
		private var brightMatrix : Array = [                      1, 0, 0, 0, BOOST, 0, 1, 0, 0, BOOST, 0, 0, 1, 0, BOOST, 0, 0, 0, 1, 0 ]
		private var brightFilter : ColorMatrixFilter = new ColorMatrixFilter( brightMatrix );

		private var camBMD : BitmapData;
		private var camBMP : Bitmap;
		private var renderBMD : BitmapData;
		private var renderBMP : Bitmap;
		private var vid : Video;
		private var cam : Camera;
		private var w : int, h : int;
		private var flowImage : Sprite;

		public function Main( _w : int, _h : int )
		{
			//width and height, also a sprite for holding the little particles;
			w = _w;
			h = _h;
			flowImage = new Sprite();
			//get the video;
			cam = Camera.getCamera();
			vid = new Video( w, h );
			vid.attachCamera( cam );

			//make a bitmap for drawing the video into
			camBMD = new BitmapData( w, h, false, BACKGROUND );
			camBMP = new Bitmap( camBMD );

			//a bitmap to stuff it all into and give some sort of effect
			renderBMD = new BitmapData( w, h, false, BACKGROUND );
			renderBMP = new Bitmap( renderBMD );

			//add the cam bmp and the rendered bmp
			addChild( camBMP );
			camBMP.y = h;

			addChild( renderBMP );
			//build the little vectors
			buildVectors();
		}

		private function buildVectors() : void
		{
			//place, rotate and scale them randomly
			var a : Arrow;

			for ( var i : int = 0; i < VECTORS; i++ )
			{
				a = new Arrow();
				a.x = Math.random() * w;
				a.y = Math.random() * h;
				a.rotation = Math.random() * 360;
				a.scaleX = a.scaleY = Math.random() + 0.1;
				flowImage.addChild( a );
			}
			addEventListener( Event.ENTER_FRAME, update, false, 0, true );
		}

		private function update( event : Event ) : void
		{
			//draw the cam to the stage, appy the grayscale Matrix, sometimes blur and brightfilters too
			camBMD.draw( vid );
			camBMP.filters = [                         grayFilter /*, blurFilter, brightFilter */  ];

			var a : Arrow;
			var brightness : Number;
			var radians : Number;
			var angle : Number;
			var speed : Number;
			var pixel : int;

			for ( var i : int = 0; i < VECTORS; i++ )
			{
				a = flowImage.getChildAt( i ) as Arrow;
				//get the brightness values of the pixel positioned directly under the vector
				pixel = camBMD.getPixel( a.x, a.y );
				brightness = pixel / 0xFFFFFF;
				//adjust spped and angle acording to the brightness
				speed = 0.1 + brightness * a.speed;
				angle = 360 * brightness * a.wander;
				radians = angle * Math.PI / 180;
				//make sure the vector is moving in the same direction as it's velocity
				a.x += Math.cos( radians ) * a.speed;
				a.y += Math.sin( radians ) * a.speed;
				//scale and rotate the vector according to brightness and
				a.rotation = angle;
				a.scaleX = a.scaleY = 0.1 + brightness * SCALE;
				//wrap around
				if ( a.x > w )
					a.x = 0;
				else if ( a.x < 0 )
					a.x = w;
				if ( a.y > h )
					a.y = 0;
				else if ( a.y < 0 )
					a.y = h;

			}

			//apply the colortransform, it determines the transparency of the bitmaps being stacked
			renderBMD.colorTransform( renderBMD.rect, fading );
			//draw the Sprite that hold all the vectors.
			renderBMD.draw( flowImage );

		}
	}
}
</pre>
<p>Here is the Arrow Class:</p>
<pre lang="java">package com.rickigregersen.flow_bump
{
	import flash.display.Shape;

	public class Arrow extends Shape
	{
		public var speed : Number = Math.random() * 3;
		public var wander : Number = Math.random() * 0.5;

		public function Arrow()
		{
			draw();
		}

		private function draw() : void
		{
			/* 			graphics.clear();
			   graphics.lineStyle( 0, 0xFF0000, 0.4 );
			   graphics.moveTo( 0, 0 );
			   graphics.lineTo( 3, 0 );
			   graphics.moveTo( 2, -1 );
			   graphics.lineTo( 3, 0 );
			 graphics.lineTo( 2, 1 ); */

			/* 			graphics.beginFill( 0xFF0000, 0.9 );
			   graphics.drawEllipse( 0, 0, 6, 2 );
			 graphics.endFill(); */

			/*
			   graphics.beginFill( 0x00FF00, 0.9 );
			   graphics.drawCircle( 0, 0, 5 );
			   graphics.endFill();
			 */
			graphics.clear();
			graphics.beginFill( 0xff6600, 1.0 );
			graphics.moveTo( -1, -0.5 );
			graphics.lineTo( 1, 0 );
			graphics.lineTo( -1, 0.5 );
			graphics.lineTo( -1, -0.5 );
			graphics.endFill();
		}
	}
}</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.rickigregersen.com/2009/05/25/webcam-flow-field-in-flash/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Encapsulate some of the Event and try/catch clutter in AS3 and Flex</title>
		<link>http://www.rickigregersen.com/2009/04/20/encapsulate-some-of-the-event-and-trycatch-clutter-in-as3-and-flex/</link>
		<comments>http://www.rickigregersen.com/2009/04/20/encapsulate-some-of-the-event-and-trycatch-clutter-in-as3-and-flex/#comments</comments>
		<pubDate>Mon, 20 Apr 2009 20:03:53 +0000</pubDate>
		<dc:creator>Ricki</dc:creator>
				<category><![CDATA[ActionScript 3.0]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[Event]]></category>
		<category><![CDATA[try/catch]]></category>

		<guid isPermaLink="false">http://www.rickigregersen.com/?p=43</guid>
		<description><![CDATA[A few months back I finally got to clean up some of my classes in an old project. I realized that it was way overdue with a custom PictureLoader Class, a lot of the clutter that made my classes hard to read was try catch and Event handling. When loading a simple piece of graphics [...]]]></description>
			<content:encoded><![CDATA[<p>A few months back I finally got to clean up some of my classes in an old project. I realized that it was way overdue with a custom PictureLoader Class, a lot of the clutter that made my classes hard to read was try catch and Event handling.  When loading a simple piece of graphics into an AIR app(Flash, Flex also of course) you will go through at least these steps:</p>
<ol>
<li>declare loader, url and eventListener.</li>
<li>write listener method</li>
<li>start loading</li>
<li>catch complete event and add the loader to the displayList</li>
</ol>
<p>Ok, not that much work and only a bit of clutter, however I am programming these AIR apps for a 52&#8243; touch screen, meaining no mouse or keyboard, and any exception not caught will result in the UI being rendered useless and me having to VPN into the system and restart it. I really do not want to do this so here is the list that everyone should abide by when loading anything <img src='http://www.rickigregersen.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<ol>
<li>declare loader, url and eventListener for ProgressEvent, IOErrorEvent and CompleteEvent</li>
<li>write listener methods for all of the above events</li>
<li>wrap the loader into a try/catch block to catch exceptions</li>
<li>catch complete or error event and remove all the EventListeners</li>
<li>cast the content of the loader into the appropriate type</li>
<li>copy this new content into a new object so that the loader can be null&#8217;ed and memory freed up</li>
</ol>
<p>Ok, suddenly a lot of work and subsequently heaps of clutter. This is a result of the Event driven way of ActionScript 3.0 but a small price for the OOP architecture it now builds upon.  I wished to encapsulate all the Event logic into the loader and have that deal with it, the Event code adds clutter. The solution uses the same approach as the Proxy Class so if You tried extending that this will make perfect sense. The main idea is to instantiate a PictureLoader object and pass it the methods that should be executed when events happen and let the PictureLoader handle all garbage collection and event clutter (Ok I&#8217;ll stop using the word clutter now, promise).</p>
<pre lang="actionscript">/**
* Author: Ricki Gregersen www.rickigregersen.com
* GPL: All code is snatch and grab
*/
package com.rickigregersen.loaders
{
	import flash.display.Bitmap;
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.net.URLRequest;

	public class PictureLoader extends Loader
	{
		private var pic:Bitmap;
		private var loader:Loader
		private var percent:int;
		private var failFunction:Function;
		private var progressFunction:Function;
		private var successFunction:Function;

		public function PictureLoader()
		{
			//the worlds blankiest blank...
		}
		public function loadPicture(url:String, success:Function = null,  progress:Function = null, fail:Function = null):void
		{
			//set the local functions to your global functions
			failFunction = fail;
			progressFunction = progress;
			successFunction	= success;
			//make the needed objects ready
			pic = new Bitmap();
			loader = new Loader();
			//Add a listener for all the different events the could be fired
			loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, pictureProgress, false, 0, true);
			loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, pictureFailed, false, 0, true);
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, pictureLoaded, false, 0, true);
			//Wrap the load method in a try-catch to catch any wildcard errors that could break the VM
			try{
				loader.load(new URLRequest(url));
			}catch(error:Error){
				pictureFailed();
			}
		}
		//getters for the percent loaded and the picture
		public function get progress():int
		{
			return percent;
		}
		public function get picture():Bitmap
		{
			return pic;
		}
		//Functions to catch the events
		private function pictureProgress(event:Event):void
		{
			percent = (loader.contentLoaderInfo.bytesLoaded / loader.contentLoaderInfo.bytesTotal) * 100;
			//call the parents progressFunction to alert it that a loadProgress has happend
			progressFunction();
		}

		private function pictureLoaded(event:Event):void
		{
			//cast the picture into a bitmap, before this the picture is of type "loader.content"
			pic = (Bitmap)(event.target.content);
			//remove the eventlistener
			loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, pictureLoaded);
			loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, pictureFailed);
			loader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS, pictureProgress);
			//make sure to free the memory and null the loader
			loader.unloadAndStop(true);
			loader = null;
			//call the parents pictureLoaded function to alert it the picture was loaded and ready to be picked up
			successFunction();
			//successFunction(pic); could have been used, just send the parent function a reference to the picture.
			//But I like getter/setters. As long as the method in the parent accepts a bitmap as input you don't have to worry about it
			//in here.
		}
		private function pictureFailed(event:Event = null):void
		{
			//Loading failed, so clean the loader and alert the parent
			loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, pictureLoaded);
			loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, pictureFailed);
			loader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS, pictureProgress);
			loader.unloadAndStop(true);
			loader = null;
			failFunction();
		}
	}
}</pre>
<p>This is how it is used, notice that this is complete with all the logic for listening for the complete, progress and error events.</p>
<pre lang="actionscript">			import com.rickigregersen.loaders.PictureLoader;

			private var picLoader:PictureLoader;
			private var pic:Bitmap;
			private var container:Sprite;

			private function init():void
			{
				picLoader = new PictureLoader();
				picLoader.loadPicture("assets/picture.png", picLoaded, picProgress, picFailed);
			}

			private function picProgress():void
			{
				trace(picLoader.progress);
			}
			private function picLoaded():void
			{
				pic = picLoader.picture;
				rawChildren.addChild(pic);
			}
			private function picFailed():void
			{
				trace("error loading picture");
			}</pre>
<p>The functions passed to the PictureLoader are not mandatory, they are all set to null as default.</p>
<p>This means that the really short version of using this Class is like this:</p>
<pre lang="actionscript">
			import com.rickigregersen.loaders.PictureLoader;

			private var picLoader:PictureLoader;
			private var pic:Bitmap;
			private var container:Sprite;

			private function init():void
			{
				picLoader = new PictureLoader();
				picLoader.loadPicture("assets/picture.png", picLoaded);
			}

			private function picLoaded():void
			{
				rawChildren.addChild(picLoader.picture);
			}
</pre>
<p>The use of <em>rawChildren.addChild(picLoader.picture)</em> is because this originates from a UIComponent in Flex so all other places you would use <em>addChild</em>.</p>
<p>So when building Components or just Classes that handle lots of Events and loading of external assets, this approach is something to consider.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.rickigregersen.com/2009/04/20/encapsulate-some-of-the-event-and-trycatch-clutter-in-as3-and-flex/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
