<?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; ActionScript 3.0</title>
	<atom:link href="http://www.rickigregersen.com/category/actionscript-30/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>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>
