<?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>Soulwire &#187; AS2</title>
	<atom:link href="http://blog.soulwire.co.uk/tag/as2/feed" rel="self" type="application/rss+xml" />
	<link>http://blog.soulwire.co.uk</link>
	<description>Art + Technology</description>
	<lastBuildDate>Mon, 21 Mar 2011 22:27:20 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>AS3 Webcam Motion Tracking</title>
		<link>http://blog.soulwire.co.uk/code/actionscript-3/webcam-motion-detection-tracking</link>
		<comments>http://blog.soulwire.co.uk/code/actionscript-3/webcam-motion-detection-tracking#comments</comments>
		<pubDate>Fri, 06 Jun 2008 18:24:18 +0000</pubDate>
		<dc:creator>Soulwire</dc:creator>
				<category><![CDATA[Actionscript 3.0]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Lab]]></category>
		<category><![CDATA[Open Source]]></category>
		<category><![CDATA[AS2]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[Experiments]]></category>
		<category><![CDATA[Interactive]]></category>

		<guid isPermaLink="false">http://blog.soulwire.co.uk/?p=72</guid>
		<description><![CDATA[Update Ok, you can now grab the MotionTracker source code (AS2 &#38; AS3). Version 2 eventually will include the other methods for detecting and tracking motion which I mentioned. For now I have just included code for the technique used in the demo. For those of you without access to a webcam (and as an [...]


Related posts:<ol><li><a href='http://blog.soulwire.co.uk/code/actionscript-3/colourutils-bitmapdata-extract-colour-palette' rel='bookmark' title='Permanent Link: BitmapData Colour Palette'>BitmapData Colour Palette</a></li>
<li><a href='http://blog.soulwire.co.uk/code/actionscript-3/fit-a-displayobject-into-a-rectangle' rel='bookmark' title='Permanent Link: Fit DisplayObject into Rectangle'>Fit DisplayObject into Rectangle</a></li>
<li><a href='http://blog.soulwire.co.uk/laboratory/flash/perlin-noise-flow-field' rel='bookmark' title='Permanent Link: Perlin Noise Flow Field'>Perlin Noise Flow Field</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p><a href="http://blog.soulwire.co.uk/wp-content/uploads/2008/06/motion-tracking.swf" rel="lightbox;width=800;height=550" title="AS3 Webcam motion tracking demo" ><img src="http://blog.soulwire.co.uk/wp-content/uploads/2008/06/motion-tracking.jpg" alt="AS3 Motion Tracking and Detection" title="AS3 Motion Tracking and Detection" width="710" height="326" class="alignnone size-full wp-image-687" /></a></p>
<p><em>Update</em></p>
<p>Ok, you can now grab the <a href="http://blog.soulwire.co.uk/wp-content/plugins/download-monitor/download.php?id=MotionTracker.zip" title="Download AS3 Motion Tracker source code" >MotionTracker source code (AS2 &amp; AS3)</a>. Version 2 eventually will include the other methods for detecting and tracking motion which I mentioned. For now I have just included code for the technique used in the demo.</p>
<a href="http://blog.soulwire.co.uk/wp-content/plugins/download-monitor/download.php?id=MotionTracker.zip" class="download" title="AS3 Webcam Motion Tracking: A class for performing simple but fast motion detection on objects captured from a webcam. The download includes an example of how to use and configure the MotionTracker source."  rel="nofollow"><strong>Download:</strong> AS3 Webcam Motion Tracking</a><br />
<span id="more-72"></span></p>
<p>For those of you without access to a webcam (and as an example of a practical use for this class) here is a short video demonstrating the program I wrote for the installation piece.</p>
<p><object width="710" height="533"><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="movie" value="http://vimeo.com/moogaloop.swf?clip_id=8920378&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=4eae6a&amp;fullscreen=1" /><embed src="http://vimeo.com/moogaloop.swf?clip_id=8920378&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=4eae6a&amp;fullscreen=1" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="710" height="533"></embed></object></p>
<p><em>End of update</em></p>
<p><em>Webcam required to view the demo (obviously&#8230;)</em></p>
<p>I’m currently working on putting up a show, part of which will be a live generative piece, constructed from the movement of visitors in the gallery space. So of course I needed a method for not only detecting but tracking motion. I’ve seen some of Grant Skinner and Mario Klingemann&#8217;s incredible experiments in this field, the most accurate of which used a coloured object in the camera’s field of vision (such as a ball or glove) to track movement, however I wanted the live response of the artwork to be a surprise to the viewer, and so asking them to hold a coloured object would have detracted from the overall impact.</p>
<p>If you ever want to be amazed by the human mind, try recreating the tasks that it performs, particularly those which we take for granted; you will quickly realise the incredible amount of computation which must be happening just to, for example, detect movement and track an object, or to discern more than one object and their relative motions, velocities and distances. This isn’t an essay on Neuroscience though so I’ll save that for another time&#8230;</p>
<p>Anyway; I researched the concept of motion tracking, and made some notes on my own ideas. A few possible approaches emerged.</p>
<p>One of the most attractive ideas was to <strong>divide the screen into a grid</strong>, and average the colours within each segment at regular intervals. The difference in brightness within each segment (from one calculation to the next) would determine whether or not motion had occurred in this area of the screen. Using this data, clusters of segments with a high rate of change could be used to assume an object in motion. This approach is quite accurate, however it would require a lot of <em>getPixel</em> and <em>setPixel</em>, as well as converting these results into <em>Hue, Saturation, Brightness</em> objects; all of which would be quite processor heavy. A decent computer could handle this fairly easily, however I had to also consider the processing load of the actual generative work running over the top.</p>
<p>The method I used in the end was much lighter, and providing the parameters could be adjusted to suit the environment, it would yield enough accuracy to provide a quite responsive tracking point for the artwork to follow.</p>
<p><strong>Here’s how it works</strong></p>
<ul>
<li>After the camera object is set up, it is passed to a <strong>MotionTracker</strong> class I wrote, which extends the <em>Point</em> object. The <em>MotionTracker</em> class contains two <em>BitmapData</em> objects; one representing the current data from the webcam, the other is used to store the previous frame.</li>
<li>When the <em>update</em> method is called, the new frame is drawn on top of the previous using the <em>difference filter</em>.</li>
<li>The result then has a <em>ColorMatrixFilter</em> applied to it, increasing the contrast of the image and dropping the darker areas (with less movement) further into the background.</li>
<li>A <em>blur filter</em> is then applied in order to further reduce noise and to form blobs from areas where more movement is occurring.</li>
<li>The threshold method is then called in the resulting BitmapData, effectively causing all near black pixels to be ignored and all light pixels (where movement has been detected) to be set to a certain colour.</li>
</ul>
<p>So that takes care of the motion detection, but <em><strong>what about the tracking</strong></em>?</p>
<ul>
<li>First we call the <em>getColorBoundsRect</em> method on the <em>BitmapData</em> object, which gives us a <em>Rectangle</em> object representing the area occupied by pixels of the colour we used when applying the threshold filter.</li>
<li>If the rectangle has an area larger than a specified constant, then sufficient movement is occurring and it can be tracked <em>(choosing to ignore small areas of movement helps to eradicate noise caused by background and other interferences)</em>.</li>
<li>We then find the <em>centre point</em> of this rectangle, and then move the x and y position of the <em>MotionTracker</em> instance to these coordinates, applying some gentle <em>easing</em> to give more continuity to the movement.</li>
</ul>
<p>I will post some source code once the <em>MotionTracker</em> class has been refined, and once I have build a nicer API so that, amongst other things, you can create your own UI for tweaking parameters at runtime; a useful feature if you have requirements such as mine where getting the most accuracy from a particular environment is important.</p>
<p><a href="http://blog.soulwire.co.uk/wp-content/plugins/download-monitor/download.php?id=MotionTracker.zip" class="download" title="AS3 Webcam Motion Tracking: A class for performing simple but fast motion detection on objects captured from a webcam. The download includes an example of how to use and configure the MotionTracker source."  rel="nofollow"><strong>Download:</strong> AS3 Webcam Motion Tracking</a>
<p><em>You can also download the AS3 Motion Tracking source code from the <a rel="nofollow" href="http://blog.soulwire.co.uk//goto/http://code.google.com/p/soulwire/" title="Soulwire code repository" >soulwire code repository</a>.</em></p>


<p>Related posts:<ol><li><a href='http://blog.soulwire.co.uk/code/actionscript-3/colourutils-bitmapdata-extract-colour-palette' rel='bookmark' title='Permanent Link: BitmapData Colour Palette'>BitmapData Colour Palette</a></li>
<li><a href='http://blog.soulwire.co.uk/code/actionscript-3/fit-a-displayobject-into-a-rectangle' rel='bookmark' title='Permanent Link: Fit DisplayObject into Rectangle'>Fit DisplayObject into Rectangle</a></li>
<li><a href='http://blog.soulwire.co.uk/laboratory/flash/perlin-noise-flow-field' rel='bookmark' title='Permanent Link: Perlin Noise Flow Field'>Perlin Noise Flow Field</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://blog.soulwire.co.uk/code/actionscript-3/webcam-motion-detection-tracking/feed</wfw:commentRss>
		<slash:comments>251</slash:comments>
		</item>
		<item>
		<title>AS2 Array count values</title>
		<link>http://blog.soulwire.co.uk/code/actionscript-array-count-values</link>
		<comments>http://blog.soulwire.co.uk/code/actionscript-array-count-values#comments</comments>
		<pubDate>Wed, 20 Jun 2007 12:32:11 +0000</pubDate>
		<dc:creator>Soulwire</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[AS2]]></category>
		<category><![CDATA[Flash]]></category>

		<guid isPermaLink="false">http://blog.soulwire.co.uk/?p=38</guid>
		<description><![CDATA[In PHP there is a very useful function called array_count_values. It sorts an array by returning a 2 dimensional array consisting of non duplicate elements as keys, and values determined by how many times these elements appeared in the original array. update 09.10.10 This can be achieved (in AS2 and AS3) in a much simpler [...]


Related posts:<ol><li><a href='http://blog.soulwire.co.uk/code/split-string-into-sections-using-arrays' rel='bookmark' title='Permanent Link: Splitting Strings'>Splitting Strings</a></li>
<li><a href='http://blog.soulwire.co.uk/code/open-source/dynamic-stacking' rel='bookmark' title='Permanent Link: Dynamic Stacking'>Dynamic Stacking</a></li>
<li><a href='http://blog.soulwire.co.uk/code/actionscript-3/extract-average-colours-from-bitmapdata' rel='bookmark' title='Permanent Link: BitmapData Average Colours'>BitmapData Average Colours</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p><img src="http://blog.soulwire.co.uk/wp-content/uploads/2007/06/array-count-values.jpg" alt="array_count_values in Actionscript" title="array_count_values in Actionscript" width="710" height="325" class="alignnone size-full wp-image-836" /></p>
<p>In PHP there is a very useful function called <a rel="nofollow" href="http://blog.soulwire.co.uk//goto/http://uk.php.net/array_count_values" title="array_count_values"  target="_blank">array_count_values</a>. It sorts an array by returning a 2 dimensional array consisting of non duplicate elements as keys, and values determined by how many times these elements appeared in the original array.</p>
<blockquote><p>update 09.10.10</p>
<p>This can be achieved (in AS2 and AS3) in a much simpler manner, using the following function or prototype:</p>
<pre lang="actionscript">Array.prototype.countValues = function() : Array
{
	var i, j = -1, c, o = {}, r = [], v, n = a.length;
	for(i = 0;i < n;++i) (c = o[v = a[i]]) ? ++c[1] : r[int(++j)] = o[v] = [v,1];
	return r;
}</pre>
</blockquote>
<p>So, for example, an array <strong>[“red”,”green”,”blue”,”red”]</strong> would be converted into <strong>[“red” =&gt; 2, “green” =&gt;1, “blue” =&gt; 1]</strong>. Duplicate elements are removed and the new array indicates how many times each, now unique value occurred in the array.</p>
<p>Unfortunately, Actionscript has no such function built in, so I made one. It is a modification of <a rel="nofollow" href="http://blog.soulwire.co.uk//goto/http://www.senocular.com/" title="The great Senocular"  target="_blank">Senocular’s</a> <strong>array.unique()</strong> prototype (which you can find in his Actionscript library), however rather than simply returning a unique array, it returns the 2 dimensional array mentioned above.<span id="more-38"></span></p>
<pre lang="actionscript">Array.prototype.countValues = function ()
{
	var z, x = this.length, c = false, a = [], d = [];
	while (x--)
	{
		z = 0;
		while (z &lt; x)
		{
			if (this[x] == this[z])
			{
				c = true;
				d.push (this[x]);
				break;
			}
			z++;
		}
		if (!c) a.push ([this[x], 1]);
		else c = false;
	}
	y = a.length;
	while (y--)
	{
		q = 0;
		while (q < d.length)
		{
			if (a[y][0] == d[q]) a[y][1]++;
			q++;
		}
	}
	return a;
};</pre>
<p>Example useage:</p>
<pre lang="actionscript">var myArray:Array = new Array ("the", "cat", "sat", "on", "the", "mat");
var newArray:Array = myArray.countValues ();
trace (newArray);
// Returns [[mat,1],[on,1],[sat,1],[cat,1],[the,2]]</pre>
<p>This prototype should work with arrays of objects, though it was designed for simple datatypes such as strings.</p>
<p>Note: This prototype is case sensitive for strings. If you want it to be case insensitive, just modify to, for example:</p>
<pre lang="actionscript">if (a[y][0].toLowerCase () == d[q].toLowerCase ()) a[y][1]++;</pre>


<p>Related posts:<ol><li><a href='http://blog.soulwire.co.uk/code/split-string-into-sections-using-arrays' rel='bookmark' title='Permanent Link: Splitting Strings'>Splitting Strings</a></li>
<li><a href='http://blog.soulwire.co.uk/code/open-source/dynamic-stacking' rel='bookmark' title='Permanent Link: Dynamic Stacking'>Dynamic Stacking</a></li>
<li><a href='http://blog.soulwire.co.uk/code/actionscript-3/extract-average-colours-from-bitmapdata' rel='bookmark' title='Permanent Link: BitmapData Average Colours'>BitmapData Average Colours</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://blog.soulwire.co.uk/code/actionscript-array-count-values/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Dynamic Stacking</title>
		<link>http://blog.soulwire.co.uk/code/open-source/dynamic-stacking</link>
		<comments>http://blog.soulwire.co.uk/code/open-source/dynamic-stacking#comments</comments>
		<pubDate>Fri, 13 Apr 2007 01:22:09 +0000</pubDate>
		<dc:creator>Soulwire</dc:creator>
				<category><![CDATA[Flash]]></category>
		<category><![CDATA[Open Source]]></category>
		<category><![CDATA[AS2]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Interface]]></category>

		<guid isPermaLink="false">http://blog.soulwire.co.uk/?p=31</guid>
		<description><![CDATA[Update: AS3 Version using TweenLite is available here In this experiment, you can drag the boxes; when you release them the stack will shuffle to accommodate the dropped box. I have recently been working on building an application in which I wanted to use Photoshop style ‘layers’ which the user could create, drag onto different [...]


Related posts:<ol><li><a href='http://blog.soulwire.co.uk/code/dynamic-positioning-part-two' rel='bookmark' title='Permanent Link: Dynamic Positioning Continued'>Dynamic Positioning Continued</a></li>
<li><a href='http://blog.soulwire.co.uk/code/dynamic-positioning-part-one' rel='bookmark' title='Permanent Link: Dynamic Positioning'>Dynamic Positioning</a></li>
<li><a href='http://blog.soulwire.co.uk/code/group94-flash-scrolling-menu-list' rel='bookmark' title='Permanent Link: Group94 Style Menu'>Group94 Style Menu</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p><a href="http://blog.soulwire.co.uk/wp-content/uploads/2007/04/boxes.swf" rel="lightbox;width=800;height=600;" title="Dynamic Stacking Menu" ><img src="http://blog.soulwire.co.uk/wp-content/uploads/2007/04/dynamic-stacking-710x222.jpg" alt="Flash Dynamic Stacking Menu" title="Flash Dynamic Stacking Menu" width="710" height="222" class="alignnone size-medium wp-image-849" /></a></p>
<blockquote><p>
<strong>Update:</strong> AS3 Version using TweenLite is available <a href="http://blog.soulwire.co.uk/wp-content/plugins/download-monitor/download.php?id=dynamic-stack-as3.zip"  title="Download Dynamic Stacking Menu (AS3)">here</a>
</p></blockquote>
<p><em>In this experiment, you can drag the boxes; when you release them the stack will shuffle to accommodate the dropped box.</em></p>
<p>I have recently been working on building an application in which I wanted to use Photoshop style ‘layers’ which the user could create, drag onto different depths and delete.</p>
<p>The basic idea was for the user to <strong>click and drag</strong> ‘layers’, and drop them in between others. This would cause the other layers to reposition to accommodate the new layer and to fill the gap which it had left in it’s old position. Get it?<span id="more-31"></span></p>
<p>Perhaps I’m just a bit dim, but I was surprised at how many difficulties I encountered when trying to sort the boxes or ‘layers’. I began taking the approach of calculating whether the surrounding layers were above or below, and moving them up or down accordingly; but with this approach I found that deleting layers or adding new ones caused the system to go a bit crazy.</p>
<p>As usual, it was the simple and elegant approach which eventually yielded the solution. I basically handled all of the movement from a single, straightforward array. The array was shuffled based on a nifty little prototype I wrote for extracting and reinserting elements (essentially shuffling without randomness), from which all movement calculations (i.e. the eventual positions of the shuffling boxes) could be made.</p>
<pre lang="actionscript">Array.prototype.shuffle = function (x, y)
{
	var i:Number = this.slice (x, x + 1);
	this.splice (x,1);
	this.splice (y,0,i);
};</pre>
<p>The shuffle prototype itself could be very useful, as it pulls items out of the array, and by reinserting them; muscles the other elements outwards around it – no complicated calculations needed for determining where the elements go thanks to some really basic build in Flash Actionscript features. Also, by omitting one line of code, the prototype could be used to simply insert items into an array at a particular position (<em>insertAt(value, index)</em> or something along those lines).</p>
<p>I have attached a bare bones example for you, though I assure you, you can do great things with this script. The menu I was working on will probably make an appearance on Soulwire once the program is finished. One example of its usage would be to associate each box with a layer elsewhere; for example a Stack of photos which are depth ‘shuffled’ through the easy manipulation of their affiliated boxes.</p>
<a href="http://blog.soulwire.co.uk/wp-content/plugins/download-monitor/download.php?id=dynamic-stack-as2.zip" class="download" title="Dynamic Stacking Menu (AS2): A drag & drop menu system which dynamically shuffles as elements are dragged. Source code and example included."  rel="nofollow"><strong>Download:</strong> Dynamic Stacking Menu (AS2)</a> <a href="http://blog.soulwire.co.uk/wp-content/plugins/download-monitor/download.php?id=dynamic-stack-as3.zip" class="download" title="Dynamic Stacking Menu (AS3): A drag & drop menu system which dynamically shuffles as elements are dragged. Source code and example included and update to AS3 with added features."  rel="nofollow"><strong>Download:</strong> Dynamic Stacking Menu (AS3)</a>
<p>You can download the FLA here above, but here is the basic commented code. It is simple but works quite well.</p>
<pre lang="actionscript">// Include the Zigo tweening prototypes to make life easier
#include "lmc_tween.as"

// Create the main holder for the boxes
var menu:MovieClip = this.createEmptyMovieClip ("menu", 0);
menu._x = menu._y = 20;

// Set up our Arrays and counters
var boxes:Array = new Array ();
var boxCount:Number = 0;
var boxGap:Number = 37;

/*
The core 'SHUFFLE' function
This pulls out an array item at a given index and inserts it back into the array
at another index. The other array items are consiquently 'pushed' outwards from
the element, so our new array reflects the visual appearance we want for the
shuffled menu. We can now 'sort' our menu based on this modified array...
*/
Array.prototype.shuffle = function (x, y)
{
	var i:Number = this.slice (x, x + 1);
	this.splice (x,1);
	this.splice (y,0,i);
};

// Our sort function
function sort ()
{
	var count:Number = boxes.length;
	for (var i = 0; i &lt; count; i++)
	{
		// Check which ID is at this point in the array
		var id:Number = boxes[i];
		// Find the corrosponding box
		var box:MovieClip = eval ("menu.box" + id);
		// Calculate the position the box should be at
		var y:Number = i * boxGap;
		// Alter it's id to it's new position
		box.id = i;
		// Tween it there (or just use box._y = y)
		box.tween ("_y",y,0.8,"easeInOut");
	}
}

// Creating new boxes
function addBox ()
{
/*
We use getNextHighestDepth() because of the swapDepths() function used
when the buttons are pressed. This creates new levels and so could
cause problems if we just create boxes at depths based on the length
of the boxes array.
*/
	var d:Number = menu.getNextHighestDepth ();
	var i:Number = boxCount;
	// We could use Push(), but I want the boxes stacked, with newest on top
	boxes.unshift (i);
	// Attach the box
	var box:MovieClip = menu.attachMovie ("box", "box" + i, d);
	// Initiate the box
	box.initBox (i);
	// Increase the counter
	boxCount++;
	// Sort the boxes
	sort ();
}

/*
This is a simple function which can be called to remove boxes. It splices the array and
resorts the stack accordingly.
*/
function deleteLayer (id)
{
	var box:MovieClip = eval ("menu.box" + boxes[id]);
	box.removeMovieClip ();
	boxes.splice (id,1);
	sort ();
}

// The box actions
MovieClip.prototype.initBox = function (num)
{
	this.id = num;
	this.name.text = "Box " + num;

	this.onPress = function ()
	{
		var top:Number = boxCount + 1;

		this.swapDepths (top);
		this.startDrag ();
	};

	this.onRelease = this.onReleaseOutside = function ()
	{
		this.stopDrag ();
		// Slide back to the edge
		this.tween ("_x",0,0.8,"easeInOut");
	/*
	Find the original position, and the new position, based on the
	location of the draged box in relation to the other boxes.
	*/
		var x:Number = this.id;
		var y:Number = Math.round (this._y / boxGap);
		// Call our shuffle function based on these calculations
		boxes.shuffle (x,y);
		// Sort the boxes based on our shuffled array
		sort ();
	};
};

// Add some boxes to test it!
for (var i = 0; i &lt; 5; i++)
{
	addBox ();
}</pre>


<p>Related posts:<ol><li><a href='http://blog.soulwire.co.uk/code/dynamic-positioning-part-two' rel='bookmark' title='Permanent Link: Dynamic Positioning Continued'>Dynamic Positioning Continued</a></li>
<li><a href='http://blog.soulwire.co.uk/code/dynamic-positioning-part-one' rel='bookmark' title='Permanent Link: Dynamic Positioning'>Dynamic Positioning</a></li>
<li><a href='http://blog.soulwire.co.uk/code/group94-flash-scrolling-menu-list' rel='bookmark' title='Permanent Link: Group94 Style Menu'>Group94 Style Menu</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://blog.soulwire.co.uk/code/open-source/dynamic-stacking/feed</wfw:commentRss>
		<slash:comments>48</slash:comments>
		</item>
		<item>
		<title>Dynamic Positioning Continued</title>
		<link>http://blog.soulwire.co.uk/code/dynamic-positioning-part-two</link>
		<comments>http://blog.soulwire.co.uk/code/dynamic-positioning-part-two#comments</comments>
		<pubDate>Thu, 08 Mar 2007 15:24:55 +0000</pubDate>
		<dc:creator>Soulwire</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[AS2]]></category>

		<guid isPermaLink="false">http://blog.soulwire.co.uk/?p=25</guid>
		<description><![CDATA[Following another post regarding using stage listeners to dynamically resize and position MovieClips in Flash, I was asked to upload and FLA as an example. The previous code can just be pasted into the actions panel and will run without any MovieClips needed, and was designed to show the StageListener function rather than showcase a [...]


Related posts:<ol><li><a href='http://blog.soulwire.co.uk/code/dynamic-positioning-part-one' rel='bookmark' title='Permanent Link: Dynamic Positioning'>Dynamic Positioning</a></li>
<li><a href='http://blog.soulwire.co.uk/code/open-source/dynamic-stacking' rel='bookmark' title='Permanent Link: Dynamic Stacking'>Dynamic Stacking</a></li>
<li><a href='http://blog.soulwire.co.uk/code/actionscript-3/managing-runtime-loaded-dynamic-assets' rel='bookmark' title='Permanent Link: Hello Dynamic Sprite'>Hello Dynamic Sprite</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p><img class="alignnone size-full wp-image-892" title="Flash Liquid Layout Dynamic Positioning" src="http://blog.soulwire.co.uk/wp-content/uploads/2007/03/dynamic-positioning.jpg" alt="Flash Liquid Layout Dynamic Positioning" width="710" height="326" /></p>
<p>Following another post regarding <a href="http://blog.soulwire.co.uk/code/dynamic-positioning-part-one" title="Dynamically Position elements with Flash using Stage Listeners" >using stage listeners to dynamically resize and position MovieClips in Flash</a>, I was asked to upload and FLA as an example. The previous code can just be pasted into the actions panel and will run without any MovieClips needed, and was designed to show the StageListener function rather than showcase a particular dynamic layout.</p>
<p><a href="http://blog.soulwire.co.uk/wp-content/plugins/download-monitor/download.php?id=stage-listeners.zip" class="download" title="Fluid Flash Layout Example: An example of using stage listeners to dynamically resize Flash content and reconfigure content, creating a fluid and flexible layout in Flash. This is useful for fullscreen Flash websites."  rel="nofollow"><strong>Download:</strong> Fluid Flash Layout Example</a><span id="more-25"></span></p>
<p>Anyway, here is a slightly amended version of this simple technique which uses MovieClips, and produces a fluid layout based on percentages (much like the classic CSS three column layout).</p>
<p>Again, this can be easily modified to include more elements, margins etc. Note – if you want to add content to the Movieclips (for example A menu in the left column), simply make the resizable elements a background MovieClip inside each parent MovieClip.</p>
<p>For instance, our left column would then be resized by using:</p>
<pre lang="actionscript">leftCol.backGround.pos(x, y, w, h);</pre>
<p>The content inside the leftCol MovieClip would then maintain its size as the background resizes. Easy.</p>
<pre lang="actionscript">// Setup the Stage properties
Stage.scaleMode = "noScale";
Stage.align = "TL";

// Variables (in pixels)
var headHeight:Number = 100;
var footHeight:Number = 50;

// Variables (in percent)
var leftColWidth:Number = 25;
var mainColWidth:Number = 60;
var rightColWidth:Number = 15;

// Prototype function to position MovieClips
MovieClip.prototype.pos = function (x, y, w, h)
{
	this._x = x;
	this._y = y;
	this._width = w;
	this._height = h;
};

// Out main function
function setStage ()
{
	var sw:Number = Stage.width;
	var sh:Number = Stage.height;
	// Calculate percentages based on Stage dimensions
	var Lw:Number = sw / 100 * leftColWidth;
	var Mw:Number = sw / 100 * mainColWidth;
	var Rw:Number = sw / 100 * rightColWidth;
	var Hp:Number = sh - (headHeight + footHeight);
	// Position / resize our MovieClips
	header.pos (0,0,sw,headHeight);
	leftCol.pos (0,headHeight,Lw,Hp);
	mainCol.pos (Lw,headHeight,Mw,Hp);
	rightCol.pos (Lw + Mw,headHeight,Rw,Hp);
	footer.pos (0,sh - footHeight,sw,footHeight);
}

// Add a listener
var stageListener:Object = new Object ();
Stage.addListener (stageListener);

// When the Stage dimensions change...
stageListener.onResize = setStage;

// Call our function to set it all up right!
setStage ();</pre>


<p>Related posts:<ol><li><a href='http://blog.soulwire.co.uk/code/dynamic-positioning-part-one' rel='bookmark' title='Permanent Link: Dynamic Positioning'>Dynamic Positioning</a></li>
<li><a href='http://blog.soulwire.co.uk/code/open-source/dynamic-stacking' rel='bookmark' title='Permanent Link: Dynamic Stacking'>Dynamic Stacking</a></li>
<li><a href='http://blog.soulwire.co.uk/code/actionscript-3/managing-runtime-loaded-dynamic-assets' rel='bookmark' title='Permanent Link: Hello Dynamic Sprite'>Hello Dynamic Sprite</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://blog.soulwire.co.uk/code/dynamic-positioning-part-two/feed</wfw:commentRss>
		<slash:comments>57</slash:comments>
		</item>
		<item>
		<title>Group94 Style Menu</title>
		<link>http://blog.soulwire.co.uk/code/group94-flash-scrolling-menu-list</link>
		<comments>http://blog.soulwire.co.uk/code/group94-flash-scrolling-menu-list#comments</comments>
		<pubDate>Wed, 07 Mar 2007 20:42:15 +0000</pubDate>
		<dc:creator>Soulwire</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[AS2]]></category>
		<category><![CDATA[Interface]]></category>
		<category><![CDATA[Open Source]]></category>

		<guid isPermaLink="false">http://blog.soulwire.co.uk/?p=22</guid>
		<description><![CDATA[I have always liked the neat and compact menu system Group94 often use in their projects. It is really useful for lists or menus and is also of course very elegant. I don’t know how Group94 do it, but here is my attempt. I have created two prototypes to make it easy to use multiple [...]


Related posts:<ol><li><a href='http://blog.soulwire.co.uk/code/actionscript-3/as3-group94-style-scrolling-menu' rel='bookmark' title='Permanent Link: AS3 Group94 Menu'>AS3 Group94 Menu</a></li>
<li><a href='http://blog.soulwire.co.uk/code/circular-group94-scrolling-flash-menu' rel='bookmark' title='Permanent Link: G94 Circular Menu'>G94 Circular Menu</a></li>
<li><a href='http://blog.soulwire.co.uk/code/open-source/dynamic-stacking' rel='bookmark' title='Permanent Link: Dynamic Stacking'>Dynamic Stacking</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p><a href="http://blog.soulwire.co.uk/wp-content/uploads/2007/03/group94-scroll-list.swf" rel="lightbox;width=800;height=450;" title="Group94 Style Scrolling Menu List" ><img src="http://blog.soulwire.co.uk/wp-content/uploads/2007/03/as2-group94-menu.jpg" alt="Group94 Style Menu System" title="Group94 Style Menu System" width="710" height="326" class="alignnone size-full wp-image-895" /></a></p>
<p>I have always liked the neat and compact menu system <a rel="nofollow" href="http://blog.soulwire.co.uk//goto/http://www.group94.com/" title="The awesome Group94"  target="_blank">Group94 </a>often use in their projects. It is really useful for lists or menus and is also of course very elegant.</p>
<p>I don’t know how Group94 do it, but here is my attempt. I have created two prototypes to make it easy to use multiple times within a project.</p>
<p><a href="http://blog.soulwire.co.uk/wp-content/plugins/download-monitor/download.php?id=group94-scroll-list.zip" class="download" title="Group94 Style Scrolling List: A Group94 inspired scrolling list or menu system prototype for Flash"  rel="nofollow"><strong>Download:</strong> Group94 Style Scrolling List</a><span id="more-22"></span></p>
<p>This example just uses a simple list, but the actionscript can easily be modified to make the items clickable to use as a menu system, or you could even use images. Your call!</p>
<p>The first prototype is used to create a vertical list from an array. The second is used to scroll the list depending on clicks or mouse wheel scrolling.</p>
<p>This is bare bones, so it is nice and simple and easy to build upon.</p>
<pre lang="actionscript">/*
I have used the old Zigo tweening prorotypes here, it will also work with Fuse.
Links are always good if you use it :)
*/
#include "lmc_tween.as"

MovieClip.prototype.createList = function (array, clip, visibleItems, xPos, yPos, spacing, offset)
{
	this.listItems = new Array ();

	this.total = array.length;
	this.vis = visibleItems;
	this.spacing = spacing;
	this.offset = offset;
	this._x = xPos;
	this._y = yPos;
	this.num = 1;

	for (var i = 1; i &lt;= this.total; i++)
	{
		var listItem:MovieClip = this.attachMovie (clip, "listItem" + i, i);
		listItem.yPos = i * this.spacing;
		if (i &lt;= this.vis)
		{
			listItem._y = listItem.yPos;
		}
		else
		{
			listItem._y = this.offset + (i * this.spacing);
			listItem._alpha = 0;
		}
		listItem.id = i;
		listItem.txt.text = i + ". " + array[i - 1];
		listMc.listItems[i - 1] = listItem;
	}
};

MovieClip.prototype.shuffleList = function (n)
{
	this.num += n;
	if (this.num &gt; 0 &amp;&amp; this.num &lt;= ((this.total + 1) - this.vis))
	{
		var min:Number = this.num;
		var max:Number = this.num + this.vis;
		for (var i = 1; i &lt;= this.total; i++)
		{
			var mc = this.listItems[i - 1];
			if (i &lt; min)
			{
				var Y:Number = mc.yPos - this.offset - (this.num * this.spacing);
				var alpha:Number = 0;
			}
			else if (i &gt;= max)
			{
				var Y:Number = mc.yPos + this.offset - (this.num * this.spacing);
				var alpha:Number = 0;
			}
			else
			{
				var Y:Number = mc.yPos - (this.num * this.spacing);
				var scale:Number = 100;
				var alpha:Number = 100;
			}
			mc.tween ("_y",Y,.7);
			mc.alphaTo (alpha,.7);
		}
	}
	else
	{
		this.num -= n;
	}
};

var fruits:Array = new Array ("Cornelian cherry ", "Date palm", "Fig", "Grape", "Jujube", "Black mulberry", "Olive", "Pomegranate", "Pomelo", "Grapefruit", "Lemon", "Lime", "Mandarin", "Clementine", "Tangerine", "Orange", "Avocado", "Feijoa", "Guava", "Kumquat", "Longan", "Lúcuma", "Lychee", "Passion fruit", "Pond-apple", "Strawberry guava", "Tamarillo", "Yangmei");

var listMc:MovieClip = this.createEmptyMovieClip ("listMc", 0);
listMc.createList (fruits,"listItem",5,150,40,30,20);
listMc.shuffleList (0);

prevBtn.onPress = function ()
{
	listMc.shuffleList (-1);
};
nextBtn.onPress = function ()
{
	listMc.shuffleList (+1);
};

var mouseListener:Object = new Object ();
mouseListener.onMouseWheel = function (delta)
{
	if (delta &lt; 0)
	{
		listMc.shuffleList (+1);
	}
	else
	{
		listMc.shuffleList (-1);
	}
};
Mouse.addListener (mouseListener);</pre>


<p>Related posts:<ol><li><a href='http://blog.soulwire.co.uk/code/actionscript-3/as3-group94-style-scrolling-menu' rel='bookmark' title='Permanent Link: AS3 Group94 Menu'>AS3 Group94 Menu</a></li>
<li><a href='http://blog.soulwire.co.uk/code/circular-group94-scrolling-flash-menu' rel='bookmark' title='Permanent Link: G94 Circular Menu'>G94 Circular Menu</a></li>
<li><a href='http://blog.soulwire.co.uk/code/open-source/dynamic-stacking' rel='bookmark' title='Permanent Link: Dynamic Stacking'>Dynamic Stacking</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://blog.soulwire.co.uk/code/group94-flash-scrolling-menu-list/feed</wfw:commentRss>
		<slash:comments>34</slash:comments>
		</item>
		<item>
		<title>Splitting Strings</title>
		<link>http://blog.soulwire.co.uk/code/split-string-into-sections-using-arrays</link>
		<comments>http://blog.soulwire.co.uk/code/split-string-into-sections-using-arrays#comments</comments>
		<pubDate>Thu, 08 Feb 2007 19:16:03 +0000</pubDate>
		<dc:creator>Soulwire</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[AS2]]></category>
		<category><![CDATA[Flash]]></category>

		<guid isPermaLink="false">http://blog.soulwire.co.uk/?p=13</guid>
		<description><![CDATA[I was working on a project recently where I recieved a data feed and needed to split it into sections that would fit into a text box (ie. 200 characters at a time). The user would then click &#8216;more&#8216; and progress through the text in segments until they had read all of it. I wrote [...]


Related posts:<ol><li><a href='http://blog.soulwire.co.uk/code/open-source/dynamic-stacking' rel='bookmark' title='Permanent Link: Dynamic Stacking'>Dynamic Stacking</a></li>
<li><a href='http://blog.soulwire.co.uk/code/actionscript-array-count-values' rel='bookmark' title='Permanent Link: AS2 Array count values'>AS2 Array count values</a></li>
<li><a href='http://blog.soulwire.co.uk/code/group94-flash-scrolling-menu-list' rel='bookmark' title='Permanent Link: Group94 Style Menu'>Group94 Style Menu</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>I was working on a project recently where I recieved a data feed and needed to split it into sections that would fit into a text box (ie. 200 characters at a time). The user would then click &#8216;<em>more</em>&#8216; and progress through the text in segments until they had read all of it.</p>
<p>I wrote this simple prototype, which splits a string into sections of a defined size (and optionally adds a postfix, ie. &#8220;&#8230;&#8221; to the end), and returns an array containing the segmented text.<span id="more-13"></span></p>
<p>Here is the prototype:</p>
<pre lang="actionscript" line="1">String.prototype.splitUp = function (maxLetters, postFix)
{
	var txt:String = this + 1;
	var split:Array = new Array ();

	var num = 0;
	var counter = 0;
	while (num >= 0)
	{
		var num = txt.indexOf (" ", maxLetters);

		if (postFix != undefined &#038;&#038; num >= 0)
		{
			var t = txt.slice (0, num) + postFix;
		}
		else
		{
			var t = txt.slice (0, num);
		}

		split[counter] = t;
		txt = txt.slice (num + 1, txt.length);
		counter++;
	}

	return split;
};</pre>
<p>&#8230; And an example of it&#8217;s usage &#8211; in this instance the string is converted to an array which can then be used to populate a text box:</p>
<pre lang="actionscript">var phrases = blurb.splitUp (190, "...");</pre>
<p>It may have some bugs as I have only tested it within the context of the project I wrote it for. With a little added Actionscript though, it should be a useful tool.</p>


<p>Related posts:<ol><li><a href='http://blog.soulwire.co.uk/code/open-source/dynamic-stacking' rel='bookmark' title='Permanent Link: Dynamic Stacking'>Dynamic Stacking</a></li>
<li><a href='http://blog.soulwire.co.uk/code/actionscript-array-count-values' rel='bookmark' title='Permanent Link: AS2 Array count values'>AS2 Array count values</a></li>
<li><a href='http://blog.soulwire.co.uk/code/group94-flash-scrolling-menu-list' rel='bookmark' title='Permanent Link: Group94 Style Menu'>Group94 Style Menu</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://blog.soulwire.co.uk/code/split-string-into-sections-using-arrays/feed</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Dynamic Positioning</title>
		<link>http://blog.soulwire.co.uk/code/dynamic-positioning-part-one</link>
		<comments>http://blog.soulwire.co.uk/code/dynamic-positioning-part-one#comments</comments>
		<pubDate>Wed, 27 Sep 2006 15:43:04 +0000</pubDate>
		<dc:creator>Soulwire</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[AS2]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Interface]]></category>

		<guid isPermaLink="false">http://blog.soulwire.co.uk/?p=8</guid>
		<description><![CDATA[Since fullscreen flash websites have become so popular; this little trick (a necessity really) has been used to make sure that no matter what screen resolution a user has, they can experience the site in the way in which the designer intended. I realise that listeners are common knowledge, but I have had a lot [...]


Related posts:<ol><li><a href='http://blog.soulwire.co.uk/code/dynamic-positioning-part-two' rel='bookmark' title='Permanent Link: Dynamic Positioning Continued'>Dynamic Positioning Continued</a></li>
<li><a href='http://blog.soulwire.co.uk/code/open-source/dynamic-stacking' rel='bookmark' title='Permanent Link: Dynamic Stacking'>Dynamic Stacking</a></li>
<li><a href='http://blog.soulwire.co.uk/code/actionscript-3/fit-a-displayobject-into-a-rectangle' rel='bookmark' title='Permanent Link: Fit DisplayObject into Rectangle'>Fit DisplayObject into Rectangle</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p><img src="http://blog.soulwire.co.uk/wp-content/uploads/2006/09/soulwire-v2-interface.jpg" alt="Soulwire V2 Interface" title="Soulwire V2 Interface" width="710" height="223" class="alignnone size-full wp-image-908" /></p>
<p>Since fullscreen flash websites have become so popular; this little trick (a necessity really) has been used to make sure that no matter what screen resolution a user has, they can experience the site in the way in which the designer intended.</p>
<p>I realise that listeners are common knowledge, but I have had a lot of emails asking about <strong>dynamic positioning</strong> so I have written this brief tutorial on how to get all the elements in your Flash movie to position themselves automatically when the user&#8217;s browser window resizes, or if they are using a different screen resolution to you and you hate the idea of letting the movie scale!<span id="more-12"></span></p>
<p>The principal is simple, like in CSS with fluid layouts, you effectivelly position everything relitively, and write some simple formulas for maintaining their relitivity no matter what size stage they are on.</p>
<p>You can also apply this to the height and width of, for example, the background of a banner, so that your pixel font still looks crisp and nicely aligned as the bar behind is stretching to fit the window. The permutations are endless, and evidently, this is a really useful and basic tool to have when putting together a Flash site. I have even used it for CD Business Card presentations, to make sure text areas and graphics sit nicely in harmony with each other, even if it found its way into the grubbly hands of an 800 x 600 user.</p>
<p>Anyway, the code &#8211; Just paste this into the Actionscript panel, render and enjoy. Drag the movie&#8217;s resize handles around for some orgasmic repositioning pleasure:</p>
<pre lang="actionscript" line="1">// Setup the Stage properties
Stage.scaleMode = "noScale";
Stage.align = "TL";

// Create some dummy boxes
// This is only for the demonstration

var margin:Number = 10;
var boxSize:Number = 100;

var boxes = [
		["top", "0x9933cc"],
		["right", "0x00ff00"],
		["bottom", "0xff0000"],
		["left", "0x0099ff"],
		["center", "0xff3399"]];

for (var i = 0; i < boxes.length; i++)
{
	var newBox:MovieClip = createEmptyMovieClip (boxes[i][0], getNextHighestDepth ());

	var nameBadge = newBox.createTextField ("test_txt", 1, 0, (boxSize / 2) - 10, boxSize, 10);
	nameBadge.autoSize = "center";
	nameBadge.text = boxes[i][0];
	nameBadge.textColor = 0xffffff;

	with (newBox)
	{
		beginFill (boxes[i][1],100);
		moveTo (0,0);
		lineTo (boxSize,0);
		lineTo (boxSize,boxSize);
		lineTo (0,boxSize);
		lineTo (0,0);
		endFill ();
	}
}

/*
	The core function...
	I keep this separate from the 'onResize'
	handler so that I can call it at the start
	to position all of my elements if the screen
	size has remained the same
*/

function setStage ()
{
	var sw:Number = Stage.width;
	var sh:Number = Stage.height;
/*
Rule for Top / Center alignment
*/
	top._x = sw / 2 - top._width / 2;
	top._y = margin;
/*
Rule for Right / Middle alignment
*/
	right._x = sw - (right._width + margin);
	right._y = sh / 2 - right._height / 2;
/*
Rule for Bottom / Center alignment
*/
	bottom._x = sw / 2 - bottom._width / 2;
	bottom._y = sh - (bottom._height + margin);
/*
Rule for Left / Middle alignment
*/
	left._x = margin;
	left._y = sh / 2 - left._height / 2;
/*
Rule for Center / Middle alignment
*/
	center._x = sw / 2 - center._width / 2;
	center._y = sh / 2 - center._height / 2;
}

// Add a listener
var stageListener:Object = new Object ();
Stage.addListener (stageListener);

// When the Stage dimensions change...
stageListener.onResize = setStage;

// Call our function to set it all up right!
setStage ();</pre>
<p>Have fun!</p>


<p>Related posts:<ol><li><a href='http://blog.soulwire.co.uk/code/dynamic-positioning-part-two' rel='bookmark' title='Permanent Link: Dynamic Positioning Continued'>Dynamic Positioning Continued</a></li>
<li><a href='http://blog.soulwire.co.uk/code/open-source/dynamic-stacking' rel='bookmark' title='Permanent Link: Dynamic Stacking'>Dynamic Stacking</a></li>
<li><a href='http://blog.soulwire.co.uk/code/actionscript-3/fit-a-displayobject-into-a-rectangle' rel='bookmark' title='Permanent Link: Fit DisplayObject into Rectangle'>Fit DisplayObject into Rectangle</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://blog.soulwire.co.uk/code/dynamic-positioning-part-one/feed</wfw:commentRss>
		<slash:comments>25</slash:comments>
		</item>
	</channel>
</rss>

