<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Swingpant&#039;s Game Lab &#187; as3</title>
	<atom:link href="http://swingpants.com/tag/as3/feed/" rel="self" type="application/rss+xml" />
	<link>http://swingpants.com</link>
	<description>Swingpant&#039;s Code Nurdlings</description>
	<lastBuildDate>Tue, 08 May 2012 21:07:53 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='swingpants.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://0.gravatar.com/blavatar/4a88693aa17d6b5e1d453a6684bc1c68?s=96&#038;d=http%3A%2F%2Fs2.wp.com%2Fi%2Fbuttonw-com.png</url>
		<title>Swingpant&#039;s Game Lab &#187; as3</title>
		<link>http://swingpants.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://swingpants.com/osd.xml" title="Swingpant&#039;s Game Lab" />
	<atom:link rel='hub' href='http://swingpants.com/?pushpress=hub'/>
		<item>
		<title>AS3 Flamer Class :: Setting things on fire</title>
		<link>http://swingpants.com/2010/02/25/as3-flamer-class-setting-things-on-fire/</link>
		<comments>http://swingpants.com/2010/02/25/as3-flamer-class-setting-things-on-fire/#comments</comments>
		<pubDate>Thu, 25 Feb 2010 12:14:49 +0000</pubDate>
		<dc:creator>swingpants</dc:creator>
				<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[as3]]></category>
		<category><![CDATA[Code Snippet]]></category>
		<category><![CDATA[Effect]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Flash Games]]></category>
		<category><![CDATA[bitmapdata]]></category>
		<category><![CDATA[blendMode]]></category>
		<category><![CDATA[convolution filter]]></category>
		<category><![CDATA[fire]]></category>
		<category><![CDATA[fire effect]]></category>
		<category><![CDATA[flamer]]></category>
		<category><![CDATA[flames]]></category>
		<category><![CDATA[paletteMap]]></category>
		<category><![CDATA[perlin noise]]></category>

		<guid isPermaLink="false">http://swingpants.com/?p=230</guid>
		<description><![CDATA[The Flamer Class is a quick and easy method to set things on fire. Throw some fuel (any display object) at the class and make it burn. While working on another project I needed to make some lightweight flames. I looked around, tried a bit of perlin noise, some convolution filters &#8211; then some PixelBender [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=swingpants.com&#038;blog=5703708&#038;post=230&#038;subd=flashlabs&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>
The Flamer Class is a quick and easy method to set things on fire. Throw some fuel (any display object) at the class and make it burn.
</p>
<p>
While working on another project I needed to make some lightweight flames. I looked around, tried a bit of perlin noise, some convolution filters &#8211; then some PixelBender filtering &#8211; I wasn&#8217;t really happy with the results.
</p>
<p>
I found the excellent Saqoosha&#8217;s <a href="http://wonderfl.net/code/bffb3437de866ffdfcdd5015b1fba5ca37fff72a">Fire</a> on <a href="http://wonderfl.net">Wonderfl</a>.<br />
This demo had nearly the flicker and colour mapping that I was looking for. Rendering the perlin noise on each frame was causing a bit too much processor overhead for my purposes.<br />
Instead of rendering two perlin octaves and moving the layers against each other, I took two separate single octave perlin noise bitmaps and rotated one against the other while blending.<br />
This gives a nice effect and is very lightweight. With the Saqoosha method I was clocking at a steady 26fps, with my lightweight method I could get 90fps.
</p>
<p>
To get the flame hues I set up a gradient box, threw some colours at it, loaded those colours into an array which I then pushed to a palette map.
</p>
<p>
So how to use the Flamer class:</p>
<blockquote><p>
<strong><br />
import swingpants.effect.Flamer</p>
<p>var flamer:Flamer=new Flamer(display_object)<br />
addChild(flamer)</p>
<p>addEventListener(Event.ENTER_FRAME, enterFrameHandler)<br />
function enterFrameHandler(event:Event):void<br />
   {<br />
     flamer.update()<br />
   }<br />
</strong>
</p></blockquote>
<p>Your object will now be on fire. Feel naughty?
</p>
<p>You can set the render width and height and also set the direction of the flames by setting a point vector.</p>
<blockquote><p>
<strong><br />
var flamer:Flamer=new Flamer(display_object,render_width, render_height, flame_direction_point)<br />
addChild(flamer)</p>
<p>//The colour of the flames can be set with one of the presets (There are 4 at the moment &#8211; ORANGE_FLAME, BLUE_FLAME, YELLOW_FLAME, GREEN_FLAME &#8211; but I will be adding more)<br />
flamer.setFlameColour(Flamer.BLUE_FLAME) </p>
<p>//Direction can be set on the fly:<br />
flamer.setFlameDirection(direction_point)<br />
</strong>
</p></blockquote>
<p>Here is a demo of a pure code use of the Flamer: <a href="http://www.swingpantsflash.com/flamer/Flamer.html">The Flaming Vortex</a>. You can click on the flash to initiate an explosion, and pressing space will change the colour. Have a play:</p>
<div id="attachment_232" class="wp-caption aligncenter" style="width: 360px"><a href="http://www.swingpantsflash.com/flamer/Flamer.html"><img src="http://flashlabs.files.wordpress.com/2010/02/flaming_sun.jpg?w=655" alt="Flaming Sun" title="flaming_sun"   class="size-full wp-image-232" /></a><p class="wp-caption-text">Flamer Class. The flaming vortex demo</p></div>
<p>This next demo is an example of throwing a movieclip/sprite at the Flamer class. Don&#8217;t laugh, I quite literally spent 10 minutes putting the anims together then threw them at the Flamer. Click on Flash to change colour and object.<br />
<div id="attachment_233" class="wp-caption aligncenter" style="width: 360px"><a href="http://www.swingpantsflash.com/flamer/FlaTestFlamer.html"><img src="http://flashlabs.files.wordpress.com/2010/02/flaming_car.jpg?w=655" alt="Flaming Car" title="flaming_car"   class="size-full wp-image-233" /></a><p class="wp-caption-text">Flamer Class Demo. Help, my car is on fire</p></div></p>
<p>Lots of fun can be had by throwing different animations at the Flamer. Control some with your mouse/keys. Become a twisted fire starter.</p>
<p>The Flamer class could do with a few more features, but is a pretty good start. The speed is pretty good, I&#8217;m happy to receive any improvement recommendations.</p>
<p>I have packed the Flamer class and demos up in a zip if you want to have a play:<br />
DOWNLOAD: <a href="http://www.swingpantsflash.com/flamer/FlamerClass.zip">Flamer Class and Demos</a></p>
<p>DEMO #1: <a href="http://www.swingpantsflash.com/flamer/Flamer.html">Flaming Vortex</a><br />
DEMO #2: <a href="http://www.swingpantsflash.com/flamer/FlaTestFlamer.html">Burning Movieclips</a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/flashlabs.wordpress.com/230/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/flashlabs.wordpress.com/230/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/flashlabs.wordpress.com/230/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/flashlabs.wordpress.com/230/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/flashlabs.wordpress.com/230/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/flashlabs.wordpress.com/230/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/flashlabs.wordpress.com/230/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/flashlabs.wordpress.com/230/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/flashlabs.wordpress.com/230/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/flashlabs.wordpress.com/230/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/flashlabs.wordpress.com/230/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/flashlabs.wordpress.com/230/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/flashlabs.wordpress.com/230/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/flashlabs.wordpress.com/230/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=swingpants.com&#038;blog=5703708&#038;post=230&#038;subd=flashlabs&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://swingpants.com/2010/02/25/as3-flamer-class-setting-things-on-fire/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/1cebf29489913586137c5bdad0414c88?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">swingpants</media:title>
		</media:content>

		<media:content url="http://flashlabs.files.wordpress.com/2010/02/flaming_sun.jpg" medium="image">
			<media:title type="html">flaming_sun</media:title>
		</media:content>

		<media:content url="http://flashlabs.files.wordpress.com/2010/02/flaming_car.jpg" medium="image">
			<media:title type="html">flaming_car</media:title>
		</media:content>
	</item>
		<item>
		<title>Ranged Numbers. Non-Linear response curves for Sliders and TouchPads.</title>
		<link>http://swingpants.com/2009/11/18/ranged-numbers-non-linear-response-curves-for-sliders-and-touchpads/</link>
		<comments>http://swingpants.com/2009/11/18/ranged-numbers-non-linear-response-curves-for-sliders-and-touchpads/#comments</comments>
		<pubDate>Wed, 18 Nov 2009 09:31:04 +0000</pubDate>
		<dc:creator>swingpants</dc:creator>
				<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[as3]]></category>
		<category><![CDATA[Code Snippet]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[exponential]]></category>
		<category><![CDATA[non-linear]]></category>
		<category><![CDATA[response curves]]></category>
		<category><![CDATA[slider]]></category>
		<category><![CDATA[snippet]]></category>
		<category><![CDATA[touchpad]]></category>

		<guid isPermaLink="false">http://swingpants.com/?p=170</guid>
		<description><![CDATA[I&#8217;ve been building a few apps recently and have found that a linear response over a range of numbers has been a poor solution. I needed to be able to apply different response curves to user interface components (knobs, sliders, touchpads) as well as on screen display objects, characters, etc. I needed a good name [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=swingpants.com&#038;blog=5703708&#038;post=170&#038;subd=flashlabs&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been building a few apps recently and have found that a linear response over a range of numbers has been a poor solution. I needed to be able to apply different response curves to user interface components (knobs, sliders, touchpads) as well as on screen display objects, characters, etc.</p>
<p>I needed a good name for the class, but instead came up with the pretty poor moniker &#8216;RangedNumbers&#8217;. I&#8217;ll change it when I come up with a better one. </p>
<p>So the idea is, a &#8216;ranged number&#8217; is instantiated, initialised for the required range then a simple call with a value in that range will result with a percentage that can be applied to the intended recipient accordingly. I realised that an inverted curve would be handy too, so have added that functionality.</p>
<p>I have included exponential and powered curves as well as linear. </p>
<p>So to use the ranged number system:</p>
<blockquote><p>
//Instantiate and Initialise<br />
var ranged_num:RangedNumber=new RangedNumber(min,max)<br />
//Where min is the minumum number in the range and max is the maximum</p>
<p>//In use:<br />
trace ( ranged_num.calculatedRangeNumber(value, curve_type, inverted) )<br />
//Where:<br />
// value is the value within the range to be calculated<br />
// curve_type is the type of response curve.<br />
//    Curve types: RangedNumber.LINEAR, RangedNumber.EXPONENTIAL, RangedNumber.POWERED<br />
// inverted is a boolean flag. True if the inverted response is required</p>
</blockquote>
<p>I have put together two demos to show the RangedNumber class in action:</p>
<p>Firstly using a Slider component. Grab the slider and watch how the markers are represented on all the displayed response curves.</p>
<div id="attachment_175" class="wp-caption aligncenter" style="width: 460px"><a href="http://swingpantsflash.com/range_number/ranged_number_slider.html"><img src="http://flashlabs.files.wordpress.com/2009/11/ranged_numbers_slider.jpg?w=655" alt="" title="Ranged Numbers: Slider Demo"   class="size-full wp-image-175" /></a><p class="wp-caption-text">A demonstration of how to implement non-linear response curves</p></div>
<p>Now here is the same demo but this time using a TouchPad. Click on the touchpad to engage. In this demo horizontal movement controls the standard curves and vertical the inverted.<br />
<div id="attachment_176" class="wp-caption aligncenter" style="width: 460px"><a href="http://swingpantsflash.com/range_number/ranged_number_touchpad.html"><img src="http://flashlabs.files.wordpress.com/2009/11/ranged_numbers_touchpad.jpg?w=655" alt="" title="Ranged Numbers: Touchpad Demo"   class="size-full wp-image-176" /></a><p class="wp-caption-text">How to implement non-linear response curves with a touchpad</p></div></p>
<p>The source for these demos and the Ranged Number class can be found here: <a href="http://swingpantsflash.com/range_number/number_ranged_demos.zip">Ranged Number ZIP</a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/flashlabs.wordpress.com/170/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/flashlabs.wordpress.com/170/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/flashlabs.wordpress.com/170/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/flashlabs.wordpress.com/170/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/flashlabs.wordpress.com/170/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/flashlabs.wordpress.com/170/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/flashlabs.wordpress.com/170/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/flashlabs.wordpress.com/170/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/flashlabs.wordpress.com/170/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/flashlabs.wordpress.com/170/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/flashlabs.wordpress.com/170/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/flashlabs.wordpress.com/170/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/flashlabs.wordpress.com/170/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/flashlabs.wordpress.com/170/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=swingpants.com&#038;blog=5703708&#038;post=170&#038;subd=flashlabs&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://swingpants.com/2009/11/18/ranged-numbers-non-linear-response-curves-for-sliders-and-touchpads/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/1cebf29489913586137c5bdad0414c88?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">swingpants</media:title>
		</media:content>

		<media:content url="http://flashlabs.files.wordpress.com/2009/11/ranged_numbers_slider.jpg" medium="image">
			<media:title type="html">Ranged Numbers: Slider Demo</media:title>
		</media:content>

		<media:content url="http://flashlabs.files.wordpress.com/2009/11/ranged_numbers_touchpad.jpg" medium="image">
			<media:title type="html">Ranged Numbers: Touchpad Demo</media:title>
		</media:content>
	</item>
		<item>
		<title>Gradients and masks in actionscript</title>
		<link>http://swingpants.com/2009/04/30/gradients-and-masks-in-actionscript/</link>
		<comments>http://swingpants.com/2009/04/30/gradients-and-masks-in-actionscript/#comments</comments>
		<pubDate>Thu, 30 Apr 2009 09:24:55 +0000</pubDate>
		<dc:creator>swingpants</dc:creator>
				<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[Code Snippet]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Alpha]]></category>
		<category><![CDATA[AS2]]></category>
		<category><![CDATA[as3]]></category>
		<category><![CDATA[blendMode]]></category>
		<category><![CDATA[cacheAsBitmap]]></category>
		<category><![CDATA[Gradient]]></category>
		<category><![CDATA[Layer]]></category>
		<category><![CDATA[Masks]]></category>

		<guid isPermaLink="false">http://swingpants.com/?p=124</guid>
		<description><![CDATA[Quick post to describe how to apply a gradient mask to a display object in AS3 (&#38; AS2). #1 Put your gradient mask on the stage &#8211; lets call it gmask #2 Put the object to be masked on the stage. #3 Set both the mask and the &#8216;object to be masked&#8217; to cacheAsBitmap #4 [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=swingpants.com&#038;blog=5703708&#038;post=124&#038;subd=flashlabs&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Quick post to describe how to apply a gradient mask to a display object in AS3 (&amp; AS2).</p>
<p><strong>#1</strong> Put your gradient mask on the stage &#8211; lets call it gmask<br />
<strong>#2</strong> Put the object to be masked on the stage.<br />
<strong>#3</strong> Set both the mask and the &#8216;object to be masked&#8217; to cacheAsBitmap<br />
<strong>#4</strong> Apply the mask to the object</p>
<p><strong>In #AS3</strong><br />
  <em>gmask.cacheAsBitmap = true<br />
  obj_to_be_masked.cacheAsBitmap = true<br />
  obj_to_be_masked.mask =gmask</em></p>
<p><strong>In #AS2</strong><br />
  <em>gmask.cacheAsBitmap = true<br />
  obj_to_be_masked.cacheAsBitmap = true<br />
  obj_to_be_masked.setMask(mask)</em></p>
<p>It really is as simple as this.<br />
<strong><em>Note to designers</em></strong>: Gradient masks can&#8217;t obviously be done directly from the IDE (see below), but these  three lines can be placed on the timeline if so desired.</p>
<p>To put a gradient mask on dynamic text you will need to place the text within a container (movieclip/sprite) and mask that. </p>
<p>The object to be masked can also contain animation, text or video though this will require a bit more processor as the bitmap will need to redraw on each frame. Often this is well worth it as the effect can be great.</p>
<p><strong>There is a way of achieving this same effect with no code at all and here it is:</strong></p>
<p><strong>#1</strong> Put your gradient mask on the stage &#8211; lets call it gmask<br />
<strong>#2</strong> Put the object to be masked on the stage.<br />
<strong>#3</strong> Put both of these items into a movieclip/sprite (the container) making sure the mask is on a layer above the object<br />
<strong>#4</strong> Give the mask a blend mode of &#8216;Alpha&#8217;<br />
<strong>#5</strong> Give the &#8216;container&#8217; a blend mode of &#8216;Layer&#8217;</p>
<p>And there you have it. Gradient masking achieved with blendModes and absolutely no code.</p>
<p><strong>Benchmarking the two gradient mask methods</strong>:<br />
I used each method to mask an embedded video &#8211; then placed an instance on the display list and ran at varying frame rates.<br />
Initially there was negligible difference between the code and blend mask methods. I ramped up the FPS to 120 and put four instances of the video on screen. With this set up the code method (cacheAsBitmap) was up to <strong>4%</strong> faster than the blendMode Mask method.</p>
<p>Conclusion: The code method is faster but very marginally. If you are desperate for a bit of extra juice go with code.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/flashlabs.wordpress.com/124/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/flashlabs.wordpress.com/124/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/flashlabs.wordpress.com/124/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/flashlabs.wordpress.com/124/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/flashlabs.wordpress.com/124/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/flashlabs.wordpress.com/124/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/flashlabs.wordpress.com/124/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/flashlabs.wordpress.com/124/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/flashlabs.wordpress.com/124/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/flashlabs.wordpress.com/124/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/flashlabs.wordpress.com/124/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/flashlabs.wordpress.com/124/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/flashlabs.wordpress.com/124/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/flashlabs.wordpress.com/124/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=swingpants.com&#038;blog=5703708&#038;post=124&#038;subd=flashlabs&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://swingpants.com/2009/04/30/gradients-and-masks-in-actionscript/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/1cebf29489913586137c5bdad0414c88?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">swingpants</media:title>
		</media:content>
	</item>
		<item>
		<title>Collision Detection and Bounce Calculation using Colour Maps (part 1)</title>
		<link>http://swingpants.com/2009/03/16/collision-detection-and-bounce-calculation-using-colour-maps-part-1/</link>
		<comments>http://swingpants.com/2009/03/16/collision-detection-and-bounce-calculation-using-colour-maps-part-1/#comments</comments>
		<pubDate>Mon, 16 Mar 2009 23:21:46 +0000</pubDate>
		<dc:creator>swingpants</dc:creator>
				<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[Flash Games]]></category>
		<category><![CDATA[as3]]></category>
		<category><![CDATA[bounce]]></category>
		<category><![CDATA[collision detection]]></category>
		<category><![CDATA[Color Maps]]></category>
		<category><![CDATA[Colour Maps]]></category>
		<category><![CDATA[getColorBounds]]></category>
		<category><![CDATA[physics]]></category>

		<guid isPermaLink="false">http://flashlabs.wordpress.com/?p=95</guid>
		<description><![CDATA[Here is a method I developed for a game I produced last year. I had three days, and a top down ball bouncing game with multiple levels to render. Initially I thought I&#8217;d use box2D, zero out the gravity, and then build the scenes as box2D models. This looked like taking far too long to [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=swingpants.com&#038;blog=5703708&#038;post=95&#038;subd=flashlabs&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Here is a method I developed for a game I produced last year. I had three days, and a top down ball bouncing game with multiple levels to render. Initially I thought I&#8217;d use box2D, zero out the gravity, and then build the scenes as box2D models. This looked like taking far too long to create so I needed a quick fire solution. Then it dawned on me: Colour Maps (or to you chaps over the pond Color Maps).</p>
<p>Now Colour Maps are nothing new, but my idea adds to the technique. </p>
<p>Colour Mapping means you can build a map of your game scene with various colours representing the walls and objects. To test whether you have collided with an object, instead of numerous intersect calculations you can just test the colour of the pixel(s) underneath your character.</p>
<p>I realised that if I added a sprite graphic for my ball to the colour map using the Blend Mode &#8216;add&#8217; then I could see collision. So if I make all the static scene furniture red (0xFF0000) and the ball sprite green (0x00FF00), when they overlap the intersection area becomes yellow.<br />
<div id="attachment_99" class="wp-caption aligncenter" style="width: 460px"><img src="http://flashlabs.files.wordpress.com/2009/03/3steps_to_blendmode.jpg?w=655" alt="Blend Mode collision detection" title="3steps_to_blendmode"   class="size-full wp-image-99" /><p class="wp-caption-text">Blend Mode collision detection</p></div><br />
So how does this help? I calculated that by using getColorBounds on the intersect yellow, I can draw a line from the centre of the rectangle to the centre of the ball. This now becomes the reflection point. I can reflect the angle of bounce against this to give a very accurate result.</p>
<p><div id="attachment_100" class="wp-caption aligncenter" style="width: 460px"><img src="http://flashlabs.files.wordpress.com/2009/03/3steps_to_reflectangle.jpg?w=655" alt="Calculate the angle of reflection" title="3steps_to_reflectangle"   class="size-full wp-image-100" /><p class="wp-caption-text">Calculate the angle of reflection</p></div><br />
OK. So the method will bounce from a partial intersection &#8211; but this means that we are calculating the bounce from a ball embedded within the border/scene furniture. I have improved this by calculating the ball path using a recursive method. The function backtracks to where the edge of the ball touches the furniture and then recalculates the path for the remaining distance to be travelled on the current iteration.<br />
<div id="attachment_101" class="wp-caption aligncenter" style="width: 460px"><a href="http://www.swingpantsflash.com/colourmap/ColourMapTest.html"><img src="http://flashlabs.files.wordpress.com/2009/03/ball_and_blocks.jpg?w=655" alt="The Colour Map Bounce system in action" title="ball_and_blocks"   class="size-full wp-image-101" /></a><p class="wp-caption-text">The Colour Map Bounce system in action</p></div></p>
<p>Here is a link to a quick demo of the method in action. <a href="http://www.swingpantsflash.com/colourmap/ColourMapTest.html">Demo</a></p>
<p>Source and example usage is to follow in part 2.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/flashlabs.wordpress.com/95/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/flashlabs.wordpress.com/95/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/flashlabs.wordpress.com/95/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/flashlabs.wordpress.com/95/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/flashlabs.wordpress.com/95/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/flashlabs.wordpress.com/95/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/flashlabs.wordpress.com/95/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/flashlabs.wordpress.com/95/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/flashlabs.wordpress.com/95/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/flashlabs.wordpress.com/95/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/flashlabs.wordpress.com/95/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/flashlabs.wordpress.com/95/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/flashlabs.wordpress.com/95/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/flashlabs.wordpress.com/95/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=swingpants.com&#038;blog=5703708&#038;post=95&#038;subd=flashlabs&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://swingpants.com/2009/03/16/collision-detection-and-bounce-calculation-using-colour-maps-part-1/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/1cebf29489913586137c5bdad0414c88?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">swingpants</media:title>
		</media:content>

		<media:content url="http://flashlabs.files.wordpress.com/2009/03/3steps_to_blendmode.jpg" medium="image">
			<media:title type="html">3steps_to_blendmode</media:title>
		</media:content>

		<media:content url="http://flashlabs.files.wordpress.com/2009/03/3steps_to_reflectangle.jpg" medium="image">
			<media:title type="html">3steps_to_reflectangle</media:title>
		</media:content>

		<media:content url="http://flashlabs.files.wordpress.com/2009/03/ball_and_blocks.jpg" medium="image">
			<media:title type="html">ball_and_blocks</media:title>
		</media:content>
	</item>
		<item>
		<title>Fastest Way to Copy An Array: Concat() or Slice(0)</title>
		<link>http://swingpants.com/2009/03/12/fastest-way-to-copy-an-array-concat-or-slice0/</link>
		<comments>http://swingpants.com/2009/03/12/fastest-way-to-copy-an-array-concat-or-slice0/#comments</comments>
		<pubDate>Thu, 12 Mar 2009 00:54:48 +0000</pubDate>
		<dc:creator>swingpants</dc:creator>
				<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[as3]]></category>
		<category><![CDATA[Benchtesting]]></category>
		<category><![CDATA[Code Snippet]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Arrays]]></category>
		<category><![CDATA[benchtest]]></category>
		<category><![CDATA[concatenate]]></category>
		<category><![CDATA[slice]]></category>

		<guid isPermaLink="false">http://flashlabs.wordpress.com/?p=85</guid>
		<description><![CDATA[What is the fastest way to copy an array? Concat or Slice? There is only one way to find out. FIGHT! OK, so we can dismiss any kind of for-loop &#8211; far too slow, so that leaves us with: 1) array.concat() &#8211; i.e. concatenate an array onto nothing and deliver that as a new array. [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=swingpants.com&#038;blog=5703708&#038;post=85&#038;subd=flashlabs&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>What is the fastest way to copy an array? Concat or Slice? There is only one way to find out. FIGHT!</p>
<p>OK, so we can dismiss any kind of for-loop &#8211; far too slow, so that leaves us with:<br />
    1)   array.concat() &#8211; i.e. concatenate an array onto nothing and deliver that as a new array.<br />
    2)   array.slice(0) &#8211; i.e. return a new array consisting of all of the elements of the old array &#8211; from 0 till the end (up to a max of 16777215)</p>
<p>I&#8217;ve set up an array with a cool 1million entries (ok, it is not big, and it is not clever so it certainly isn&#8217;t cool). I need to copy this. The following code executes each method once on every iteration. It keeps a running total and records the average time each takes. I&#8217;ve limited the code to 100 iterations.</p>
<blockquote>
<p>package<br />
{<br />
	import flash.display.Sprite;<br />
	import flash.events.Event;<br />
	import flash.events.MouseEvent;<br />
	import flash.text.TextField;<br />
    import flash.utils.*;<br />
	public class TestConcat extends Sprite<br />
	{<br />
		private var iteration_count:int=0<br />
		private var concat_total:int=0<br />
		private var slice_total:int=0<br />
		private var clone_total:int=0<br />
		private var tf:TextField = new TextField()<br />
		private var test_array:Array = [];</p>
<p>		public function TestConcat():void<br />
		{<br />
			tf.x = tf.y = 100; tf.width = 600;<br />
			addChild(tf)</p>
<p>			//Set up array to copy<br />
			for(var i:int = 0; i &lt; 1000000; i++) test_array.push(i);<br />
			//Mouse click to rerun test<br />
			stage.addEventListener(MouseEvent.CLICK, go);<br />
			//First run<br />
			go()<br />
		}</p>
<p>		private function go(e:Event = null):void<br />
		{<br />
			iteration_count=concat_total=slice_total=clone_total=0<br />
			addEventListener(Event.ENTER_FRAME, iterate)<br />
		}</p>
<p>		//Loop through tests<br />
		private function iterate(e:Event=null):void<br />
		{<br />
			concat_total +=testConcat()<br />
			slice_total += testSlice()<br />
			clone_total += testByteArrayClone()<br />
			iteration_count++<br />
			tf.text = &quot;Av. Concat time=&quot; + (concat_total / iteration_count)<br />
					+ &quot;ms  Av. Slice time=&quot; + (slice_total / iteration_count)<br />
					+ &quot;ms  Av. BA Clone time=&quot; + (clone_total / iteration_count) + &quot;ms&quot;;<br />
			if(iteration_count&lt;99) removeEventListener(Event.ENTER_FRAME,iterate)<br />
		}</p>
<p>		//test array slice<br />
		private function testSlice():int<br />
		{<br />
			var time_slice_start:Number = getTimer();<br />
			var slice_copy:Array = test_array.slice(0);<br />
			return getTimer()-time_slice_start<br />
		}</p>
<p>		//test array concat<br />
		private function testConcat():int<br />
		{<br />
			var time_concat_start:Number = getTimer();<br />
			var concat_copy:Array = test_array.concat();<br />
			return getTimer()-time_concat_start<br />
		}</p>
<p>		//test BA Clone method<br />
		private function testByteArrayClone():int<br />
		{<br />
			var time_concat_start:Number = getTimer();<br />
			var concat_copy:Array = clone(test_array);<br />
			return getTimer()-time_concat_start<br />
		}</p>
<p>		//Clone method for Deep Objects(via Bruno)<br />
		private function clone(source:Object):*<br />
		{<br />
			var myBA:ByteArray = new ByteArray();<br />
			myBA.writeObject(source);<br />
			myBA.position = 0;<br />
			return(myBA.readObject());<br />
		}<br />
	}<br />
}</p>
</blockquote>
<p>On my laptop I&#8217;m clocking the <strong>concat at 14ms</strong> and the <strong>slice at over 29ms</strong>. </p>
<p>So a conclusive result. concat is twice the speed (with large arrays &#8211; the difference diminishes considerably with smaller arrays)</p>
<p>Give the code a few run throughs and see what you get. Let me know if your results are markedly different.</p>
<p>UPDATED:<br />
I have updated the code and added a swf to try out <a href="http://www.swingpantsflash.com/array_copy/">here</a> and the source code <a href="http://www.swingpantsflash.com/array_copy/TestConcat.as">here</a><br />
<div id="attachment_350" class="wp-caption aligncenter" style="width: 460px"><a href="http://flashlabs.files.wordpress.com/2009/03/copy_test.jpg"><img src="http://flashlabs.files.wordpress.com/2009/03/copy_test.jpg?w=655" alt="Fastest way to copy an array" title="copy_test"   class="size-full wp-image-350" /></a><p class="wp-caption-text">Test the array copy for yourself</p></div><br />
I&#8217;ve also added in a test for the Byte Array Clone method suggested by Bruno (see his comments below). This method seems a great one for copying &#8216;deep&#8217; arrays &#8211; arrays of complex objects (arrays, objects or other types). In this context and test (copying shallow arrays) the instantiation, writing and reading adds too much overhead. I&#8217;ll need to test this in a useful context: with deep arrays.</p>
<p>Demo: <a href="http://www.swingpantsflash.com/array_copy/">Array Copy test</a><br />
Source: <a href="http://www.swingpantsflash.com/array_copy/TestConcat.as">Source.as</a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/flashlabs.wordpress.com/85/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/flashlabs.wordpress.com/85/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/flashlabs.wordpress.com/85/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/flashlabs.wordpress.com/85/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/flashlabs.wordpress.com/85/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/flashlabs.wordpress.com/85/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/flashlabs.wordpress.com/85/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/flashlabs.wordpress.com/85/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/flashlabs.wordpress.com/85/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/flashlabs.wordpress.com/85/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/flashlabs.wordpress.com/85/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/flashlabs.wordpress.com/85/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/flashlabs.wordpress.com/85/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/flashlabs.wordpress.com/85/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=swingpants.com&#038;blog=5703708&#038;post=85&#038;subd=flashlabs&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://swingpants.com/2009/03/12/fastest-way-to-copy-an-array-concat-or-slice0/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/1cebf29489913586137c5bdad0414c88?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">swingpants</media:title>
		</media:content>

		<media:content url="http://flashlabs.files.wordpress.com/2009/03/copy_test.jpg" medium="image">
			<media:title type="html">copy_test</media:title>
		</media:content>
	</item>
		<item>
		<title>Simple Sideways-Scrolling Carousel</title>
		<link>http://swingpants.com/2009/03/09/simple-sideways-scrolling-carousel/</link>
		<comments>http://swingpants.com/2009/03/09/simple-sideways-scrolling-carousel/#comments</comments>
		<pubDate>Mon, 09 Mar 2009 22:53:57 +0000</pubDate>
		<dc:creator>swingpants</dc:creator>
				<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[as3]]></category>
		<category><![CDATA[BlurFilter]]></category>
		<category><![CDATA[carousel]]></category>
		<category><![CDATA[scrolling menu]]></category>

		<guid isPermaLink="false">http://flashlabs.wordpress.com/?p=62</guid>
		<description><![CDATA[Not another carousel! Well yes. Posting this for storage really, the only real new thing added here is the method for scaling up the focus object. On every enter frame event, each item in the list is checked to see if it is in the focus range (the centre of the visible area and a [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=swingpants.com&#038;blog=5703708&#038;post=62&#038;subd=flashlabs&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Not another carousel! Well yes. Posting this for storage really, the only real new thing added here is the method for scaling up the focus object. </p>
<p>On every enter frame event, each item in the list is checked to see if it is in the focus range (the centre of the visible area and a selected distance either side). If so, then the absolute position is calculated against the centre point. Now the scale and blur are added accordingly.</p>
<p>The class is encapsulated and easy to instantiate:</p>
<blockquote><p>
    _carousel=new Carousel()<br />
    _carousel.init(area_width, area_height, range_pc, margin, min_scale, item_array, blur_on, blur_amt)
</p></blockquote>
<p>     area_width  &#8211;  width of the visible carousel area<br />
     area_height  &#8211;  height of the visible carousel area<br />
     range_pc  &#8211;  the percent of area either side of the centre point to use as the focus range (0.1 &#8211; 1.0)<br />
     margin  &#8211;  the margin bordering each item on the carousel<br />
     min_scale  &#8211;  scale that items outside of the range reduce to (0.1 &#8211; 1.0)<br />
     item_array  &#8211;  array of sprites/movieclips buttons<br />
     blur_on  &#8211;  true=blur on, false=blur off<br />
     blur_amt  &#8211;  amount of blur to apply to the items out of range</p>
<div id="attachment_176" class="wp-caption aligncenter" style="width: 460px"><a href="http://www.swingpantsflash.com/carousel/test_carousel.html"><img src="http://flashlabs.files.wordpress.com/2009/03/carousel.jpg?w=655" alt="Simple AS3 Carousel" title="Simple Carousel"  class="size-full wp-image-176" /></a><p class="wp-caption-text">AS3 Simple Carousel</p></div>
<p>A demo has been set up and can be seen <a href="http://www.swingpantsflash.com/carousel/test_carousel.html">here</a></p>
<p>The items in the carousel can be anything from simple pictures or buttons to more complex interactive movieclips.</p>
<p>There are three main interactive methods in the Carousel class:<br />
    scrollLeft() / scrollRight()   &#8211;   Increment along the list<br />
    gotoItem(index)   &#8211;   scroll to the selected item (where index is the item array reference for the item)</p>
<p>I have only added the functions required for my current project, but it should be very simple to add extra functionality such as looped content, reflections, etc.</p>
<p><a href="http://www.swingpantsflash.com/carousel/carousel.zip">Source files</a><br />
<a href="http://www.swingpantsflash.com/carousel/test_carousel.html">Demo</a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/flashlabs.wordpress.com/62/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/flashlabs.wordpress.com/62/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/flashlabs.wordpress.com/62/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/flashlabs.wordpress.com/62/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/flashlabs.wordpress.com/62/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/flashlabs.wordpress.com/62/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/flashlabs.wordpress.com/62/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/flashlabs.wordpress.com/62/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/flashlabs.wordpress.com/62/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/flashlabs.wordpress.com/62/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/flashlabs.wordpress.com/62/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/flashlabs.wordpress.com/62/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/flashlabs.wordpress.com/62/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/flashlabs.wordpress.com/62/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=swingpants.com&#038;blog=5703708&#038;post=62&#038;subd=flashlabs&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://swingpants.com/2009/03/09/simple-sideways-scrolling-carousel/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/1cebf29489913586137c5bdad0414c88?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">swingpants</media:title>
		</media:content>

		<media:content url="http://flashlabs.files.wordpress.com/2009/03/carousel.jpg" medium="image">
			<media:title type="html">Simple Carousel</media:title>
		</media:content>
	</item>
		<item>
		<title>Keeping Lip-Sync in Sync</title>
		<link>http://swingpants.com/2009/03/02/keeping-lip-sync-in-sync/</link>
		<comments>http://swingpants.com/2009/03/02/keeping-lip-sync-in-sync/#comments</comments>
		<pubDate>Mon, 02 Mar 2009 23:03:14 +0000</pubDate>
		<dc:creator>swingpants</dc:creator>
				<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[as3]]></category>
		<category><![CDATA[Code Snippet]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[lip synchronisation]]></category>
		<category><![CDATA[lip sync]]></category>

		<guid isPermaLink="false">http://flashlabs.wordpress.com/?p=48</guid>
		<description><![CDATA[I&#8217;m currently working on a project where I have been handed a lot of movieclips with animated mouths to be sync-ed with spoken audio triggered from the library. Each sentence had been put together in a separate movieclip. Obviously this method exposes the frame rate on slow computers leading to loss of lip-synchronisation. I was [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=swingpants.com&#038;blog=5703708&#038;post=48&#038;subd=flashlabs&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m currently working on a project where I have been handed a lot of movieclips with animated mouths to be sync-ed with spoken audio triggered from the library. Each sentence had been put together in a separate movieclip. Obviously this method exposes the frame rate on slow computers leading to loss of lip-synchronisation. I was told to go through all of the moveiclips and add the audio to the timeline &#8211; a very inelegant solution that would take a lot of time with poor results.</p>
<p>I came up with a simple solution that basically just pushes the playhead forward everytime it drops out of sync. I use the frame rate and getTimer to work out where the playhead should be on each iteration. If the playhead drops then it is moved to the position it should be at. </p>
<p>Very simple &#8211; saved me a lot of time.</p>
<blockquote>
<p>private const FPS:int = 30//Current frame rate<br />
private const ERROR_MARGIN:int=1<br />
private var start_time:int<br />
private var last_frame:int=0//Keep track of last frame to see if mc has stopped playing<br />
private var loop_started:Boolean=false</p>
<p>public function sayPhrase(phrase:String) : void<br />
	 {<br />
		  mouth_mc.mouth.gotoAndPlay(phrase)<br />
		  last_frame=0 //reset last frame<br />
		  loop_started=false<br />
		  start_time = getTimer()<br />
		  addEventListener(Event.ENTER_FRAME,lipSyncTracking)<br />
	 }</p>
<p>private function lipSyncTracking(e:Event = null):void<br />
	 {<br />
		  if (mouth_mc.mouth)<br />
			   {<br />
				    var cframe:int = mouth_mc.mouth.currentFrame //get the current frame<br />
				    var ctime:int = getTimer() //current time<br />
				    var time_expired:Number = (ctime &#8211; start_time )<br />
				    var target_frame:int = Math.floor((FPS/1000)*time_expired)<br />
				    if(target_frame&gt;1 &amp;&amp; cframe&gt;1)loop_started=true //need to check for looping<br />
				    if (cframe == last_frame || (cframe==1 &amp;&amp; loop_started))<br />
					     {//MC must have stopped or looped back to beginning<br />
						      removeEventListener(Event.ENTER_FRAME,lipSyncTracking)<br />
					     }<br />
					     else<br />
					     {//If frame is out by the margin of error then correct<br />
					  	      if (target_frame &gt;= cframe + ERROR_MARGIN)<br />
							      {<br />
								       mouth_mc.mouth.gotoAndPlay(target_frame)<br />
							      }<br />
					     }</p>
<p>				    last_frame = cframe<br />
			   }<br />
	  }</p>
</blockquote>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/flashlabs.wordpress.com/48/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/flashlabs.wordpress.com/48/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/flashlabs.wordpress.com/48/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/flashlabs.wordpress.com/48/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/flashlabs.wordpress.com/48/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/flashlabs.wordpress.com/48/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/flashlabs.wordpress.com/48/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/flashlabs.wordpress.com/48/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/flashlabs.wordpress.com/48/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/flashlabs.wordpress.com/48/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/flashlabs.wordpress.com/48/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/flashlabs.wordpress.com/48/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/flashlabs.wordpress.com/48/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/flashlabs.wordpress.com/48/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=swingpants.com&#038;blog=5703708&#038;post=48&#038;subd=flashlabs&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://swingpants.com/2009/03/02/keeping-lip-sync-in-sync/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/1cebf29489913586137c5bdad0414c88?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">swingpants</media:title>
		</media:content>
	</item>
		<item>
		<title>Jonga3D: Testing Jiglib with Away3D</title>
		<link>http://swingpants.com/2009/02/18/jonga3d-testing-jiglib-with-away3d/</link>
		<comments>http://swingpants.com/2009/02/18/jonga3d-testing-jiglib-with-away3d/#comments</comments>
		<pubDate>Wed, 18 Feb 2009 00:20:25 +0000</pubDate>
		<dc:creator>swingpants</dc:creator>
				<category><![CDATA[3D]]></category>
		<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[as3]]></category>
		<category><![CDATA[away3d]]></category>
		<category><![CDATA[jiglib]]></category>
		<category><![CDATA[physics]]></category>
		<category><![CDATA[rigid bodies]]></category>

		<guid isPermaLink="false">http://flashlabs.wordpress.com/?p=34</guid>
		<description><![CDATA[I just wanted to test the (newly available in AS3) 3D Physics engine Jiglib. My first test for Box2d was to build a Jenga-style game (Jonga) so I thought I would do the same here. Only a quickie this. I wanted to see if Jiglib would be able to allow &#8216;rigid boides&#8217; in a 3D [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=swingpants.com&#038;blog=5703708&#038;post=34&#038;subd=flashlabs&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I just wanted to test the (newly available in AS3) 3D Physics engine <a href="http://www.jiglibflash.com/" target="_blank">Jiglib</a>.</p>
<p>My first test for Box2d was to build a Jenga-style game (Jonga) so I thought I would do the same here. Only a quickie this. I wanted to see if Jiglib would be able to allow &#8216;rigid boides&#8217; in a 3D environment with good performance. I know we are limited in Flash so was prepared for a few foibles.</p>
<p>Jiglib has been put to great use in other formats so I had high hopes for its Flash port. </p>
<p>Now I have only given the library 3 or 4 hours of play but my one major probelm so far is the frame rate hit. In Jonga3d I have had to reduce the number of blocks to 15 (5 layers &#8211; I wanted 15 (45 blocks)) and I am still only getting 6fps. I was really hoping for more.</p>
<p>I&#8217;m not complaining too much, a 3d physics engine is a complicated library to pull off &#8211; I&#8217;m just waiting for the day I can build an unfettered 3d environment with a workable physics engine.</p>
<p>Jiglib has a few other problems &#8211; the jiggling static box for example &#8211; but these are understandable for v0.25</p>
<div id="attachment_176" class="wp-caption aligncenter" style="width: 460px"><a href="http://www.swingpantsflash.com/jonga3d/jiglib_away.html"><img src="http://flashlabs.files.wordpress.com/2009/02/jonga3d.jpg?w=655" alt="Jonga3D" title="Jonga3D"  class="size-full wp-image-176" /></a><p class="wp-caption-text">Testing Jiblib: Jonga3D</p></div>
<p>In the test I didn&#8217;t get around to implementing the mouse drag interactions so to see a collapse use the arrow keys to control the sphere &#8211; ram it into the stack.</p>
<p>I am using Jiglib 0.25 &#8211; Away3d version  and Away3D 2.3 <a href="http://www.away3d.com/" target="_blank">(Link)</a><br />
I use <a href="http://www.lostinactionscript.com/blog/index.php/2008/10/06/as3-swf-profiler/" target="_blank">SwfProfiler</a> to track the fps.</p>
<p>Source:<br />
<a href="http://www.swingpantsflash.com/jonga3d/jiglib_away.html" target="_blank">Jonga3D</a><br />
<a href="http://www.swingpantsflash.com/jonga3d/src.zip">Source</a></p>
<p>Thanks to <a href="http://sleepydesign.blogspot.com/2009/01/away3d-jiglib-flash-3d-physics-engine.html" target="_blank">SleepyDesign</a> for getting the ball rolling on this.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/flashlabs.wordpress.com/34/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/flashlabs.wordpress.com/34/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/flashlabs.wordpress.com/34/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/flashlabs.wordpress.com/34/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/flashlabs.wordpress.com/34/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/flashlabs.wordpress.com/34/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/flashlabs.wordpress.com/34/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/flashlabs.wordpress.com/34/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/flashlabs.wordpress.com/34/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/flashlabs.wordpress.com/34/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/flashlabs.wordpress.com/34/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/flashlabs.wordpress.com/34/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/flashlabs.wordpress.com/34/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/flashlabs.wordpress.com/34/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=swingpants.com&#038;blog=5703708&#038;post=34&#038;subd=flashlabs&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://swingpants.com/2009/02/18/jonga3d-testing-jiglib-with-away3d/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/1cebf29489913586137c5bdad0414c88?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">swingpants</media:title>
		</media:content>

		<media:content url="http://flashlabs.files.wordpress.com/2009/02/jonga3d.jpg" medium="image">
			<media:title type="html">Jonga3D</media:title>
		</media:content>
	</item>
		<item>
		<title>Global Game Jam &#8211; The Deep &#8211; Capture Detection</title>
		<link>http://swingpants.com/2009/02/13/global-game-jam-the-deep-capture-detection/</link>
		<comments>http://swingpants.com/2009/02/13/global-game-jam-the-deep-capture-detection/#comments</comments>
		<pubDate>Fri, 13 Feb 2009 01:04:33 +0000</pubDate>
		<dc:creator>swingpants</dc:creator>
				<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[Flash Games]]></category>
		<category><![CDATA[as3]]></category>
		<category><![CDATA[bitmapdata]]></category>
		<category><![CDATA[capture detection]]></category>
		<category><![CDATA[collision detection]]></category>

		<guid isPermaLink="false">http://flashlabs.wordpress.com/?p=18</guid>
		<description><![CDATA[ii) In &#8216;The Deep&#8217; game we have a string membrane that can contort into many shapes both concave and convex. I needed to be able to detect if a &#8216;plankton&#8217; (or many) is within the membrane when the digest mechanism is triggered (the creature&#8217;s orbs touching together). My method was to draw a shape using [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=swingpants.com&#038;blog=5703708&#038;post=18&#038;subd=flashlabs&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>ii)  In <a href="http://globalgamejam.org/games/deep-0">&#8216;The Deep&#8217;</a> game we have a string membrane that can contort into many shapes both concave and convex. I needed to be able to detect if a &#8216;plankton&#8217; (or many) is within the membrane when the digest mechanism is triggered (the creature&#8217;s orbs touching together).</p>
<p>  My method was to draw a shape using the membrane as a template. Fill the shape, then test the plankton to see if their coordinates are over the fill colour.</p>
<div id="attachment_176" class="wp-caption aligncenter" style="width: 460px"><a href="http://www.swingpantsflash.com/thedeep/the_deep.html"><img src="http://flashlabs.files.wordpress.com/2009/02/deep_membrane.jpg?w=655" alt="The Deep: Plankton Capture Detection" title="The Deep: Plankton Capture Detection"  class="size-full wp-image-176" /></a><p class="wp-caption-text">The Deep: Plankton Capture Detection</p></div>
<p>   1)  The creature&#8217;s orbs touch triggering the digest mechanism.</p>
<p>   2)  Using the vertices on the rope chain draw a shape.</p>
<p>   3)  Colour fill the shape.</p>
<p>   4)  Find the extents of the shape. Calculate the bounding box. Now check to see which plankton are within the bounding box. Take the coordinates of each of these plankton and test whether they have the fill colour at that coordinate. If so, then the plankton must be inside and is therefore digested. Points scored.</p>
<p>Again the source of the game can be found <a href="http://www.swingpantsflash.com/thedeep/the_deep_src.zip">here</a>.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/flashlabs.wordpress.com/18/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/flashlabs.wordpress.com/18/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/flashlabs.wordpress.com/18/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/flashlabs.wordpress.com/18/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/flashlabs.wordpress.com/18/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/flashlabs.wordpress.com/18/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/flashlabs.wordpress.com/18/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/flashlabs.wordpress.com/18/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/flashlabs.wordpress.com/18/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/flashlabs.wordpress.com/18/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/flashlabs.wordpress.com/18/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/flashlabs.wordpress.com/18/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/flashlabs.wordpress.com/18/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/flashlabs.wordpress.com/18/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=swingpants.com&#038;blog=5703708&#038;post=18&#038;subd=flashlabs&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://swingpants.com/2009/02/13/global-game-jam-the-deep-capture-detection/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/1cebf29489913586137c5bdad0414c88?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">swingpants</media:title>
		</media:content>

		<media:content url="http://flashlabs.files.wordpress.com/2009/02/deep_membrane.jpg" medium="image">
			<media:title type="html">The Deep: Plankton Capture Detection</media:title>
		</media:content>
	</item>
		<item>
		<title>Global Gam Jam &#8211; The Deep &#8211; How to produce a string in water with AS3</title>
		<link>http://swingpants.com/2009/02/13/global-gam-jam-the-deep-how-to-produce-a-string-in-water-with-as3/</link>
		<comments>http://swingpants.com/2009/02/13/global-gam-jam-the-deep-how-to-produce-a-string-in-water-with-as3/#comments</comments>
		<pubDate>Fri, 13 Feb 2009 00:46:02 +0000</pubDate>
		<dc:creator>swingpants</dc:creator>
				<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[as3]]></category>
		<category><![CDATA[Flash Games]]></category>
		<category><![CDATA[box2d]]></category>
		<category><![CDATA[physics]]></category>
		<category><![CDATA[rope]]></category>
		<category><![CDATA[string]]></category>

		<guid isPermaLink="false">http://flashlabs.wordpress.com/?p=7</guid>
		<description><![CDATA[I attended the Global Game Jam 2009 recently &#8211; forming the team &#8216;Fish Don&#8217;t Flock&#8217;. We produced a game called &#8216;The Deep&#8217; &#8211; in 48 sleepless hours. In the game you get to control a deep sea bio-luminescent creature that is formed from two orbs and a membrane. The creature eats plankton by enveloping it [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=swingpants.com&#038;blog=5703708&#038;post=7&#038;subd=flashlabs&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I attended the <a href="http://globalgamejam.org/games/deep-0">Global Game Jam 2009</a> recently &#8211; forming the team &#8216;Fish Don&#8217;t Flock&#8217;. We produced a game called <a href="http://www.swingpantsflash.com/thedeep/the_deep.html">&#8216;The Deep&#8217;</a> &#8211; in 48 sleepless hours.</p>
<p>In the game you get to control a deep sea bio-luminescent creature that is formed from two orbs and a membrane. The creature eats plankton by enveloping it inside it&#8217;s membrane.</p>
<p>There were a couple of innovations we managed fit into the game. The first I will detail here, the other will be in my next post.</p>
<p>(i) We needed to make the membrane behave like a ribbon under water:</p>
<p>I&#8217;m a huge fan of Box2D, and have done a few AS3 rope experiments. To get the behaviour I wanted, I figured that I could chain together a series of balls within Box2D, turn off the gravity for the top-down underwater view effect. This way I could detect collisions and if all creatures have mass within the physics engine they would interact correctly with the membrane.</p>
<div id="attachment_176" class="wp-caption aligncenter" style="width: 460px"><a href="http://www.swingpantsflash.com/thedeep/the_deep.html"><img src="http://flashlabs.files.wordpress.com/2009/02/deep_chain.jpg?w=655" alt="The Deep: Rope membrane" title="The Deep: Rope membrane"   class="size-full wp-image-176" /></a><p class="wp-caption-text">The Deep: Rope membrane</p></div>
<p>    (1)  Create a chain out of box2D circles.</p>
<p>    (2)  Link the chain circles using revolute joints.</p>
<p>    (3)  Draw membrane/rope by drawing from joint point to joint point and add orb sprite.</p>
<p>    (4)  Remove rendering of circles.</p>
<p>The source is available for this game <a href="http://www.swingpantsflash.com/thedeep/the_deep_src.zip">here</a>.<br />
(Obviously it was a rush job so expect a lot of code imperfection.)</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/flashlabs.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/flashlabs.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/flashlabs.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/flashlabs.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/flashlabs.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/flashlabs.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/flashlabs.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/flashlabs.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/flashlabs.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/flashlabs.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/flashlabs.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/flashlabs.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/flashlabs.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/flashlabs.wordpress.com/7/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=swingpants.com&#038;blog=5703708&#038;post=7&#038;subd=flashlabs&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://swingpants.com/2009/02/13/global-gam-jam-the-deep-how-to-produce-a-string-in-water-with-as3/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/1cebf29489913586137c5bdad0414c88?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">swingpants</media:title>
		</media:content>

		<media:content url="http://flashlabs.files.wordpress.com/2009/02/deep_chain.jpg" medium="image">
			<media:title type="html">The Deep: Rope membrane</media:title>
		</media:content>
	</item>
	</channel>
</rss>
