<?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>ClickPopMedia &#187; Paul</title>
	<atom:link href="http://www.clickpopmedia.com/category/paul/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.clickpopmedia.com</link>
	<description>ClickPopMedia is a great little design and illustration firm.</description>
	<lastBuildDate>Thu, 03 Dec 2009 17:28:23 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>www.NathanRutherford.com</title>
		<link>http://www.clickpopmedia.com/2009/10/08/www-nathanrutherford-com/</link>
		<comments>http://www.clickpopmedia.com/2009/10/08/www-nathanrutherford-com/#comments</comments>
		<pubDate>Fri, 09 Oct 2009 04:54:32 +0000</pubDate>
		<dc:creator>Weese</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Paul]]></category>
		<category><![CDATA[Portfolio]]></category>
		<category><![CDATA[VQ]]></category>
		<category><![CDATA[Weese]]></category>
		<category><![CDATA[Wordpress]]></category>
		<category><![CDATA[web]]></category>
		<category><![CDATA[ClickPopMedia]]></category>
		<category><![CDATA[CPM]]></category>
		<category><![CDATA[Nathan Rutherford]]></category>
		<category><![CDATA[Photography]]></category>
		<category><![CDATA[Photography Websites]]></category>
		<category><![CDATA[Skate Scene]]></category>
		<category><![CDATA[Skateboard]]></category>
		<category><![CDATA[Skateboarding]]></category>
		<category><![CDATA[Skateboarding Website]]></category>
		<category><![CDATA[Snowboard Scene]]></category>
		<category><![CDATA[Snowboarding Website]]></category>
		<category><![CDATA[Surf Scene]]></category>
		<category><![CDATA[UK]]></category>
		<category><![CDATA[UK Skate Photographer]]></category>
		<category><![CDATA[Website]]></category>

		<guid isPermaLink="false">http://www.clickpopmedia.com/?p=584</guid>
		<description><![CDATA[

So www.NathanRutherford.com, ClickPopMedia&#8217;s latest work, has gone live! We&#8217;re really excited about the site. VQ and I have a real love for the Skateboard/Snowboard/Surf scene. And in our travels we&#8217;ve had the privilege to meet some great talents&#8230;
Nathan is no exception, he&#8217;s a young guy from the UK with a lot of skill, both behind [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: center;"><a href="http://www.nathanrutherford.com" target="_blank"><img class="size-large wp-image-585 aligncenter" title="Screen shot 2009-10-09 at 12.40.36 AM" src="http://www.clickpopmedia.com/wp-content/uploads/2009/10/Screen-shot-2009-10-09-at-12.40.36-AM-1024x685.png" alt="Screen shot 2009-10-09 at 12.40.36 AM" width="600" height="401" /></a></p>
<p style="text-align: center;">
<p style="text-align: left;">So <a href="http://www.nathanrutherford.com" target="_blank">www.NathanRutherford.com</a>, ClickPopMedia&#8217;s latest work, has gone live! We&#8217;re really excited about the site. VQ and I have a real love for the Skateboard/Snowboard/Surf scene. And in our travels we&#8217;ve had the privilege to meet some great talents&#8230;</p>
<p style="text-align: left;">Nathan is no exception, he&#8217;s a young guy from the UK with a lot of skill, both behind the camera and on a skateboard. He&#8217;s also become a good friend over the years. Head on over to his site and have a look at some of <em>his</em> work.</p>
<p style="text-align: left;">May God bless you today!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.clickpopmedia.com/2009/10/08/www-nathanrutherford-com/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Collision Detection and Game Design</title>
		<link>http://www.clickpopmedia.com/2008/07/07/collision-detection-and-game-design/</link>
		<comments>http://www.clickpopmedia.com/2008/07/07/collision-detection-and-game-design/#comments</comments>
		<pubDate>Mon, 07 Jul 2008 23:58:21 +0000</pubDate>
		<dc:creator>Paul</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Paul]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Asteroids]]></category>
		<category><![CDATA[Collision Detection]]></category>
		<category><![CDATA[Game Development]]></category>

		<guid isPermaLink="false">http://www.clickpopmedia.com/?p=353</guid>
		<description><![CDATA[

This is the second step in my series of tutorial, the first being Easy Keyboard Controls and Game Design. In this tutorial I will be building on what we already have, adding asteroids and the ability to crash into them.
I&#8217;ve made a few minor changes to the original code and graphics since the last tutorial, [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: center;"><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="550" height="400" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="src" value="http://www.clickpopmedia.com/wp-content/uploads/2008/07/asteroids.swf" /><embed type="application/x-shockwave-flash" width="550" height="400" src="http://www.clickpopmedia.com/wp-content/uploads/2008/07/asteroids.swf"></embed></object></p>
<p style="text-align: left;">
This is the second step in my series of tutorial, the first being <a href="http://www.clickpopmedia.com/2008/06/25/easy-keyboard-controls-and-game-design/">Easy Keyboard Controls and Game Design</a>. In this tutorial I will be building on what we already have, adding asteroids and the ability to crash into them.<br />
<span id="more-353"></span>I&#8217;ve made a few minor changes to the original code and graphics since the last tutorial, but nothing big.</p>
<p>So I create a MovieClip with 3 frames and a different asteroid shape on each frame (you could have more or less if you wanted).  I give the asteroids their own class since each is going to have to move independently.</p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw3">public</span> <span class="kw3">static</span> const LARGE:<span class="kw3">String</span> = <span class="st0">&quot;large&quot;</span>;<br />
<span class="kw3">public</span> <span class="kw3">static</span> const MEDIUM:<span class="kw3">String</span> = <span class="st0">&quot;medium&quot;</span>;<br />
<span class="kw3">public</span> <span class="kw3">static</span> const SMALL:<span class="kw3">String</span> = <span class="st0">&quot;small&quot;</span>;</p>
<p><span class="kw3">public</span> <span class="kw2">var</span> speedX:<span class="kw3">Number</span>;<br />
<span class="kw3">public</span> <span class="kw2">var</span> speedY:<span class="kw3">Number</span>;<br />
<span class="kw3">public</span> <span class="kw2">var</span> <span class="kw3">size</span>:<span class="kw3">String</span>;<br />
&#8230;<br />
<span class="kw3">public</span> <span class="kw2">function</span> Move<span class="br0">&#40;</span><span class="br0">&#41;</span>:<span class="kw3">void</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">this</span>.<span class="me1">x</span> += speedX;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">this</span>.<span class="me1">y</span> += speedY;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span><span class="kw3">this</span>.<span class="me1">x</span> &gt; <span class="nu0">550</span> + <span class="kw3">this</span>.<span class="kw3">width</span>/<span class="nu0">2</span><span class="br0">&#41;</span> <span class="kw3">this</span>.<span class="me1">x</span> = -<span class="kw3">this</span>.<span class="kw3">width</span>/<span class="nu0">2</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span><span class="kw3">this</span>.<span class="me1">x</span> &lt; -<span class="kw3">this</span>.<span class="kw3">width</span>/<span class="nu0">2</span><span class="br0">&#41;</span> <span class="kw3">this</span>.<span class="me1">x</span> = <span class="nu0">550</span> + <span class="kw3">this</span>.<span class="kw3">width</span>/<span class="nu0">2</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span><span class="kw3">this</span>.<span class="me1">y</span> &gt; <span class="nu0">400</span> + <span class="kw3">this</span>.<span class="kw3">height</span>/<span class="nu0">2</span><span class="br0">&#41;</span> <span class="kw3">this</span>.<span class="me1">y</span> = -<span class="kw3">this</span>.<span class="kw3">height</span>/<span class="nu0">2</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span><span class="kw3">this</span>.<span class="me1">y</span> &lt; -<span class="kw3">this</span>.<span class="kw3">height</span>/<span class="nu0">2</span><span class="br0">&#41;</span> <span class="kw3">this</span>.<span class="me1">y</span> = <span class="nu0">400</span> + <span class="kw3">this</span>.<span class="kw3">height</span>/<span class="nu0">2</span>;<br />
<span class="br0">&#125;</span></div>
<p>What I&#8217;ve done here is give my Asteroid class X and Y speed properties that work with the <strong>Move()</strong> method for motion.  I intend to call the <strong>Move()</strong> method for each asteroid in the <strong>MainLoop()</strong> function.</p>
<p>On creation (in the constructor) I want to know if this asteroid is actually a piece of a larger asteroid or not.  If it is I&#8217;m going to be passing it the base asteroid that this one is breaking off of to use it&#8217;s X:Y coordinates and speed, plus to know what size to make the new asteroid.</p>
<p>Now back to the <strong>Main</strong> class (in the <a href="http://www.clickpopmedia.com/2008/06/25/easy-keyboard-controls-and-game-design/">last tutorial</a> I called this the <strong>Asteroids</strong> class but changed it to <strong>Main</strong> since I wanted an <strong>Asteroid</strong> class).</p>
<p>We need to keep track of our asteroids.  To do that we will use an <strong>Array</strong>.  We will add this to the <strong>Main</strong> constructor:</p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw3">public</span> <span class="kw2">function</span> Main<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; dCount = <span class="nu0">0</span>;<br />
&#8230;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="me1">refresh_tf</span>.<span class="me1">addEventListener</span><span class="br0">&#40;</span>MouseEvent.<span class="me1">MOUSE_UP</span>, resetGame<span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; asteroids = <span class="kw2">new</span> <span class="kw3">Array</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">for</span><span class="br0">&#40;</span><span class="kw2">var</span> i:<span class="kw3">int</span> = <span class="nu0">0</span>; i&lt;<span class="nu0">6</span>; i++<span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; asteroids.<span class="kw3">push</span><span class="br0">&#40;</span><span class="kw2">new</span> Asteroid<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">stage</span>.<span class="me1">addChild</span><span class="br0">&#40;</span>asteroids<span class="br0">&#91;</span>i<span class="br0">&#93;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></div>
<p>This is just adding 6 asteroids to the stage and giving a click event to a reset button I decided to add.<br />
<strong>dCount</strong> stands for Death Count with an obvious purpose.</p>
<p>The <strong>resetGame()</strong> function is just resetting all the properties to the default.  I still have to make sure to remove all the asteroids before adding all new ones, but that&#8217;s it.</p>
<p>I moved the contents of the <strong>MainLoop()</strong> from the <a href="http://www.clickpopmedia.com/2008/06/25/easy-keyboard-controls-and-game-design/">previous tutorial</a> into a function called <strong>shipControl()</strong> which is now being called from the new <strong>MainLoop()</strong>;</p>
<p>Our revised <strong>MainLoop()</strong> method is as follows:</p>
<div class="dean_ch" style="white-space: wrap;">protected <span class="kw2">function</span> MainLoop<span class="br0">&#40;</span><span class="kw3">e</span>:TimerEvent<span class="br0">&#41;</span>:<span class="kw3">void</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">for</span><span class="br0">&#40;</span><span class="kw2">var</span> i <span class="kw1">in</span> asteroids<span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; asteroids<span class="br0">&#91;</span>i<span class="br0">&#93;</span>.<span class="me1">Move</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span>dead<span class="br0">&#41;</span><span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; deathTime&#8211;;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span>deathTime &lt;=<span class="nu0">0</span><span class="br0">&#41;</span><span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dead = <span class="kw2">false</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; invincibleTime = <span class="nu0">150</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; invincible = <span class="kw2">true</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; addChild<span class="br0">&#40;</span>ship_mc<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><span class="kw1">else</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; shipControl<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span>!invincible<span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; collisionCheck<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><span class="kw1">else</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span><span class="kw3">e</span>.<span class="me1">currentTarget</span>.<span class="me1">currentCount</span>%<span class="nu0">10</span> == <span class="nu0">0</span><span class="br0">&#41;</span><span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ship_mc.<span class="kw3">visible</span> = <span class="br0">&#40;</span>ship_mc.<span class="kw3">visible</span><span class="br0">&#41;</span>? <span class="kw2">false</span>: <span class="kw2">true</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; invincibleTime&#8211;;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span>invincibleTime &lt;= <span class="nu0">0</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ship_mc.<span class="kw3">visible</span> = <span class="kw2">true</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; invincible = <span class="kw2">false</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></div>
<p>These nested <strong>if..else</strong> statements are here to handle death and resurrection (RIGHT ON!).</p>
<p>So we have our asteroids flying about the screen, but we have yet to make them destroy your ship.  You should notice above the call for to a method call <strong>collisionCheck()</strong>;<br />
Here is that function now:</p>
<div class="dean_ch" style="white-space: wrap;">protected <span class="kw2">function</span> collisionCheck<span class="br0">&#40;</span><span class="br0">&#41;</span>:<span class="kw3">void</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">var</span> p1:Point = <span class="kw2">new</span> Point<span class="br0">&#40;</span>ship_mc.<span class="me1">p1</span>.<span class="me1">x</span>, ship_mc.<span class="me1">p1</span>.<span class="me1">y</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; p1 = ship_mc.<span class="kw3">localToGlobal</span><span class="br0">&#40;</span>p1<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">var</span> p2:Point = <span class="kw2">new</span> Point<span class="br0">&#40;</span>ship_mc.<span class="me1">p2</span>.<span class="me1">x</span>, ship_mc.<span class="me1">p2</span>.<span class="me1">y</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; p2 = ship_mc.<span class="kw3">localToGlobal</span><span class="br0">&#40;</span>p2<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">var</span> p3:Point = <span class="kw2">new</span> Point<span class="br0">&#40;</span>ship_mc.<span class="me1">p3</span>.<span class="me1">x</span>, ship_mc.<span class="me1">p3</span>.<span class="me1">y</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; p3 = ship_mc.<span class="kw3">localToGlobal</span><span class="br0">&#40;</span>p3<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">var</span> p4:Point = <span class="kw2">new</span> Point<span class="br0">&#40;</span>ship_mc.<span class="me1">p4</span>.<span class="me1">x</span>, ship_mc.<span class="me1">p4</span>.<span class="me1">y</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; p4 = ship_mc.<span class="kw3">localToGlobal</span><span class="br0">&#40;</span>p4<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">var</span> p5:Point = <span class="kw2">new</span> Point<span class="br0">&#40;</span>ship_mc.<span class="me1">p5</span>.<span class="me1">x</span>, ship_mc.<span class="me1">p5</span>.<span class="me1">y</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; p5 = ship_mc.<span class="kw3">localToGlobal</span><span class="br0">&#40;</span>p5<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">for</span><span class="br0">&#40;</span><span class="kw2">var</span> i <span class="kw1">in</span> asteroids<span class="br0">&#41;</span><span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span><span class="br0">&#40;</span>asteroids<span class="br0">&#91;</span>i<span class="br0">&#93;</span>.<span class="me1">hitTestPoint</span><span class="br0">&#40;</span>p1.<span class="me1">x</span>, p1.<span class="me1">y</span>, <span class="kw2">true</span><span class="br0">&#41;</span> ||<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; asteroids<span class="br0">&#91;</span>i<span class="br0">&#93;</span>.<span class="me1">hitTestPoint</span><span class="br0">&#40;</span>p2.<span class="me1">x</span>, p2.<span class="me1">y</span>, <span class="kw2">true</span><span class="br0">&#41;</span> ||<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; asteroids<span class="br0">&#91;</span>i<span class="br0">&#93;</span>.<span class="me1">hitTestPoint</span><span class="br0">&#40;</span>p3.<span class="me1">x</span>, p3.<span class="me1">y</span>, <span class="kw2">true</span><span class="br0">&#41;</span> ||<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; asteroids<span class="br0">&#91;</span>i<span class="br0">&#93;</span>.<span class="me1">hitTestPoint</span><span class="br0">&#40;</span>p4.<span class="me1">x</span>, p4.<span class="me1">y</span>, <span class="kw2">true</span><span class="br0">&#41;</span> ||<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; asteroids<span class="br0">&#91;</span>i<span class="br0">&#93;</span>.<span class="me1">hitTestPoint</span><span class="br0">&#40;</span>p5.<span class="me1">x</span>, p5.<span class="me1">y</span>, <span class="kw2">true</span><span class="br0">&#41;</span><span class="br0">&#41;</span> &amp;amp;&amp;amp; !dead<span class="br0">&#41;</span> <span class="br0">&#123;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; destroyShip<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; remove<span class="br0">&#40;</span>i<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></div>
<p>Each asteroid does a <strong>hitTest</strong> with each point on my <strong>ship_mc</strong>.  It&#8217;s important that I&#8217;m using <strong>hitTestPoint()</strong> here because it is capable of checking based on the actual shape of the movie clip.  A regular <strong>hitTest</strong> between two <strong>MovieClips</strong> will only check to see if the bounding boxes are overlapping&#8230;  in our case that could still be quite 20 pixels or more away from an actual collision.  This is exactly what makes realistic 2D physics so difficult in ActionScript.</p>
<p>Anyways, the Hit Test may be a little buggy in some cases, but <strong>hitTestPoint</strong> works pretty well for us here.  After we detect a hit of course we have to destroy the ship and asteroid and set the death timer.  the <strong>destroyShip()</strong> function is trivial enough, but I would like to take a quick look at a line from the remove() method:</p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw3">stage</span>.<span class="me1">addChild</span><span class="br0">&#40;</span>asteroids<span class="br0">&#91;</span>asteroids.<span class="kw3">push</span><span class="br0">&#40;</span><span class="kw2">new</span> Asteroid<span class="br0">&#40;</span>asteroids<span class="br0">&#91;</span>num<span class="br0">&#93;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="nu0">-1</span><span class="br0">&#93;</span><span class="br0">&#41;</span>;</div>
<p>I really just need to explain how this slightly complicated jumble works.  Reading this from the inner most parenthases out;</p>
<p>We are creating a new Asteroid object (passing it our parent asteroid that has just been destroyed).<br />
<strong>new Asteroid(asteroids[num])</strong></p>
<p>We add that Asteroid object to the end of our asteroids Array which is returning the new length of the Array.<br />
<strong>asteroids.push(&#8230;)</strong></p>
<p>We then use that new length (minus 1) to add our newest asteroid to the stage.<br />
<strong>stage.addChild(asteroids[...-1]);</strong></p>
<p>Eh, see now?  Collision detection is easy!  I think my next installment might be a little more interesting when I add levels, lives, and a scoring system.</p>
<p>&#8230;  No bullets you ask?  Well that&#8217;s just an extension of what I&#8217;ve already written here right?  The only extra thing you have to remember is that the bullets have a limited lifespan.</p>
<p>If you are still confused and can&#8217;t do it on your own (if you want to <em>learn</em> you really aught to at least try) then you can always check out the finished source when it&#8217;s released.</p>
<p>The source code for the example in this tutorial can be found here: <a href="http://www.clickpopmedia.com/wp-content/uploads/2008/07/asteroidsv2source.rar">Asteroids &#8211; V2 Source</a></p>
<p><a href="http://www.clickpopmedia.com/2008/06/25/easy-keyboard-controls-and-game-design/">&lt; Lesson 1: Easy Keyboard Controls and Game Design</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.clickpopmedia.com/2008/07/07/collision-detection-and-game-design/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Easy Keyboard Controls and Game Design</title>
		<link>http://www.clickpopmedia.com/2008/06/25/easy-keyboard-controls-and-game-design/</link>
		<comments>http://www.clickpopmedia.com/2008/06/25/easy-keyboard-controls-and-game-design/#comments</comments>
		<pubDate>Thu, 26 Jun 2008 01:21:07 +0000</pubDate>
		<dc:creator>Paul</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Paul]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Asteroids]]></category>
		<category><![CDATA[Game Development]]></category>
		<category><![CDATA[Keyboard]]></category>

		<guid isPermaLink="false">http://www.clickpopmedia.com/?p=340</guid>
		<description><![CDATA[
In this tutorial I will be going over some of the basic elements to making a game in Flash. This being the first in a short series of tutorials on the subject, I will cover the main game loop and keyboard controls.

First off we need to set up some simple graphics.  I did some [...]]]></description>
			<content:encoded><![CDATA[<p><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="550" height="400" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="src" value="http://www.clickpopmedia.com/wp-content/uploads/2008/06/asteroids.swf" /><embed type="application/x-shockwave-flash" width="550" height="400" src="http://www.clickpopmedia.com/wp-content/uploads/2008/06/asteroids.swf"></embed></object><br />
In this tutorial I will be going over some of the basic elements to making a game in Flash. This being the first in a short series of tutorials on the subject, I will cover the main game loop and keyboard controls.<br />
<span id="more-340"></span><br />
First off we need to set up some simple graphics.  I did some REALLY simple graphics as you can see.  The <strong>ship_mc</strong> being just a triangle and on the 3rd frame of the ship movie clip it get&#8217;s a little fire tail.  Since I am just imitating <em>Atari Interactive, Inc.</em>&#8217;s<em> Asteroids</em>, we don&#8217;t really need anything more then that for now.<br />
Now, to get our initial code setup:</p>
<div class="dean_ch" style="white-space: wrap;">package <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">import</span> flash.<span class="me1">display</span>.<span class="me1">*</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">import</span> flash.<span class="me1">events</span>.<span class="me1">*</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">import</span> flash.<span class="kw3">text</span>.<span class="me1">*</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">import</span> flash.<span class="me1">utils</span>.<span class="me1">Timer</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">public</span> <span class="kw2">class</span> Asteroids <span class="kw3">extends</span> <span class="kw3">MovieClip</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; protected <span class="kw2">var</span> mainTimer:Timer;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">public</span> <span class="kw2">function</span> Asteroids<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mainTimer = <span class="kw2">new</span> Timer<span class="br0">&#40;</span><span class="nu0">20</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mainTimer.<span class="me1">addEventListener</span><span class="br0">&#40;</span>TimerEvent.<span class="me1">TIMER</span>, MainLoop<span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ship_mc.<span class="kw3">stop</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">this</span>.<span class="me1">addEventListener</span><span class="br0">&#40;</span>Event.<span class="me1">DEACTIVATE</span>, deactive<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">this</span>.<span class="me1">addEventListener</span><span class="br0">&#40;</span>Event.<span class="me1">ACTIVATE</span>, active<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">stage</span>.<span class="me1">addEventListener</span><span class="br0">&#40;</span>KeyboardEvent.<span class="me1">KEY_DOWN</span>, kDown<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">stage</span>.<span class="me1">addEventListener</span><span class="br0">&#40;</span>KeyboardEvent.<span class="me1">KEY_UP</span>, kUp<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; protected <span class="kw2">function</span> MainLoop<span class="br0">&#40;</span><span class="kw3">e</span>:TimerEvent<span class="br0">&#41;</span>:<span class="kw3">void</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; protected <span class="kw2">function</span> deactive<span class="br0">&#40;</span><span class="kw3">e</span>:Event<span class="br0">&#41;</span>:<span class="kw3">void</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mainTimer.<span class="kw3">stop</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; protected <span class="kw2">function</span> active<span class="br0">&#40;</span><span class="kw3">e</span>:Event<span class="br0">&#41;</span>:<span class="kw3">void</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mainTimer.<span class="kw3">start</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; protected <span class="kw2">function</span> kDown<span class="br0">&#40;</span><span class="kw3">e</span>:KeyboardEvent<span class="br0">&#41;</span>:<span class="kw3">void</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; protected <span class="kw2">function</span> kUp<span class="br0">&#40;</span><span class="kw3">e</span>:KeyboardEvent<span class="br0">&#41;</span>:<span class="kw3">void</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></div>
<p>So far this is just a skeleton of our game.  We have our <strong>KEY_DOWN</strong> and <strong>KEY_UP</strong> events which will handle all of our keyboard controls later.  But, more importantly, we have our <strong>mainTimer</strong>.  By setting up our game engine to run based on a timer I can later change the frame rate without changing the game dynamics (I left the frame rate at 12/sec for the example above).  This is also useful if your game has too many graphical elements and the frame rate drops on some machines, this will preserve the speed of the game itself.<br />
So, please notice that I have the Timer set to &#8220;tick&#8221; every 20 milliseconds (1000 milliseconds to 1 seconds) which is about 50 ticks per second. Because of that, we are going to have some small values to work with when playing with flight speed and rotation in our ship.<br />
But, first let us think about keyboard controls:</p>
<div class="dean_ch" style="white-space: wrap;">protected <span class="kw2">var</span> thrust:<span class="kw3">Boolean</span>;<br />
protected <span class="kw2">var</span> rpm:<span class="kw3">Number</span>;<br />
protected <span class="kw2">function</span> kDown<span class="br0">&#40;</span><span class="kw3">e</span>:KeyboardEvent<span class="br0">&#41;</span>:<span class="kw3">void</span> <span class="br0">&#123;</span><br />
<span class="co1">//&nbsp; &nbsp; &nbsp; trace(e.keyCode);</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span><span class="kw3">e</span>.<span class="me1">keyCode</span> == <span class="nu0">37</span> || <span class="kw3">e</span>.<span class="me1">keyCode</span> == <span class="nu0">65</span> || <span class="kw3">e</span>.<span class="me1">keyCode</span> == <span class="nu0">100</span><span class="br0">&#41;</span><span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rpm = <span class="nu0">-4</span>;<br />
<span class="co1">//&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; trace(&quot;LEFT&quot;);</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span><span class="kw3">e</span>.<span class="me1">keyCode</span> == <span class="nu0">38</span> || <span class="kw3">e</span>.<span class="me1">keyCode</span> == <span class="nu0">87</span> || <span class="kw3">e</span>.<span class="me1">keyCode</span> == <span class="nu0">104</span><span class="br0">&#41;</span><span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; thrust = <span class="kw2">true</span>;<br />
<span class="co1">//&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; trace(&quot;UP&quot;);</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span><span class="kw3">e</span>.<span class="me1">keyCode</span> == <span class="nu0">39</span> || <span class="kw3">e</span>.<span class="me1">keyCode</span> == <span class="nu0">68</span> || <span class="kw3">e</span>.<span class="me1">keyCode</span> == <span class="nu0">102</span><span class="br0">&#41;</span><span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rpm = <span class="nu0">4</span>;<br />
<span class="co1">//&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; trace(&quot;RIGHT&quot;);</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span><br />
protected <span class="kw2">function</span> kUp<span class="br0">&#40;</span><span class="kw3">e</span>:KeyboardEvent<span class="br0">&#41;</span>:<span class="kw3">void</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span><span class="kw3">e</span>.<span class="me1">keyCode</span> == <span class="nu0">37</span> || <span class="kw3">e</span>.<span class="me1">keyCode</span> == <span class="nu0">65</span> || <span class="kw3">e</span>.<span class="me1">keyCode</span> == <span class="nu0">100</span><span class="br0">&#41;</span><span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rpm = <span class="nu0">0</span>;<br />
<span class="co1">//&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; trace(&quot;LEFT&quot;);</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span><span class="kw3">e</span>.<span class="me1">keyCode</span> == <span class="nu0">38</span> || <span class="kw3">e</span>.<span class="me1">keyCode</span> == <span class="nu0">87</span> || <span class="kw3">e</span>.<span class="me1">keyCode</span> == <span class="nu0">104</span><span class="br0">&#41;</span><span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; thrust = <span class="kw2">false</span>;<br />
<span class="co1">//&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; trace(&quot;UP&quot;);</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span><span class="kw3">e</span>.<span class="me1">keyCode</span> == <span class="nu0">39</span> || <span class="kw3">e</span>.<span class="me1">keyCode</span> == <span class="nu0">68</span> || <span class="kw3">e</span>.<span class="me1">keyCode</span> == <span class="nu0">102</span><span class="br0">&#41;</span><span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rpm = <span class="nu0">0</span>;<br />
<span class="co1">//&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; trace(&quot;RIGHT&quot;);</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></div>
<p><strong>rpm</strong> and <strong>thrust</strong> are going to control our ships flight in the <strong>MainLoop</strong> later.  The <strong>keyCode</strong> is a number which represents the key being pressed or released on the keyboard.  I prefer to set a value on down because the <strong>KEY_DOWN</strong> event will be fired off numerous times very quickly if the key is held too long.  I have the controls set up to work with WASD, the arrow keys, or the num pad.<br />
To find the key values I just traced <strong>e.keyCode</strong> in the <strong>kUp()</strong> function and tried pressing the keys in question.  This got me the numbers you see in the code above.  The commented out traces are there to make sure we have the correct keys, since we don&#8217;t have any other indicator yet.</p>
<p>The next step is a simple one (except for some of the math):</p>
<div class="dean_ch" style="white-space: wrap;">protected const TOP_SPEED:<span class="kw3">Number</span> = <span class="nu0">10</span>;<br />
protected const ACCELERATION:<span class="kw3">Number</span> = .<span class="nu0">1</span>;<br />
protected const FRICTION:<span class="kw3">Number</span> = .<span class="nu0">995</span>;</p>
<p>protected <span class="kw2">var</span> mainTimer:Timer;<br />
protected <span class="kw2">var</span> speedX:<span class="kw3">Number</span>;<br />
protected <span class="kw2">var</span> speedY:<span class="kw3">Number</span>;</p>
<p><span class="kw3">public</span> <span class="kw2">function</span> Asteroids<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; rpm = <span class="nu0">0</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; speedX = <span class="nu0">0</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; speedY = <span class="nu0">0</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &#8230;<br />
<span class="br0">&#125;</span></p>
<p>protected <span class="kw2">function</span> MainLoop<span class="br0">&#40;</span><span class="kw3">e</span>:TimerEvent<span class="br0">&#41;</span>:<span class="kw3">void</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; ship_mc.<span class="me1">rotation</span> += rpm;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">var</span> dx:<span class="kw3">Number</span> = ACCELERATION * <span class="kw3">Math</span>.<span class="kw3">cos</span><span class="br0">&#40;</span><span class="br0">&#40;</span>ship_mc.<span class="me1">rotation*</span><span class="kw3">Math</span>.<span class="kw3">PI</span><span class="br0">&#41;</span>/<span class="nu0">180</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">var</span> dy:<span class="kw3">Number</span> = ACCELERATION * <span class="kw3">Math</span>.<span class="kw3">sin</span><span class="br0">&#40;</span><span class="br0">&#40;</span>ship_mc.<span class="me1">rotation*</span><span class="kw3">Math</span>.<span class="kw3">PI</span><span class="br0">&#41;</span>/<span class="nu0">180</span><span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span>thrust<span class="br0">&#41;</span><span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; speedX += dx;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; speedY += dy;<br />
<span class="co1">//We want to have a max speed in any direction of the compass</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">var</span> speed = speedX*speedX + speedY*speedY;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span>speed &gt;= TOP_SPEED*TOP_SPEED<span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; speedX *= <span class="br0">&#40;</span>TOP_SPEED*TOP_SPEED<span class="br0">&#41;</span>/speed;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; speedY *= <span class="br0">&#40;</span>TOP_SPEED*TOP_SPEED<span class="br0">&#41;</span>/speed;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ship_mc.<span class="kw3">gotoAndStop</span><span class="br0">&#40;</span><span class="nu0">3</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><span class="kw1">else</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ship_mc.<span class="kw3">gotoAndStop</span><span class="br0">&#40;</span><span class="nu0">1</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
<span class="co1">//I know there is no friction in space, but I can&#8217;t remember if Asteroids had it or not.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; speedX *= FRICTION;<br />
&nbsp; &nbsp; &nbsp; &nbsp; speedY *= FRICTION;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; ship_mc.<span class="me1">x</span> += speedX;<br />
&nbsp; &nbsp; &nbsp; &nbsp; ship_mc.<span class="me1">y</span> += speedY;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span>ship_mc.<span class="me1">x</span> &gt; <span class="nu0">560</span><span class="br0">&#41;</span> ship_mc.<span class="me1">x</span> = <span class="nu0">-10</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span>ship_mc.<span class="me1">x</span> &lt; <span class="nu0">-10</span><span class="br0">&#41;</span> ship_mc.<span class="me1">x</span> = <span class="nu0">560</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span>ship_mc.<span class="me1">y</span> &gt; <span class="nu0">410</span><span class="br0">&#41;</span> ship_mc.<span class="me1">y</span> = <span class="nu0">-10</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span>ship_mc.<span class="me1">y</span> &lt; <span class="nu0">-10</span><span class="br0">&#41;</span> ship_mc.<span class="me1">y</span> = <span class="nu0">410</span>;<br />
<span class="br0">&#125;</span></div>
<p>Here we have initialized some variables in the constructor first.  Then we filled in our <strong>MainLoop</strong> to give our spaceship some life.  I would think the <strong>rotation</strong> is self evident&#8230;  it&#8217;s the first line of the method.  I used some constants in my math (<strong>ACCELERATION, TOP_SPEED,</strong> and <strong>FRICTION</strong>) so that I can easily adjust the game settings all in one place, without searching for the parts of the code that I need changed.<br />
The last group of <strong>if</strong> statements above are checking to see in our ship has flown off the screen and if it has it will be moved to the other side.</p>
<p>That&#8217;s all for now.  Next time I will probably add some asteroids and talk a little about <a href="http://www.clickpopmedia.com/2008/07/07/collision-detection-and-game-design/">collision detection</a>.</p>
<p>You can download the source for this example here: <a href="http://www.clickpopmedia.com/wp-content/uploads/2008/06/asteroidssource.rar">AsteroidsSource.rar</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.clickpopmedia.com/2008/06/25/easy-keyboard-controls-and-game-design/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Realistic Shadow Effects using Filters</title>
		<link>http://www.clickpopmedia.com/2008/06/11/realistic-shadow-effects-using-filters/</link>
		<comments>http://www.clickpopmedia.com/2008/06/11/realistic-shadow-effects-using-filters/#comments</comments>
		<pubDate>Wed, 11 Jun 2008 15:37:42 +0000</pubDate>
		<dc:creator>Paul</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Paul]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[DropShadowFilter]]></category>
		<category><![CDATA[Filters]]></category>

		<guid isPermaLink="false">http://www.clickpopmedia.com/?p=329</guid>
		<description><![CDATA[
I have another simple but neat, little tutorial here on Flash CS3 filters.  This time I&#8217;m not using Tweens at all.  Our effect is going to use the MouseEvent.MOUSE_MOVE event to create the illusion of a light source coming from our cursor.

To begin with we are going to make our Shadows class.  [...]]]></description>
			<content:encoded><![CDATA[<pre><center><embed src="http://www.clickpopmedia.com/wp-content/uploads/2008/06/shadows.swf" width="550" height="400" allowscriptaccess="always" allowfullscreen="true"></center></pre>
<p>I have another simple but neat, little tutorial here on Flash CS3 filters.  This time I&#8217;m not using <strong>Tweens</strong> at all.  Our effect is going to use the <strong>MouseEvent.MOUSE_MOVE</strong> event to create the illusion of a light source coming from our cursor.</p>
<p><span id="more-329"></span></p>
<p>To begin with we are going to make our Shadows class.  This class is going to extend <strong>MovieClip</strong>, have a <strong>DropShadowFilter</strong> pre-applied to it and a public method to change the drop shadow filters distance and angle.</p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw3">public</span> <span class="kw2">class</span> Shadows <span class="kw3">extends</span> <span class="kw3">MovieClip</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">public</span> <span class="kw3">static</span> <span class="kw2">var</span> All:<span class="kw3">Array</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; protected <span class="kw2">var</span> txtF:<span class="kw3">TextField</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; protected <span class="kw2">var</span> drop:DropShadowFilter;<br />
&nbsp; &nbsp; &nbsp; &nbsp; protected <span class="kw2">var</span> distance:<span class="kw3">Number</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; protected <span class="kw2">var</span> angle:<span class="kw3">Number</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; protected <span class="kw2">var</span> dropAlpha:<span class="kw3">Number</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; protected <span class="kw2">var</span> blurX:<span class="kw3">Number</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; protected <span class="kw2">var</span> blurY:<span class="kw3">Number</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">public</span> <span class="kw2">function</span> Shadows<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span>Shadows.<span class="me1">All</span> == <span class="kw2">null</span><span class="br0">&#41;</span> Shadows.<span class="me1">All</span> = <span class="kw2">new</span> <span class="kw3">Array</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; distance = <span class="nu0">0</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; angle = <span class="nu0">0</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dropAlpha = <span class="nu0">1</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; blurX = <span class="nu0">0</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; blurX = <span class="nu0">0</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; drop = <span class="kw2">new</span> DropShadowFilter<span class="br0">&#40;</span>distance, angle, <span class="nu0">0</span>, dropAlpha, blurX, blurY<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">this</span>.<span class="me1">filters</span> = <span class="br0">&#91;</span>drop<span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Shadows.<span class="me1">All</span>.<span class="kw3">push</span><span class="br0">&#40;</span><span class="kw3">this</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">public</span> <span class="kw2">function</span> lightSource<span class="br0">&#40;</span>X:<span class="kw3">Number</span>, Y:<span class="kw3">Number</span><span class="br0">&#41;</span>:<span class="kw3">void</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">var</span> dx:<span class="kw3">Number</span> = <span class="br0">&#40;</span><span class="kw3">this</span>.<span class="me1">x</span> + <span class="br0">&#40;</span><span class="kw3">this</span>.<span class="kw3">width</span>/<span class="nu0">2</span><span class="br0">&#41;</span><span class="br0">&#41;</span>-X;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">var</span> dy:<span class="kw3">Number</span> = <span class="br0">&#40;</span><span class="kw3">this</span>.<span class="me1">y</span> + <span class="br0">&#40;</span><span class="kw3">this</span>.<span class="kw3">height</span>/<span class="nu0">2</span><span class="br0">&#41;</span><span class="br0">&#41;</span>-Y;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; angle = <span class="br0">&#40;</span><span class="nu0">180</span> * <span class="kw3">Math</span>.<span class="kw3">atan</span><span class="br0">&#40;</span>dy/dx<span class="br0">&#41;</span><span class="br0">&#41;</span> / <span class="kw3">Math</span>.<span class="kw3">PI</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span>dx &lt; <span class="nu0">0</span><span class="br0">&#41;</span><span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; angle += <span class="nu0">180</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; distance = <span class="kw3">Math</span>.<span class="kw3">sqrt</span><span class="br0">&#40;</span>dy*dy + dx*dx<span class="br0">&#41;</span>/<span class="nu0">10</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dropAlpha = <span class="nu0">10</span>/distance;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; blurX = <span class="kw3">Math</span>.<span class="kw3">abs</span><span class="br0">&#40;</span>dx/<span class="nu0">20</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; blurY = <span class="kw3">Math</span>.<span class="kw3">abs</span><span class="br0">&#40;</span>dy/<span class="nu0">20</span><span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; drop = <span class="kw2">new</span> DropShadowFilter<span class="br0">&#40;</span>distance, angle, <span class="nu0">0</span>, dropAlpha, blurX, blurY<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">this</span>.<span class="me1">filters</span> = <span class="br0">&#91;</span>drop<span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></div>
<p>I hope  you can see what is going on in the code.  I&#8217;m simply setting all my variables equal to 0 or 1, depending on what it&#8217;s for, and applying my filter in the constructor.  Also note that I&#8217;m saving all my objects in a static array.  This makes it easier to call all the <strong>lightSource()</strong> methods at once in a for loop later.</p>
<p>In the <strong>lightSource()</strong> method we have a little bit of math that you might be unfamiliar with.<br />
My variables <strong>dy</strong> and <strong>dx</strong> stand for distance y or x and I&#8217;m setting them equal to the distance from the point given and the center of my ShadowObject.</p>
<p><strong>angle</strong> is going to be the angle of the drop shadow and it needs to be in degrees.  <strong>Math.atan()</strong> is called arc-tangent in the Trigonometry world and it gives your the value of the angle opposite the Y side of a right triangle in radians.  If that doesn&#8217;t make sense, just know that it&#8217;s giving me that angle I need, but in the wrong units. I convert it by multiplying by 180 and dividing by PI.<strong> Math.atan()</strong> only returns a value between -pi/2 and pi/2 (-90 to 90 degrees) which only covers half a circle, so I have to check if we actually need the other side.  And, that&#8217;s what my if() statement is for.</p>
<p><strong>distance</strong> is calculated using the Pythagorean theorem C^2 = A^2 + B^2 (and then divided by 10 otherwise it&#8217;s just too much).<strong></strong></p>
<p><strong>DropAlpha</strong>, <strong>blurX</strong>, and <strong>blurY</strong> are all set pretty clearly (blur has to be positive, thus the <strong>Math.abs()</strong> or absolute value).</p>
<p>Finally our function creates a new drop filter with our new values and applies it to our Shadow object.</p>
<p>Except for trying to remember the math involved, this one has been pretty easy so far, huh?  Well then, let&#8217;s try using it in a project!</p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw2">var</span> str = <span class="kw2">new</span> <span class="kw3">String</span><span class="br0">&#40;</span><span class="st0">&quot;ClickPop&quot;</span><span class="br0">&#41;</span>;<br />
<span class="kw2">var</span> str2 = <span class="kw2">new</span> <span class="kw3">String</span><span class="br0">&#40;</span><span class="st0">&quot;Media&quot;</span><span class="br0">&#41;</span>;<br />
<span class="kw2">var</span> letter:Shadows;<br />
<span class="kw2">var</span> newX:<span class="kw3">Number</span> = <span class="nu0">30</span>;<br />
<span class="kw2">var</span> overLetter:<span class="kw3">TextField</span>;</p>
<p><span class="kw1">for</span><span class="br0">&#40;</span><span class="kw2">var</span> i=<span class="nu0">0</span>; i&lt;str.<span class="kw3">length</span>; i++<span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; letter = <span class="kw2">new</span> Shadows<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; letter.<span class="me1">x</span> = newX;<br />
&nbsp; &nbsp; &nbsp; &nbsp; letter.<span class="me1">y</span> = <span class="nu0">80</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; overLetter = <span class="kw2">new</span> <span class="kw3">TextField</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; overLetter.<span class="kw3">text</span> = str.<span class="kw3">charAt</span><span class="br0">&#40;</span>i<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; overLetter.<span class="kw3">setTextFormat</span><span class="br0">&#40;</span><span class="kw2">new</span> <span class="kw3">TextFormat</span><span class="br0">&#40;</span><span class="st0">&quot;Georgia&quot;</span>, <span class="nu0">100</span>, 0&#215;0000FF, <span class="kw2">true</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; overLetter.<span class="kw3">autoSize</span> = TextFieldAutoSize.<span class="kw3">LEFT</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; letter.<span class="me1">addChild</span><span class="br0">&#40;</span>overLetter<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; addChild<span class="br0">&#40;</span>letter<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; newX += letter.<span class="kw3">width</span>;<br />
<span class="br0">&#125;</span><br />
newX = <span class="nu0">80</span>;<br />
<span class="kw1">for</span><span class="br0">&#40;</span>i=<span class="nu0">0</span>; i&lt;str2.<span class="kw3">length</span>; i++<span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; letter = <span class="kw2">new</span> Shadows<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; letter.<span class="me1">x</span> = newX;<br />
&nbsp; &nbsp; &nbsp; &nbsp; letter.<span class="me1">y</span> = <span class="nu0">180</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; overLetter = <span class="kw2">new</span> <span class="kw3">TextField</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; overLetter.<span class="kw3">text</span> = str2.<span class="kw3">charAt</span><span class="br0">&#40;</span>i<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; overLetter.<span class="kw3">setTextFormat</span><span class="br0">&#40;</span><span class="kw2">new</span> <span class="kw3">TextFormat</span><span class="br0">&#40;</span><span class="st0">&quot;Georgia&quot;</span>, <span class="nu0">100</span>, 0&#215;0000FF, <span class="kw2">true</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; overLetter.<span class="kw3">autoSize</span> = TextFieldAutoSize.<span class="kw3">LEFT</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; letter.<span class="me1">addChild</span><span class="br0">&#40;</span>overLetter<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; addChild<span class="br0">&#40;</span>letter<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; newX += letter.<span class="kw3">width</span>;<br />
<span class="br0">&#125;</span></p>
<p><span class="kw2">function</span> changeLight<span class="br0">&#40;</span>event:MouseEvent<span class="br0">&#41;</span>:<span class="kw3">void</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">for</span><span class="br0">&#40;</span><span class="kw2">var</span> i=<span class="nu0">0</span>; i&lt;Shadows.<span class="me1">All</span>.<span class="kw3">length</span>; i++<span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Shadows.<span class="me1">All</span><span class="br0">&#91;</span>i<span class="br0">&#93;</span>.<span class="me1">lightSource</span><span class="br0">&#40;</span><span class="kw3">stage</span>.<span class="me1">mouseX</span>, <span class="kw3">stage</span>.<span class="me1">mouseY</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span><br />
<span class="kw3">stage</span>.<span class="me1">addEventListener</span><span class="br0">&#40;</span>MouseEvent.<span class="me1">MOUSE_MOVE</span>, changeLight<span class="br0">&#41;</span>;</div>
<p>So, there it is.  I&#8217;m adding each letter to a different Shadow object so that their shadows move independently.  I then update all the shadow light sources to the mouse&#8217;s X and Y coordinates every time the mouse moves.  SO EASY!!</p>
<p>I have a few more ideas for cool effects that you can make with filters and I will probably post about them soon.</p>
<p>Download the Source Code for the example used in this post here: <a href="http://www.clickpopmedia.com/wp-content/uploads/2008/06/shadows.zip">ShadowsSource.zip</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.clickpopmedia.com/2008/06/11/realistic-shadow-effects-using-filters/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Cool Rollover Effects with Filters</title>
		<link>http://www.clickpopmedia.com/2008/05/28/cool-rollover-effects-with-filters/</link>
		<comments>http://www.clickpopmedia.com/2008/05/28/cool-rollover-effects-with-filters/#comments</comments>
		<pubDate>Wed, 28 May 2008 05:15:51 +0000</pubDate>
		<dc:creator>Paul</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Paul]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Filters]]></category>
		<category><![CDATA[Menu]]></category>
		<category><![CDATA[Rollover Effects]]></category>

		<guid isPermaLink="false">http://www.clickpopmedia.com/?p=317</guid>
		<description><![CDATA[
Above is a little example menu I made for a fake game called &#8220;Top Secret.&#8221;  Don&#8217;t expect a game to start when you click on the Play button.  Instead, when you click a button you will get a little menu to change the rollover effects of said button.
What I&#8217;m going to be talking [...]]]></description>
			<content:encoded><![CDATA[<p><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="550" height="400" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="src" value="http://www.clickpopmedia.com/wp-content/uploads/2008/05/topsecret.swf" /><embed type="application/x-shockwave-flash" width="550" height="400" src="http://www.clickpopmedia.com/wp-content/uploads/2008/05/topsecret.swf"></embed></object></p>
<p>Above is a little example menu I made for a fake game called &#8220;Top Secret.&#8221;  Don&#8217;t expect a game to start when you click on the Play button.  Instead, when you click a button you will get a little menu to change the rollover effects of said button.</p>
<p>What I&#8217;m going to be talking about for the next&#8230; however long you read this, is how to recreate these cool effects for your own projects. Hopefully you will come up with some better ideas for using filters then just rollover effects.<br />
<span id="more-317"></span><br />
<strong>Setting Up The Example:</strong><br />
I simply typed all the text you see using whatever font seemed nice.  I then selected each TextField and broke them apart by pressing Ctrl+B twice.  After making each button into a MovieClip and giving them appropriate instance names (i.e. the Play button has a name of &#8220;<strong>play_mc</strong>&#8221; and you can see this in the popup menu) I felt like it was high time to make some cool rollover effects!</p>
<p><strong>Working w/ The Code:</strong><br />
I&#8217;m just going to talk about the one filter here, since the others are all pretty much the same.</p>
<p>To make a blur effect we need to create a new AS class file (one for each button).  We are going to use Tweens, Filters, Events, and we are extending a MovieClip, so we need to import the apropriate library classes for those:</p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw3">import</span> flash.<span class="me1">display</span>.<span class="me1">*</span>;<br />
<span class="kw3">import</span> flash.<span class="me1">events</span>.<span class="me1">*</span>;<br />
<span class="kw3">import</span> flash.<span class="me1">filters</span>.<span class="me1">*</span>;<br />
<span class="kw3">import</span> fl.<span class="me1">transitions</span>.<span class="me1">*</span>;<br />
<span class="kw3">import</span> fl.<span class="me1">transitions</span>.<span class="me1">easing</span>.<span class="me1">*</span>;</div>
<p>The star in <strong>flash.events.*;</strong> just means &#8220;all of them&#8221; and it&#8217;s a quick way of importing lots of stuff at once.</p>
<p>Start out with making and applying our filter to get an idea of what we want:</p>
<div class="dean_ch" style="white-space: wrap;">blur = <span class="kw2">new</span> BlurFilter<span class="br0">&#40;</span><span class="nu0">8</span>, <span class="nu0">4</span>, <span class="nu0">1</span><span class="br0">&#41;</span>; <span class="co1">//blurX, blurY, quality</span><br />
<span class="kw3">this</span>.<span class="me1">filters</span> = <span class="br0">&#91;</span>blur<span class="br0">&#93;</span>;</div>
<p>This is the default value/filter you see before you change any settings in my example movie above.</p>
<p>The <strong>filters</strong> property that I&#8217;m setting in the above code is an Array property of any DisplayObject (including Sprites, MovieClips, and any other children thereof).  That is why I need the square brackets, because it&#8217;s an Array.</p>
<p>With the filter in place we need to check for our <strong>MOUSE_OVER</strong> and <strong>MOUSE_OUT</strong> events and tween the blur filter.</p>
<p>A <strong>Tween</strong> object can change any <strong>Number</strong> property of almost any object over time using <strong>easing functions</strong>.  To read more on Tweens you can check out <a href="http://www.clickpopmedia.com/2008/03/10/tween-intro/">Intro to ActionScript 3.0 Tweening</a>.</p>
<div class="dean_ch" style="white-space: wrap;">protected <span class="kw2">var</span> blurX_tw:Tween;<br />
protected <span class="kw2">var</span> blurY_tw:Tween;</p>
<p><span class="kw3">public</span> <span class="kw2">function</span> <span class="coMULTI">/*CONSTRUCTOR*/</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">this</span>.<span class="me1">addEventListener</span><span class="br0">&#40;</span>MouseEvent.<span class="me1">MOUSE_OVER</span>, Over<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">this</span>.<span class="me1">addEventListener</span><span class="br0">&#40;</span>MouseEvent.<span class="me1">MOUSE_OUT</span>, Out<span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; blurX_tw = <span class="kw2">new</span> Tween<span class="br0">&#40;</span>blur, <span class="st0">&quot;blurX&quot;</span>, Regular.<span class="me1">easeInOut</span>, <span class="nu0">8</span>, <span class="nu0">8</span>, <span class="nu0">1</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; blurX_tw.<span class="me1">addEventListener</span><span class="br0">&#40;</span>TweenEvent.<span class="me1">MOTION_CHANGE</span>, updateBlurFilter<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; blurY_tw = <span class="kw2">new</span> Tween<span class="br0">&#40;</span>blur, <span class="st0">&quot;blurY&quot;</span>, Regular.<span class="me1">easeInOut</span>, <span class="nu0">4</span>, <span class="nu0">4</span>, <span class="nu0">1</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; blurY_tw.<span class="me1">addEventListener</span><span class="br0">&#40;</span>TweenEvent.<span class="me1">MOTION_CHANGE</span>, updateBlurFilter<span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span><br />
protected <span class="kw2">function</span> updateBlurFilter<span class="br0">&#40;</span><span class="kw3">e</span>:TweenEvent<span class="br0">&#41;</span>:<span class="kw3">void</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">this</span>.<span class="me1">filters</span> = <span class="br0">&#91;</span><span class="kw3">e</span>.<span class="me1">currentTarget</span>.<span class="me1">obj</span><span class="br0">&#93;</span>;<br />
<span class="br0">&#125;</span><br />
protected <span class="kw2">function</span> Over<span class="br0">&#40;</span>event:MouseEvent<span class="br0">&#41;</span>:<span class="kw3">void</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; blurX_tw.<span class="me1">continueTo</span><span class="br0">&#40;</span><span class="nu0">0</span>, <span class="nu0">10</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; blurY_tw.<span class="me1">continueTo</span><span class="br0">&#40;</span><span class="nu0">0</span>, <span class="nu0">10</span><span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span><br />
protected <span class="kw2">function</span> Out<span class="br0">&#40;</span>event:MouseEvent<span class="br0">&#41;</span>:<span class="kw3">void</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; blurX_tw.<span class="me1">continueTo</span><span class="br0">&#40;</span><span class="nu0">8</span>, <span class="nu0">15</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; blurY_tw.<span class="me1">continueTo</span><span class="br0">&#40;</span><span class="nu0">4</span>, <span class="nu0">15</span><span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span></div>
<p>Ah! Well there we go.  We now have a tween that effects our X&amp;Y blur. Notice that we have an event listener on those two tweens for <strong>TweenEvent.MOTION_CHANGE</strong>.  This is because the blur filter that we are tweening is NOT the filter that is on our MovieClip.  So for every step that the tween changes the blur values, we need to reapply the filter to our Button.</p>
<p>I&#8217;m sure you have noticed that I am making most of my functions and variables <strong>protected</strong>.  This is because <strong>protected</strong> is just like <strong>private</strong>, but private will not be inherited by child classes and protected will.  I want to keep inheritance because this way I can (and have) make a base class with all my effects and just have my button classes extend that base.</p>
<p>If you download the source for my example movie <a href="http://www.clickpopmedia.com/wp-content/uploads/2008/05/topsecretsource.zip">HERE</a>, you will notice that I have almost all my code within the Rollover class and all my button classes are extending that one.</p>
<p>Once again, you can download the source for this tutorial and example movie here: <a href="http://www.clickpopmedia.com/wp-content/uploads/2008/05/topsecretsource.zip">TopSecret Example Source Code (ZIP &#8211; 958 KB)</a></p>
<p>If you are interested in some of the components I used or want to know more about working with colors, let me know in a comment and maybe I will make a separate tutorial on them.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.clickpopmedia.com/2008/05/28/cool-rollover-effects-with-filters/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Creating an XML Gallery with ActionScript 3.0</title>
		<link>http://www.clickpopmedia.com/2008/05/14/creating-an-xml-gallery-with-actionscript-30/</link>
		<comments>http://www.clickpopmedia.com/2008/05/14/creating-an-xml-gallery-with-actionscript-30/#comments</comments>
		<pubDate>Thu, 15 May 2008 02:37:28 +0000</pubDate>
		<dc:creator>Paul</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[DIY]]></category>
		<category><![CDATA[Just for Fun!]]></category>
		<category><![CDATA[Paul]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[VQ]]></category>
		<category><![CDATA[Beginner]]></category>
		<category><![CDATA[Gallery]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://www.clickpopmedia.com/?p=309</guid>
		<description><![CDATA[For any dynamic Flash project, XML will be a powerful tool.  One of the most common examples (and one of the simplest) is a picture gallery like the one I have here:

Images came from http://www.stockvault.net/
I will be talking about how to use Flash and XML to make a simple, yet superfly, gallery.

So, let us [...]]]></description>
			<content:encoded><![CDATA[<p>For any dynamic Flash project, XML will be a powerful tool.  One of the most common examples (and one of the simplest) is a picture gallery like the one I have here:</p>
<p><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="600" height="700" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="src" value="http://www.clickpopmedia.com/wp-content/uploads/2008/05/xmlgallery.swf" /><embed type="application/x-shockwave-flash" width="600" height="700" src="http://www.clickpopmedia.com/wp-content/uploads/2008/05/xmlgallery.swf"></embed></object><br />
<em>Images came from <a href="http://www.stockvault.net/">http://www.stockvault.net/</a></em></p>
<p>I will be talking about how to use Flash and XML to make a simple, yet superfly, gallery.<br />
<span id="more-309"></span><br />
So, let us  start off by looking at my XML file&#8217;s structure:</p>
<div class="dean_ch" style="white-space: wrap;"><span class="sc3"><span class="re1">&lt;gallery</span> <span class="re0">dir</span>=<span class="st0">&quot;//url/Photos/&quot;</span><span class="re2">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;img</span> <span class="re0">id</span>=<span class="st0">&#8216;0&#8242;</span> <span class="re0">file</span>=<span class="st0">&quot;photo_7030_20070301.jpg&quot;</span> <span class="re0">width</span>=<span class="st0">&#8216;500&#8242;</span> <span class="re0">height</span>=<span class="st0">&#8216;375&#8242;</span><span class="re2">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Cute Puppy! &nbsp;Who couldn&#8217;t love that face?<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;/img<span class="re2">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;img</span> <span class="re0">id</span>=<span class="st0">&#8216;1&#8242;</span> <span class="re0">file</span>=<span class="st0">&quot;photo_4095_20070301.jpg&quot;</span> <span class="re0">width</span>=<span class="st0">&#8216;500&#8242;</span> <span class="re0">height</span>=<span class="st0">&#8216;375&#8242;</span><span class="re2">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Three Friendly Cats.<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;/img<span class="re2">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;img</span> <span class="re0">id</span>=<span class="st0">&#8216;2&#8242;</span> <span class="re0">file</span>=<span class="st0">&quot;photo_4281_20070301.jpg&quot;</span> <span class="re0">width</span>=<span class="st0">&#8216;500&#8242;</span> <span class="re0">height</span>=<span class="st0">&#8216;375&#8242;</span><span class="re2">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; The Terror of the UnderSofa!<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;/img<span class="re2">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;img</span> <span class="re0">id</span>=<span class="st0">&#8216;3&#8242;</span> <span class="re0">file</span>=<span class="st0">&quot;photo_6257_20070301.jpg&quot;</span> <span class="re0">width</span>=<span class="st0">&#8216;500&#8242;</span> <span class="re0">height</span>=<span class="st0">&#8216;375&#8242;</span><span class="re2">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Bored? Let&#8217;s play catch!<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;/img<span class="re2">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;img</span> <span class="re0">id</span>=<span class="st0">&#8216;4&#8242;</span> <span class="re0">file</span>=<span class="st0">&quot;photo_6882_20070301.jpg&quot;</span> <span class="re0">width</span>=<span class="st0">&#8216;500&#8242;</span> <span class="re0">height</span>=<span class="st0">&#8216;377&#8242;</span><span class="re2">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; It may be natural, but this picture still feels weird to me.<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;/img<span class="re2">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;img</span> <span class="re0">id</span>=<span class="st0">&#8216;5&#8242;</span> <span class="re0">file</span>=<span class="st0">&quot;photo_7241_20070301.jpg&quot;</span> <span class="re0">width</span>=<span class="st0">&#8216;500&#8242;</span> <span class="re0">height</span>=<span class="st0">&#8216;375&#8242;</span><span class="re2">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; My cats hate the snow.<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;/img<span class="re2">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;img</span> <span class="re0">id</span>=<span class="st0">&#8216;6&#8242;</span> <span class="re0">file</span>=<span class="st0">&quot;photo_7538_20070507.jpg&quot;</span> <span class="re0">width</span>=<span class="st0">&#8216;500&#8242;</span> <span class="re0">height</span>=<span class="st0">&#8216;622&#8242;</span><span class="re2">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="sc2">&lt;![CDATA[&lt;I&gt;LOOK&lt;/I&gt; IT'S &lt;B&gt;HTML&lt;/B&gt;!!!]]&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;/img<span class="re2">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;img</span> <span class="re0">id</span>=<span class="st0">&#8216;7&#8242;</span> <span class="re0">file</span>=<span class="st0">&quot;photo_7915_20070609.jpg&quot;</span> <span class="re0">width</span>=<span class="st0">&#8216;500&#8242;</span> <span class="re0">height</span>=<span class="st0">&#8216;371&#8242;</span><span class="re2">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Skinny cats are OK too.<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;/img<span class="re2">&gt;</span></span></span><br />
<span class="sc3"><span class="re1">&lt;/gallery<span class="re2">&gt;</span></span></span></div>
<p>XML is very simple to understand if you know anything about HTML&#8217;s tag structure.  The XML document has a root <strong>tag</strong> &lt;gallery&gt; that has an <strong>attribute</strong> dir=&#8221;//url/Photos/&#8221;.  Every tag has to be closed within it&#8217;s own level of the tree.   Our gallery tag is closed at the bottom of the document with <strong>&lt;/gallery&gt;</strong>.   This means that all the  <strong>&lt;img&gt;</strong> tags are elements inside the gallery element (both open and close tags combined make up an element).</p>
<p>Our <strong>&lt;img&gt;</strong> tags have several attributes, such as the images file name and it&#8217;s pixel demensions.  Inside each <strong>&lt;img&gt;</strong> element we have a short comment about the image that will be displayed in a TextField in our Flash movie.</p>
<blockquote><p>One important thing to notice here is that if you want to have HTML in your TextField then you need to tell the code that something is a string containing HTML and not more elements.  We do this with a special bracket &lt;![CDATA[  <em>HTML GOES HERE</em> ]]&gt;  Isn&#8217;t that GREAT!  That right there probably made this whole tutorial worth while (that was a problem that took me a long time to solve).</p></blockquote>
<p>OK, now that we&#8217;ve looked at the XML we need to make it work with ActionScript 3. In Adobe Flash CS3 I created a new AS3 FLA doc and a new AS doc.  In the FLA I simply created some very basic MovieClips for my left and right arrows and I placed a TextField at the bottom of my stage (which I set to 600 X 700 with a black background).  After naming my instances and setting up the Document Class linkage I was done with the FLA side of thing and the rest is ActionScript.</p>
<p>Our Document Class has to extend MovieClip so we take our first steps thusly:</p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw3">public</span> <span class="kw2">class</span> Main <span class="kw3">extends</span> <span class="kw3">MovieClip</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">public</span> <span class="kw2">var</span> currentImg:<span class="kw3">Number</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">public</span> <span class="kw2">var</span> lastImg:<span class="kw3">Number</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">public</span> <span class="kw2">var</span> <span class="kw3">xml</span>:<span class="kw3">XML</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">public</span> <span class="kw2">var</span> loader:Loader;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">public</span> <span class="kw2">var</span> xmlLoader:URLLoader;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">public</span> <span class="kw2">function</span> Main<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; loader = <span class="kw2">new</span> Loader<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; addChild<span class="br0">&#40;</span>loader<span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; xmlLoader = <span class="kw2">new</span> URLLoader<span class="br0">&#40;</span><span class="kw2">new</span> URLRequest<span class="br0">&#40;</span><span class="st0">&quot;//url/gallery.xml&quot;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; xmlLoader.<span class="me1">addEventListener</span><span class="br0">&#40;</span>Event.<span class="me1">COMPLETE</span>, <span class="kw3">parseXML</span><span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; left_mc.<span class="kw3">stop</span><span class="br0">&#40;</span><span class="br0">&#41;</span>; <span class="co1">//Need to stop the arrows from flickering</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; right_mc.<span class="kw3">stop</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></div>
<p>In our constructor we are initializing our Loader which will contain the loaded images and we are loading our XML document.  When the XML doc is done loading, the function parseXML will handle the content.</p>
<div class="dean_ch" style="white-space: wrap;">protected <span class="kw2">function</span> <span class="kw3">parseXML</span><span class="br0">&#40;</span>event:Event<span class="br0">&#41;</span>:<span class="kw3">void</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; left_mc.<span class="me1">addEventListener</span><span class="br0">&#40;</span>MouseEvent.<span class="me1">MOUSE_UP</span>, nextImg<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; left_mc.<span class="me1">addEventListener</span><span class="br0">&#40;</span>MouseEvent.<span class="me1">MOUSE_OUT</span>, arrowOut<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; left_mc.<span class="me1">addEventListener</span><span class="br0">&#40;</span>MouseEvent.<span class="me1">MOUSE_OVER</span>, arrowOver<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; right_mc.<span class="me1">addEventListener</span><span class="br0">&#40;</span>MouseEvent.<span class="me1">MOUSE_UP</span>, prevImg<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; right_mc.<span class="me1">addEventListener</span><span class="br0">&#40;</span>MouseEvent.<span class="me1">MOUSE_OUT</span>, arrowOut<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; right_mc.<span class="me1">addEventListener</span><span class="br0">&#40;</span>MouseEvent.<span class="me1">MOUSE_OVER</span>, arrowOver<span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">xml</span> = <span class="kw2">new</span> <span class="kw3">XML</span><span class="br0">&#40;</span>event.<span class="me1">currentTarget</span>.<span class="kw3">data</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; lastImg = <span class="kw3">xml</span>.<span class="me1">img</span>.<span class="kw3">length</span><span class="br0">&#40;</span><span class="br0">&#41;</span> &#8211; <span class="nu0">1</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; currentImg = <span class="nu0">0</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; loadImg<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span></div>
<p>I&#8217;ve recently gotten into the habit of using protected functions rather then private functions.  There is only one difference between the two and that&#8217;s that private <em>anything</em> won&#8217;t be inherited by child classes.<br />
In this function I set the MouseEvents for the arrows because if the XML failed to load, there would be no reason for the arrows to work.</p>
<p>Alright, so I saved my XML tree in the var <strong>xml</strong> for ease of use later.  I also saved the number of images in my XML doc using the <strong>length()</strong> method.  Something to know about the length method is it will only tell you how many elements there are of that tag type at that level.  For example, if I were to have child elements inside my <strong>img</strong> elements that would not affect the return value of <strong>xml.img.length();</strong> It&#8217;s only telling you how many img elements are children of <strong>gallery</strong> (<strong>gallery</strong> is accessed with the var <strong>xml</strong> directly).</p>
<div class="dean_ch" style="white-space: wrap;">protected <span class="kw2">function</span> loadImg<span class="br0">&#40;</span><span class="br0">&#41;</span>:<span class="kw3">void</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; loader.<span class="me1">x</span> = <span class="br0">&#40;</span><span class="kw3">stage</span>.<span class="kw3">width</span> &#8211; <span class="kw3">Number</span><span class="br0">&#40;</span><span class="kw3">xml</span>.<span class="me1">img</span><span class="br0">&#91;</span>currentImg<span class="br0">&#93;</span>.@<span class="kw3">width</span><span class="br0">&#41;</span><span class="br0">&#41;</span>/<span class="nu0">2</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; loader.<span class="me1">y</span> = <span class="br0">&#40;</span><span class="kw3">stage</span>.<span class="kw3">height</span> &#8211; <span class="nu0">50</span> &#8211; <span class="kw3">Number</span><span class="br0">&#40;</span><span class="kw3">xml</span>.<span class="me1">img</span><span class="br0">&#91;</span>currentImg<span class="br0">&#93;</span>.@<span class="kw3">height</span><span class="br0">&#41;</span><span class="br0">&#41;</span>/<span class="nu0">2</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; loader.<span class="kw3">load</span><span class="br0">&#40;</span><span class="kw2">new</span> URLRequest<span class="br0">&#40;</span><span class="kw3">xml</span>.@dir + <span class="kw3">xml</span>.<span class="me1">img</span><span class="br0">&#91;</span>currentImg<span class="br0">&#93;</span>.@file<span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; loader.<span class="me1">contentLoaderInfo</span>.<span class="me1">addEventListener</span><span class="br0">&#40;</span>Event.<span class="me1">COMPLETE</span>, loadFin<span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span><br />
protected <span class="kw2">function</span> loadFin<span class="br0">&#40;</span>event:Event<span class="br0">&#41;</span>:<span class="kw3">void</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; textDisp_TF.<span class="kw3">htmlText</span> = <span class="kw3">xml</span>.<span class="me1">img</span><span class="br0">&#91;</span>currentImg<span class="br0">&#93;</span>;<br />
<span class="br0">&#125;</span></div>
<p>In that first function I&#8217;m just going to focus on the one line with the URLRequest.  In order to traverse an XML tree you need start with the base.  Your XML var (in our case it&#8217;s called <strong>xml</strong>) points to that base (in this case the base is <strong>gallery</strong>).  So when we want to access the attribute <strong>dir</strong> in the tag <strong>gallery</strong>, we need to type <strong>xml.@dir</strong> and this will return <em>&#8220;//url/Photos/&#8221;</em>.  The &#8216;@&#8217; symbol denotes that it&#8217;s an attribute and not an element (tag).</p>
<p>Getting the file name is just one step further.  <strong>currentImg</strong> is a Number variable that stores which image we are on.  Thinking of our XML var as something similar to a bunch of nested arrays we can see how you might access a specific file name with <strong>xml.img[currentImg].@file</strong> This says we want the file attribute in the [currentImg] (first second so forth) img element which is in our base element.  There are quite a few ways of finding what you need in an XML doc and you can learn more about them<a href="http://livedocs.adobe.com/flash/9.0/main/wwhelp/wwhimpl/common/html/wwhelp.htm?context=LiveDocs_Parts&amp;file=00000129.html"> here</a>.</p>
<p>In the second function we are displaying the text from within our XML img element.  Here we aren&#8217;t accessing any attributes or anything, this is where we get the content between the open &lt;img&gt; and close &lt;/img&gt; tags.</p>
<p>Finally we have the arrow MouseEvent handlers:</p>
<div class="dean_ch" style="white-space: wrap;">protected <span class="kw2">function</span> arrowOut<span class="br0">&#40;</span>event:MouseEvent<span class="br0">&#41;</span>:<span class="kw3">void</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">MovieClip</span><span class="br0">&#40;</span>event.<span class="me1">currentTarget</span><span class="br0">&#41;</span>.<span class="kw3">gotoAndStop</span><span class="br0">&#40;</span><span class="nu0">1</span><span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span></p>
<p>protected <span class="kw2">function</span> arrowOver<span class="br0">&#40;</span>event:MouseEvent<span class="br0">&#41;</span>:<span class="kw3">void</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">MovieClip</span><span class="br0">&#40;</span>event.<span class="me1">currentTarget</span><span class="br0">&#41;</span>.<span class="kw3">gotoAndStop</span><span class="br0">&#40;</span><span class="nu0">2</span><span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span></p>
<p>protected <span class="kw2">function</span> nextImg<span class="br0">&#40;</span>event:MouseEvent<span class="br0">&#41;</span>:<span class="kw3">void</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; loader.<span class="me1">unload</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; currentImg = <span class="br0">&#40;</span>currentImg &lt; lastImg<span class="br0">&#41;</span>? currentImg + <span class="nu0">1</span> : <span class="nu0">0</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; loadImg<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span></p>
<p>protected <span class="kw2">function</span> prevImg<span class="br0">&#40;</span>event:MouseEvent<span class="br0">&#41;</span>:<span class="kw3">void</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; loader.<span class="me1">unload</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; currentImg = <span class="br0">&#40;</span>currentImg &gt; <span class="nu0">0</span><span class="br0">&#41;</span>? currentImg &#8211; <span class="nu0">1</span> : lastImg;<br />
&nbsp; &nbsp; &nbsp; &nbsp; loadImg<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span></div>
<p>Once again I&#8217;m using the less obvious form of the if..else statement.</p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw1">if</span><span class="br0">&#40;</span>num&gt;<span class="nu0">5</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; num = <span class="nu0">5</span>;<br />
<span class="br0">&#125;</span><span class="kw1">else</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; num = <span class="nu0">0</span>;<br />
<span class="br0">&#125;</span><br />
<span class="co1">//OR, THIS NEXT PART DOES THE SAME THING</span><br />
num = <span class="br0">&#40;</span>num&gt;<span class="nu0">5</span><span class="br0">&#41;</span>? <span class="nu0">5</span> : <span class="nu0">0</span>;</div>
<p>Well that&#8217;s about it for this level of a gallery.  You can make galleries look really nice with fade transitions between images, small loading animations for larger images, scrollable thumbnail selection menu&#8230;  etc.</p>
<p>Here is a downloadable source of my example file (images not included&#8230; get them <a href="http://www.stockvault.net/">here</a>): <a href="http://www.clickpopmedia.com/wp-content/uploads/2008/05/xmlgallery.zip">XMLGallery.zip</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.clickpopmedia.com/2008/05/14/creating-an-xml-gallery-with-actionscript-30/feed/</wfw:commentRss>
		<slash:comments>24</slash:comments>
		</item>
		<item>
		<title>Changes to Box2DFlash “Hello World” example.</title>
		<link>http://www.clickpopmedia.com/2008/04/29/specific-changes-to-box2dflash-%e2%80%9chello-world%e2%80%9d-example/</link>
		<comments>http://www.clickpopmedia.com/2008/04/29/specific-changes-to-box2dflash-%e2%80%9chello-world%e2%80%9d-example/#comments</comments>
		<pubDate>Wed, 30 Apr 2008 00:28:36 +0000</pubDate>
		<dc:creator>Paul</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Paul]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Box2DFlash]]></category>
		<category><![CDATA[Intermediate]]></category>
		<category><![CDATA[Intro]]></category>
		<category><![CDATA[Physics]]></category>

		<guid isPermaLink="false">http://www.clickpopmedia.com/?p=281</guid>
		<description><![CDATA[I&#8217;ve talked about the basics of Box2DFlash v1.4.3 in my Introductory tutorial.  I showed how to create your world and how to introduce simple bodies into that world.  With the release of Box2DFlash v2.0.0 the basics remain mostly the same, there are a few details changed.  I need to go over these [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve talked about the basics of Box2DFlash v1.4.3 in my <a href="http://www.clickpopmedia.com/2008/03/17/physics-in-actionscript-3/">Introductory tutorial</a>.  I showed how to create your world and how to introduce simple bodies into that world.  With the release of Box2DFlash v2.0.0 the basics remain mostly the same, there are a few details changed.  I need to go over these small changes before I move on to the next step of adding joints and forces to the bodies.</p>
<p>The first and biggest change (although really a small thing) is that we no longer work in pixels!  We now work in units that by default equal 30 pixels to 1 unit (think of it as meters; 30px : 1m).  With this change our normal gravity is no longer (0, 300) but instead (1, 10).  This makes more sense to me at least because gravity in reality is 9.81 m/s/s.  This is going to effect almost everything you do, from applying forces to setting the width and height of a box.</p>
<p><span id="more-281"></span></p>
<p>The reason for such a change (I believe) is to make it easier for you to dynamically scale a project.  Just change the ratio of pixels per unit and everything should scale correctly.</p>
<p>The second change is the way we add objects to the world.  The creation of a new shape and object is still very much the same, but we now have the ability to adjust the center of mass.  For now, just leaving the center of mass to auto calculate we need to get the body object when adding the new bodyDef to the world.</p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw2">var</span> body:b2Body;<br />
<span class="kw2">var</span> bodyDef:b2BodyDef;<br />
<span class="kw2">var</span> boxDef:b2PolygonDef;</p>
<p><span class="co1">// Add ground body</span><br />
bodyDef = <span class="kw2">new</span> b2BodyDef<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
bodyDef.<span class="kw3">position</span>.<span class="kw3">Set</span><span class="br0">&#40;</span><span class="nu0">10</span>, <span class="nu0">12</span><span class="br0">&#41;</span>; &nbsp;<span class="co1">//Small numbers&#8230; &nbsp;remember it&#8217;s scaled 30:1</span><br />
boxDef = <span class="kw2">new</span> b2PolygonDef<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
boxDef.<span class="me1">SetAsBox</span><span class="br0">&#40;</span><span class="nu0">30</span>, <span class="nu0">3</span><span class="br0">&#41;</span>; &nbsp;<span class="co1">//THIS IS NEW!</span><br />
boxDef.<span class="me1">friction</span> = <span class="nu0">0.3</span>;</p>
<p><span class="co1">// Add sprite to body userData</span><br />
bodyDef.<span class="me1">userData</span> = <span class="kw2">new</span> PhysGround<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
bodyDef.<span class="me1">userData</span>.<span class="kw3">width</span> = <span class="nu0">30</span> * <span class="nu0">2</span> * <span class="nu0">30</span>;<br />
bodyDef.<span class="me1">userData</span>.<span class="kw3">height</span> = <span class="nu0">30</span> * <span class="nu0">2</span> * <span class="nu0">3</span>;<br />
addChild<span class="br0">&#40;</span>bodyDef.<span class="me1">userData</span><span class="br0">&#41;</span>;<br />
body = m_world.<span class="me1">CreateStaticBody</span><span class="br0">&#40;</span>bodyDef<span class="br0">&#41;</span>; &nbsp;<span class="co1">//THIS IS MOSTLY NEW</span><br />
body.<span class="me1">CreateShape</span><span class="br0">&#40;</span>boxDef<span class="br0">&#41;</span>; &nbsp;<span class="co1">//THIS IS NEW</span><br />
body.<span class="me1">SetMassFromShapes</span><span class="br0">&#40;</span><span class="br0">&#41;</span>; &nbsp;<span class="co1">//THIS IS NEW</span></div>
<p>Haha!  I guess I forgot to mention that we no longer have a b2BoxDef anymore&#8230;  we only have b2PolyDef (renamed b2PolygonDef).  We can quickly define it as a box though using the method <strong>public function SetAsBox(hx:Number, hy:Number):void</strong>.</p>
<p>Anyways, let us bring our attention back to adding the object to the world.  When you add the object to the world, you are creating it as either a static (unmoving) body or as a dynamic body (<strong>body = m_world.CreateDynamicBody(bodyDef);</strong>).<br />
It then returns the new body object BY REFRENCE.  It is not giving you a copy of that object.  This way when we make changes, such as add the shape and set the mass (center of mass really) we don&#8217;t have to go send it back again.</p>
<p>The method <strong>public function SetMassFromShapes():void</strong> is the way to auto calculate and set your center of mass.  Otherwise you have to us <strong>public function SetMass(massData:b2MassData):void</strong>, but this is a whole new complication.</p>
<p>OK!  With that, we are pretty much back up to speed after my last tutorial.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.clickpopmedia.com/2008/04/29/specific-changes-to-box2dflash-%e2%80%9chello-world%e2%80%9d-example/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Box2DFlash v2.0.0 Released!</title>
		<link>http://www.clickpopmedia.com/2008/04/21/box2dflash-v200-released/</link>
		<comments>http://www.clickpopmedia.com/2008/04/21/box2dflash-v200-released/#comments</comments>
		<pubDate>Mon, 21 Apr 2008 18:30:33 +0000</pubDate>
		<dc:creator>Paul</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[News]]></category>
		<category><![CDATA[Paul]]></category>
		<category><![CDATA[Box2D]]></category>
		<category><![CDATA[Box2DFlash]]></category>
		<category><![CDATA[Box2DFlashAS3]]></category>

		<guid isPermaLink="false">http://www.clickpopmedia.com/?p=264</guid>
		<description><![CDATA[Box2DFlash v2.0.0 was finally released on the 17th of this month (April).  Box2DFlash is the Flash AS3 port of Erin Catto&#8217;s C++ physics engine Box2D.  Erin Catto is a physics programmer at Blizzard Entertainment (WarCraaaft!).  I&#8217;ve been looking forward to this release since I found out about it while making a tutorial [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://box2dflash.sourceforge.net/">Box2DFlash v2.0.0</a> was finally released on the 17th of this month (April).  Box2DFlash is the Flash AS3 port of Erin Catto&#8217;s C++ physics engine <a href="http://www.box2d.org/">Box2D</a>.  <a href="http://www.gphysics.com/">Erin Catto</a> is a physics programmer at Blizzard Entertainment (WarCraaaft!).  I&#8217;ve been looking forward to this release since I found out about it while making a <a href="http://www.clickpopmedia.com/2008/03/17/physics-in-actionscript-3/">tutorial</a> for the previous release (v1.4.3) about a month ago.<br />
<span id="more-264"></span><br />
Some of the major changes/additions to this version are:</p>
<p>- <strong>Continuous physics</strong>.<br />
Continuous physics means that the physics is now independent of the frame rate.  This prevents bodies from passing through each other at higher speeds.  This feature can potentially make this version slower then previous versions, but it also makes it MUCH more accurate.<br />
- <strong>Shape create/destroy</strong>.</p>
<blockquote><p>You can destroy a shape on the parent body easily. You may do this to model a breakable object.</p></blockquote>
<p>In the past you couldn&#8217;t add or remove shapes from a body object that was already in the world object.<br />
- <strong>Joints defined in local coordinates</strong>.</p>
<blockquote><p>Box2D requires [joint anchor] points to be specified in local coordinates. This way the joint can be specified even when the current body transforms violate the joint constraint &#8212; a common ocurrence when a game is saved and reloaded.</p></blockquote>
<p>- <strong>Moveable center of mass</strong>.<br />
This is great because you can now have a hammer that&#8217;s center of mass is in the head, not the middle of the handle.<br />
- <strong>Sensors</strong>.</p>
<blockquote><p>Some times game logic needs to know when two shapes overlap yet there should be no collision response. This is done by using sensors. A sensor is a shape that detects collision but does not produce a response&#8230;</p></blockquote>
<p>- <strong>Contact listener callback</strong>.<br />
- <strong>Contact filter callback</strong>.<br />
These callbacks are essentially events.  It is now much easier to <em>do</em> something or other on collisions.  Say, cause damage to your spaceship when a meteor strikes it.<br />
- <strong>Revised manual</strong>.<br />
Sure.  Having an up to date manual is a good thing.  Currently the manual is only for the C++ version, but all the concepts are the same for the flash version and it will help a lot for understanding the example projects.<br />
- <strong>Doxygen documents</strong>.<br />
- <strong>Eliminated deferred destruction</strong>.<br />
This was a feature in previous versions of Box2D that was only needed because there was no contact callbacks.  It&#8217;s gone now so I&#8217;m not going to bother explaining what it was.<br />
- <strong>Improved testbed with more examples</strong>.<br />
<a href="http://box2dflash.sourceforge.net/">Check it out!</a> It&#8217;s really just the example project/code that they give you to learn some of the features.</p>
<p>With this release I will be getting back to making some tutorials on how to use this awesome resource.    So, keep checking back to see what&#8217;s new and I will hopefully not disappoint you.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.clickpopmedia.com/2008/04/21/box2dflash-v200-released/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Making an MP3 player in AS3</title>
		<link>http://www.clickpopmedia.com/2008/04/15/making-an-mp3-player-in-as3/</link>
		<comments>http://www.clickpopmedia.com/2008/04/15/making-an-mp3-player-in-as3/#comments</comments>
		<pubDate>Wed, 16 Apr 2008 00:12:24 +0000</pubDate>
		<dc:creator>Paul</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Paul]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Intermediate]]></category>
		<category><![CDATA[MP3 Player]]></category>
		<category><![CDATA[Sound]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://www.clickpopmedia.com/?p=250</guid>
		<description><![CDATA[
Music Credits:
Title: The Best Day Ever
Artist: SpongBob
Album: The SpongeBob SquarePants Movie: Music From the Movie and More
Creating an MP3 Player in ActionScript 3.0 is fairly straight forward. The complexity varies greatly depending on what kind of features you want your player to have.  I don&#8217;t plan on getting into ID3 tags here, but otherwise [...]]]></description>
			<content:encoded><![CDATA[<p><center><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="288" height="170" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="src" value="http://www.clickpopmedia.com/wp-content/uploads/2008/04/as3mp3player.swf" /><embed type="application/x-shockwave-flash" width="288" height="170" src="http://www.clickpopmedia.com/wp-content/uploads/2008/04/as3mp3player.swf"></embed></object></center><br />
<em>Music Credits:<br />
Title: The Best Day Ever<br />
Artist: SpongBob<br />
Album: The SpongeBob SquarePants Movie: Music From the Movie and More</em></p>
<p>Creating an MP3 Player in ActionScript 3.0 is fairly straight forward. The complexity varies greatly depending on what kind of features you want your player to have.  I don&#8217;t plan on getting into ID3 tags here, but otherwise (as you can see) I will be going over the full functionality of a normal MP3 Player.<br />
<span id="more-250"></span><br />
The first step is setting up the display and graphics.  I went into the FLVPlayback skin SkinOverAll.fla file and fetched all the button graphics I needed (I&#8217;m not a very good artist).  It was only a little trouble to recreate the buttons in a way that satisfied me (<i>The only important thing to note about my buttons is that I gave them frame labels on the keyframes to make it easier to remember while writing the code</i>).</p>
<h3>Setting up the framework:</h3>
<p>Now for the code!  You can approach code like this from a number of different directions.  I chose to start with making my button instances look like buttons, even though they don&#8217;t do anything yet.  It&#8217;s really just a matter of setting up a LOT of event listeners (4 for each button):</p>
<div class="dean_ch" style="white-space: wrap;">&#8230;<br />
<span class="me1">stop_mc</span>.<span class="me1">addEventListener</span><span class="br0">&#40;</span>MouseEvent.<span class="me1">MOUSE_OVER</span>, StopOver<span class="br0">&#41;</span>;<br />
stop_mc.<span class="me1">addEventListener</span><span class="br0">&#40;</span>MouseEvent.<span class="me1">MOUSE_OUT</span>, StopOut<span class="br0">&#41;</span>;<br />
stop_mc.<span class="me1">addEventListener</span><span class="br0">&#40;</span>MouseEvent.<span class="me1">MOUSE_DOWN</span>, StopDown<span class="br0">&#41;</span>;<br />
stop_mc.<span class="me1">addEventListener</span><span class="br0">&#40;</span>MouseEvent.<span class="me1">MOUSE_UP</span>, StopUp<span class="br0">&#41;</span>;<br />
&#8230;<br />
<span class="kw3">private</span> <span class="kw2">function</span> StopOver<span class="br0">&#40;</span>event:MouseEvent<span class="br0">&#41;</span>:<span class="kw3">void</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">var</span> obj:<span class="kw3">Object</span> = event.<span class="me1">currentTarget</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; obj.<span class="kw3">gotoAndStop</span><span class="br0">&#40;</span><span class="st0">&#8217;stop_over&#8217;</span><span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span><br />
<span class="kw3">private</span> <span class="kw2">function</span> StopOut<span class="br0">&#40;</span>event:MouseEvent<span class="br0">&#41;</span>:<span class="kw3">void</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">var</span> obj:<span class="kw3">Object</span> = event.<span class="me1">currentTarget</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; obj.<span class="kw3">gotoAndStop</span><span class="br0">&#40;</span><span class="st0">&#8217;stop&#8217;</span><span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span><br />
<span class="kw3">private</span> <span class="kw2">function</span> StopDown<span class="br0">&#40;</span>event:MouseEvent<span class="br0">&#41;</span>:<span class="kw3">void</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">var</span> obj:<span class="kw3">Object</span> = event.<span class="me1">currentTarget</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; obj.<span class="kw3">gotoAndStop</span><span class="br0">&#40;</span><span class="st0">&#8217;stop_down&#8217;</span><span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span><br />
<span class="kw3">private</span> <span class="kw2">function</span> StopUp<span class="br0">&#40;</span>event:MouseEvent<span class="br0">&#41;</span>:<span class="kw3">void</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">var</span> obj:<span class="kw3">Object</span> = event.<span class="me1">currentTarget</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; playPause_mc.<span class="me1">playing</span> = <span class="kw2">false</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; playPause_mc.<span class="kw3">gotoAndStop</span><span class="br0">&#40;</span><span class="st0">&#8216;play&#8217;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; obj.<span class="kw3">gotoAndStop</span><span class="br0">&#40;</span><span class="st0">&#8217;stop_over&#8217;</span><span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span><br />
&#8230;</div>
<p>At this point most of the buttons are the same.  The only difference might lay in the MOUSE_UP event or how the play button has to switch between play and pause, so I have the boolean property playing in my PlayPauseBtn class.</p>
<h3>Bringing in a PlayList:</h3>
<p>Now that we have basic working buttons that don&#8217;t do anything, it feels like the framework is in place to start making our MP3 player play music.  To do this we will need a few elements:</p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw3">public</span> <span class="kw2">var</span> <span class="kw3">url</span>:URLRequest;<br />
<span class="kw3">public</span> <span class="kw2">var</span> xmlLoad:URLLoader;<br />
<span class="kw3">public</span> <span class="kw2">var</span> musicXML:<span class="kw3">XML</span>;<br />
<span class="kw3">public</span> <span class="kw2">var</span> song:<span class="kw3">Sound</span>;<br />
<span class="kw3">public</span> <span class="kw2">var</span> channel:SoundChannel;<br />
<span class="kw3">public</span> <span class="kw2">var</span> sndTrans:SoundTransform;<br />
<span class="kw3">public</span> <span class="kw2">var</span> <span class="kw3">position</span>:<span class="kw3">Number</span>;</div>
<p>Our <b>song:Sound</b> will load the song and play it.  The <b>play()</b> method in the Sound class returns a sound channel which we need to store in our channel:SoundChannel in order to pause/play/stop, or change the volume of our music.  </p>
<p>The <b>SoundTransform</b> class is what is used to change the volume through the channel.  It can also change the pan (balance of left and right speakers), but I&#8217;m not going to get into that.</p>
<p>What we are going to do is load an external XML file with our playlist in it&#8230;  something like this:</p>
<div class="dean_ch" style="white-space: wrap;"><span class="sc3"><span class="re1">&lt;music<span class="re2">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;song</span> <span class="re0">id</span>=<span class="st0">&quot;/url/TheBestDayEver.mp3&quot;</span> <span class="re0">disp</span>=<span class="st0">&quot;The Best Day Ever&quot;</span><span class="re2">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;song</span> <span class="re0">id</span>=<span class="st0">&quot;/url/come_away.mp3&quot;</span> <span class="re0">disp</span>=<span class="st0">&quot;Come Away&quot;</span><span class="re2">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;song</span> <span class="re0">id</span>=<span class="st0">&quot;/url/nightmares.mp3&quot;</span> <span class="re0">disp</span>=<span class="st0">&quot;Nightmares and Dreams&quot;</span><span class="re2">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;song</span> <span class="re0">id</span>=<span class="st0">&quot;/url/override.mp3&quot;</span> <span class="re0">disp</span>=<span class="st0">&quot;Override&quot;</span><span class="re2">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;song</span> <span class="re0">id</span>=<span class="st0">&quot;/url/rescue.mp3&quot;</span> <span class="re0">disp</span>=<span class="st0">&quot;Rescue&quot;</span><span class="re2">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;song</span> <span class="re0">id</span>=<span class="st0">&quot;/url/stick_em_up.mp3&quot;</span> <span class="re0">disp</span>=<span class="st0">&quot;Stick&#8217;em Up&quot;</span><span class="re2">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;song</span> <span class="re0">id</span>=<span class="st0">&quot;/url/trogdor.mp3&quot;</span> <span class="re0">disp</span>=<span class="st0">&quot;TROGDOOOR&quot;</span><span class="re2">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;song</span> <span class="re0">id</span>=<span class="st0">&quot;/url/you_are.mp3&quot;</span> <span class="re0">disp</span>=<span class="st0">&quot;You Are&quot;</span><span class="re2">/&gt;</span></span><br />
<span class="sc3"><span class="re1">&lt;/music<span class="re2">&gt;</span></span></span></div>
<p>Then we want to add each song on that playlist to a list component on the stage (I&#8217;ve called it <b>playList</b>).  After we have the URLs of our songs, then we can start to load and play them.</p>
<p>In our <b>xmlComplete(event:Event)</b> event handler (handling the COMPLETE event from loading our XML file with a URLLoader) we need to parse the playlist into our List:</p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw3">private</span> <span class="kw2">function</span> xmlComplete<span class="br0">&#40;</span>event:Event<span class="br0">&#41;</span>:<span class="kw3">void</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">var</span> item:<span class="kw3">Object</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; musicXML = <span class="kw2">new</span> <span class="kw3">XML</span><span class="br0">&#40;</span>event.<span class="me1">currentTarget</span>.<span class="kw3">data</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">for</span> each<span class="br0">&#40;</span><span class="kw2">var</span> prop:<span class="kw3">XML</span> <span class="kw1">in</span> musicXML.<span class="me1">song</span><span class="br0">&#41;</span><span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; item = <span class="kw2">new</span> <span class="kw3">Object</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; item.<span class="me1">label</span> = prop.@disp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; item.<span class="kw3">data</span> = prop.@id;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; playList.<span class="me1">addItem</span><span class="br0">&#40;</span>item<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; song = <span class="kw2">new</span> <span class="kw3">Sound</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; song.<span class="me1">addEventListener</span><span class="br0">&#40;</span>IOErrorEvent.<span class="me1">IO_ERROR</span>, ioErrorHandler<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; song.<span class="kw3">load</span><span class="br0">&#40;</span><span class="kw2">new</span> URLRequest<span class="br0">&#40;</span>playList.<span class="me1">getItemAt</span><span class="br0">&#40;</span><span class="nu0">0</span><span class="br0">&#41;</span>.<span class="kw3">data</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">this</span>.<span class="me1">addEventListener</span><span class="br0">&#40;</span>Event.<span class="me1">ENTER_FRAME</span>, updateSeek<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; playList.<span class="me1">selectedIndex</span> = <span class="nu0">0</span>;<br />
<span class="br0">&#125;</span></div>
<p>The <b>for each..in loop</b> is one that not everyone knows about.  It&#8217;s very useful for getting all the elements of an XML object when you aren&#8217;t sure how many elements there will be.  Basically it&#8217;s assigning each song tag from the <b>XMLList musicXML.song</b> to prop and then runs the code within the curly brackets {}.</p>
<p>I don&#8217;t like it when music or sounds start playing on the internet without my consent.  So, whenever I make something like and MP3 Player, I set it up to NOT automatically play.</p>
<p>Still, I want it to be ready to play as soon as the user tells it to.  So, I needed to add those lines after my <b>for each..in loop</b>.  What is happening might be obvious enough.  I&#8217;m creating a new Sound object and storing it in song.  I always check for load errors so that the end user doesn&#8217;t get annoying pop-ups telling them that I&#8217;m a bad programmer.  Then I&#8217;m loading the first song on the playList and making that song highlighted in my List component.</p>
<p>The event listener with a handle of <b>updateSeek</b> is needed for my seekbar. I will get to that later.</p>
<h3>Playing the music and traversing out PlayList:</h3>
<p>Great!  So, now we have everything set up to look like a complete player.  We even have our first song loaded.  But, there is no way to get it to play yet.  Our PLAY button doesn&#8217;t work yet.</p>
<p>This is where we have to start giving those buttons some functionality.  Most (if not all) of the functionality for each button is going to be in the MOUSE_UP event handlers:</p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw3">private</span> <span class="kw2">function</span> PlayUp<span class="br0">&#40;</span>event:MouseEvent<span class="br0">&#41;</span>:<span class="kw3">void</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">var</span> obj:<span class="kw3">Object</span> = event.<span class="me1">currentTarget</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span>obj.<span class="me1">playing</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; obj.<span class="kw3">gotoAndStop</span><span class="br0">&#40;</span><span class="st0">&#8216;play_over&#8217;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">position</span> = channel.<span class="kw3">position</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; channel.<span class="kw3">stop</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; obj.<span class="me1">playing</span> = <span class="kw2">false</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span> <span class="kw1">else</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; obj.<span class="kw3">gotoAndStop</span><span class="br0">&#40;</span><span class="st0">&#8216;pause_over&#8217;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; channel = song.<span class="kw3">play</span><span class="br0">&#40;</span><span class="kw3">position</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; channel.<span class="me1">soundTransform</span> = <span class="br0">&#40;</span>mute_mc.<span class="me1">isMute</span><span class="br0">&#41;</span>? <span class="kw2">new</span> SoundTransform<span class="br0">&#40;</span><span class="nu0">0</span><span class="br0">&#41;</span> : sndTrans;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; channel.<span class="me1">addEventListener</span><span class="br0">&#40;</span>Event.<span class="me1">SOUND_COMPLETE</span>, soundComplete<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; obj.<span class="me1">playing</span> = <span class="kw2">true</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></p>
<p>&#8230;</p>
<p><span class="kw3">private</span> <span class="kw2">function</span> StopUp<span class="br0">&#40;</span>event:MouseEvent<span class="br0">&#41;</span>:<span class="kw3">void</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">var</span> obj:<span class="kw3">Object</span> = event.<span class="me1">currentTarget</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; channel.<span class="kw3">stop</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">position</span> = <span class="nu0">0</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; playPause_mc.<span class="me1">playing</span> = <span class="kw2">false</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; playPause_mc.<span class="kw3">gotoAndStop</span><span class="br0">&#40;</span><span class="st0">&#8216;play&#8217;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; obj.<span class="kw3">gotoAndStop</span><span class="br0">&#40;</span><span class="st0">&#8217;stop_over&#8217;</span><span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span></p>
<p>&#8230;</p>
<p><span class="me1">playList</span>.<span class="me1">addEventListener</span><span class="br0">&#40;</span>ListEvent.<span class="me1">ITEM_CLICK</span>, SnglClick<span class="br0">&#41;</span>;</p>
<p>&#8230;</p>
<p><span class="kw3">private</span> <span class="kw2">function</span> SnglClick<span class="br0">&#40;</span>event:ListEvent<span class="br0">&#41;</span>:<span class="kw3">void</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">var</span> item:<span class="kw3">Object</span> = event.<span class="me1">item</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">try</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span>song.<span class="kw3">close</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<span class="br0">&#125;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">catch</span><span class="br0">&#40;</span><span class="kw3">error</span><span class="br0">&#41;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><span class="kw3">trace</span><span class="br0">&#40;</span><span class="kw3">error</span><span class="br0">&#41;</span>;<span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">try</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span>channel.<span class="kw3">stop</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<span class="br0">&#125;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">catch</span><span class="br0">&#40;</span><span class="kw3">error</span><span class="br0">&#41;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><span class="kw3">trace</span><span class="br0">&#40;</span><span class="kw3">error</span><span class="br0">&#41;</span>;<span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; song = <span class="kw2">new</span> <span class="kw3">Sound</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; song.<span class="me1">addEventListener</span><span class="br0">&#40;</span>IOErrorEvent.<span class="me1">IO_ERROR</span>, ioErrorHandler<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; song.<span class="kw3">load</span><span class="br0">&#40;</span><span class="kw2">new</span> URLRequest<span class="br0">&#40;</span>item.<span class="kw3">data</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; channel = song.<span class="kw3">play</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; channel.<span class="me1">soundTransform</span> = <span class="br0">&#40;</span>mute_mc.<span class="me1">isMute</span><span class="br0">&#41;</span>? <span class="kw2">new</span> SoundTransform<span class="br0">&#40;</span><span class="nu0">0</span><span class="br0">&#41;</span> : sndTrans;<br />
&nbsp; &nbsp; &nbsp; &nbsp; channel.<span class="me1">addEventListener</span><span class="br0">&#40;</span>Event.<span class="me1">SOUND_COMPLETE</span>, soundComplete<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">position</span> = <span class="nu0">0</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; playPause_mc.<span class="me1">playing</span> = <span class="kw2">true</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; playPause_mc.<span class="kw3">gotoAndStop</span><span class="br0">&#40;</span><span class="st0">&#8216;pause&#8217;</span><span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span></p>
<p>&#8230;</p>
<p><span class="kw3">private</span> <span class="kw2">function</span> soundComplete<span class="br0">&#40;</span>event:Event<span class="br0">&#41;</span>:<span class="kw3">void</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span>playList.<span class="kw3">length</span> &gt; playList.<span class="me1">selectedIndex</span> + <span class="nu0">1</span><span class="br0">&#41;</span><span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; playList.<span class="me1">selectedIndex</span>++;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; playList.<span class="me1">scrollToSelected</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span> <span class="kw1">else</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; playList.<span class="me1">selectedIndex</span> = <span class="nu0">0</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; playList.<span class="me1">scrollToSelected</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; playList.<span class="me1">dispatchEvent</span><span class="br0">&#40;</span><span class="kw2">new</span> ListEvent<span class="br0">&#40;</span>ListEvent.<span class="me1">ITEM_CLICK</span>, <span class="kw2">false</span>, <span class="kw2">true</span>, <span class="nu0">0</span>, playList.<span class="me1">selectedIndex</span>, playList.<span class="me1">selectedIndex</span>, playList.<span class="me1">selectedItem</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span></p>
<p>&#8230;</p>
<p><span class="kw3">private</span> <span class="kw2">function</span> ForwardUp<span class="br0">&#40;</span>event:MouseEvent<span class="br0">&#41;</span>:<span class="kw3">void</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">var</span> obj:<span class="kw3">Object</span> = event.<span class="me1">currentTarget</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; obj.<span class="kw3">gotoAndStop</span><span class="br0">&#40;</span><span class="st0">&#8216;forward_over&#8217;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span>playList.<span class="kw3">length</span> &gt; playList.<span class="me1">selectedIndex</span> + <span class="nu0">1</span><span class="br0">&#41;</span><span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; playList.<span class="me1">selectedIndex</span>++;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; playList.<span class="me1">scrollToSelected</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span> <span class="kw1">else</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; playList.<span class="me1">selectedIndex</span> = <span class="nu0">0</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; playList.<span class="me1">scrollToSelected</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; playList.<span class="me1">dispatchEvent</span><span class="br0">&#40;</span><span class="kw2">new</span> ListEvent<span class="br0">&#40;</span>ListEvent.<span class="me1">ITEM_CLICK</span>, <span class="kw2">false</span>, <span class="kw2">true</span>, <span class="nu0">0</span>, playList.<span class="me1">selectedIndex</span>, playList.<span class="me1">selectedIndex</span>, playList.<span class="me1">selectedItem</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span></div>
<p>My Play/Pause button, if playing, will save the current position and then stop the music through the channel.  Otherwise, it will play from position and set the volume and add a listener for the end of the song.</p>
<p>The Stop button obviously just stops the song and sets the position back to zero (0). I guess it also resets the Play button.</p>
<p>The  <b>ListEvent.ITEM_CLICK</b> is when we click on a list item (a new song in the playlist).  The handler closes any open stream that might be there (thus the <b>try..catch</b>) and if not then to simply stop the channel.  It then loads the song URL stored in that items data provider and starts it playing.</p>
<p>The forward and back buttons are almost exactly the same.  They just change the selection in the List component and then dispatch a fake <b>ITEM_CLICK</b> event.</p>
<p>The <b>soundComplete()</b> method, as you can see, is also very similar to the forward button.  This event handler is called when a song played right to the end.</p>
<h3>Working with volume:</h3>
<p>Since I&#8217;ve already mentioned setting the volume, I might as well get into that next.  The volume is a property of the <b>SoundTransform class</b>.  We already made a <b>SoundTransform</b> variable, we just haven&#8217;t stored an ST object in it yet.</p>
<p>My main volume control is <b>sndTrans:SoundTransform;</b>  After setting the volume property within it I can assign it to the <b>channel.soundTransform</b> and it will take effect.  My volume bar has it&#8217;s own class to handle the effect of dragging the handle around:</p>
<div class="dean_ch" style="white-space: wrap;">package controls <span class="br0">&#123;</span><br />
<span class="kw3">import</span> flash.<span class="me1">display</span>.<span class="kw3">MovieClip</span>;<br />
<span class="kw3">import</span> flash.<span class="me1">events</span>.<span class="me1">*</span>;<br />
<span class="kw3">import</span> flash.<span class="me1">geom</span>.<span class="me1">Rectangle</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
<span class="kw3">public</span> <span class="kw2">class</span> VolControl <span class="kw3">extends</span> <span class="kw3">MovieClip</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">private</span> <span class="kw2">var</span> bounds:Rectangle;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">public</span> <span class="kw2">var</span> percent:<span class="kw3">Number</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">public</span> <span class="kw2">function</span> VolControl<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; percent = <span class="nu0">1</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bounds = <span class="kw2">new</span> Rectangle<span class="br0">&#40;</span><span class="nu0">0</span>, <span class="nu0">10</span>, <span class="nu0">50</span>, <span class="nu0">0</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; volBar_mc.<span class="me1">hit_mc</span>.<span class="me1">addEventListener</span><span class="br0">&#40;</span>MouseEvent.<span class="me1">MOUSE_DOWN</span>, dragHandle<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; volHandle_mc.<span class="me1">addEventListener</span><span class="br0">&#40;</span>MouseEvent.<span class="me1">MOUSE_DOWN</span>, dragHandle<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; volHandle_mc.<span class="me1">addEventListener</span><span class="br0">&#40;</span>MouseEvent.<span class="me1">MOUSE_UP</span>, endDrag<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; volHandle_mc.<span class="me1">addEventListener</span><span class="br0">&#40;</span>MouseEvent.<span class="me1">MOUSE_OUT</span>, endDrag<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">private</span> <span class="kw2">function</span> dragHandle<span class="br0">&#40;</span>event:MouseEvent<span class="br0">&#41;</span>:<span class="kw3">void</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; volHandle_mc.<span class="kw3">startDrag</span><span class="br0">&#40;</span><span class="kw2">true</span>, bounds<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">this</span>.<span class="me1">addEventListener</span><span class="br0">&#40;</span>Event.<span class="me1">ENTER_FRAME</span>, changeVol<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">private</span> <span class="kw2">function</span> endDrag<span class="br0">&#40;</span>event:MouseEvent<span class="br0">&#41;</span>:<span class="kw3">void</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; volHandle_mc.<span class="kw3">stopDrag</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; volBar_mc.<span class="me1">fullness_mc</span>.<span class="me1">fill_mc</span>.<span class="me1">x</span> = volHandle_mc.<span class="me1">x</span> &#8211; volBar_mc.<span class="me1">fullness_mc</span>.<span class="me1">fill_mc</span>.<span class="kw3">width</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; percent = volHandle_mc.<span class="me1">x</span>/<span class="nu0">50</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dispatchEvent<span class="br0">&#40;</span><span class="kw2">new</span> Event<span class="br0">&#40;</span><span class="st0">&quot;volume_change&quot;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">this</span>.<span class="me1">removeEventListener</span><span class="br0">&#40;</span>Event.<span class="me1">ENTER_FRAME</span>, changeVol<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">private</span> <span class="kw2">function</span> changeVol<span class="br0">&#40;</span>event:Event<span class="br0">&#41;</span>:<span class="kw3">void</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; volBar_mc.<span class="me1">fullness_mc</span>.<span class="me1">fill_mc</span>.<span class="me1">x</span> = volHandle_mc.<span class="me1">x</span> &#8211; volBar_mc.<span class="me1">fullness_mc</span>.<span class="me1">fill_mc</span>.<span class="kw3">width</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; percent = volHandle_mc.<span class="me1">x</span>/<span class="nu0">50</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dispatchEvent<span class="br0">&#40;</span><span class="kw2">new</span> Event<span class="br0">&#40;</span><span class="st0">&quot;volume_change&quot;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></div>
<p>This is simply letting you drag around the little pointer within its bounds and dispatching an event to let the main class know that the volume has been changed.  I needed to repeat some of the code from <b>changeVol()</b> in <b>endDrag()</b> because if you just click quickly then the ENTER_FRAME event never gets handled before it&#8217;s removed.</p>
<p>Back in the main class we are handling that “<b>volume_change</b>” event:</p>
<div class="dean_ch" style="white-space: wrap;">vol_mc.<span class="me1">addEventListener</span><span class="br0">&#40;</span><span class="st0">&quot;volume_change&quot;</span>, updateVolume<span class="br0">&#41;</span>;<br />
&#8230;<br />
<span class="kw3">private</span> <span class="kw2">function</span> updateVolume<span class="br0">&#40;</span>event:Event<span class="br0">&#41;</span>:<span class="kw3">void</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; sndTrans.<span class="me1">volume</span> = vol_mc.<span class="me1">percent</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; channel.<span class="me1">soundTransform</span> = <span class="br0">&#40;</span>mute_mc.<span class="me1">isMute</span><span class="br0">&#41;</span>? <span class="kw2">new</span> SoundTransform<span class="br0">&#40;</span><span class="nu0">0</span><span class="br0">&#41;</span> : sndTrans;<br />
<span class="br0">&#125;</span></div>
<p>Another thing that a lot of new programmers don&#8217;t know about is the conditional ? : operator.  It&#8217;s shorthand for an if..else statement.  Basically these two bits of code are the same:</p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw1">if</span><span class="br0">&#40;</span>num&gt;<span class="nu0">5</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; num = <span class="nu0">5</span>;<br />
<span class="br0">&#125;</span><span class="kw1">else</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; num = <span class="nu0">0</span>;<br />
<span class="br0">&#125;</span><br />
<span class="co1">//OR THIS NEXT PART DOES THE SAME THING</span><br />
num = <span class="br0">&#40;</span>num&gt;<span class="nu0">5</span><span class="br0">&#41;</span>? <span class="nu0">5</span> : <span class="nu0">0</span>;</div>
<p>Isn&#8217;t that great?</p>
<p>Getting back to our MP3 player.  We should add our Mute button while we are working with volume.  You&#8217;ve already seen that I take the mute condition into account when I set that volume.  Here is where I set the mute condition:</p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw3">private</span> <span class="kw2">function</span> MuteUp<span class="br0">&#40;</span>event:MouseEvent<span class="br0">&#41;</span>:<span class="kw3">void</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">var</span> obj:<span class="kw3">Object</span> = event.<span class="me1">currentTarget</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span>obj.<span class="me1">isMute</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; obj.<span class="kw3">gotoAndStop</span><span class="br0">&#40;</span><span class="st0">&#8216;mute_over&#8217;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; obj.<span class="me1">isMute</span> = <span class="kw2">false</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; channel.<span class="me1">soundTransform</span> = sndTrans;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span> <span class="kw1">else</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; obj.<span class="kw3">gotoAndStop</span><span class="br0">&#40;</span><span class="st0">&#8216;unmute_over&#8217;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; obj.<span class="me1">isMute</span> = <span class="kw2">true</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; channel.<span class="me1">soundTransform</span> = <span class="kw2">new</span> SoundTransform<span class="br0">&#40;</span><span class="nu0">0</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></div>
<p>I never want to set <b>sndTrans.volume</b> to zero (0) just because I pressed the mute button.  I would rather leave that as it is so that I can unmute back to the original volume again.</p>
<p>*phew* About this time I&#8217;m thinking to myself, “It sure takes a lot of code to get a simple MP3 Player to work correctly.”  But, there is light on the horizon so we must press on.</p>
<h3>Making the seek bar to search a song:</h3>
<p>Now for the most complicated part of our player.  The seek bar.  The seek bar isn&#8217;t THAT bad, but it&#8217;s definitely worse then what we have done so far.</p>
<p>First of all we will give the seek bar the same drag or click functionality that we gave to volume control bar:</p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw3">public</span> <span class="kw2">class</span> SeekControl <span class="kw3">extends</span> <span class="kw3">MovieClip</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">private</span> <span class="kw2">var</span> bounds:Rectangle;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">public</span> <span class="kw2">var</span> percent:<span class="kw3">Number</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">public</span> <span class="kw2">var</span> seeking:<span class="kw3">Boolean</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">public</span> <span class="kw2">function</span> SeekControl<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; seeking = <span class="kw2">false</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; percent = <span class="nu0">0</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bounds = <span class="kw2">new</span> Rectangle<span class="br0">&#40;</span><span class="nu0">0</span>, <span class="nu0">12</span>, <span class="nu0">100</span>, <span class="nu0">0</span><span class="br0">&#41;</span>; <span class="co1">//I made the seek bar 100 wide for convenience</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; seekHit_mc.<span class="me1">addEventListener</span><span class="br0">&#40;</span>MouseEvent.<span class="me1">MOUSE_DOWN</span>, dragHandle<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; seekHandle_mc.<span class="me1">addEventListener</span><span class="br0">&#40;</span>MouseEvent.<span class="me1">MOUSE_DOWN</span>, dragHandle<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; seekHandle_mc.<span class="me1">addEventListener</span><span class="br0">&#40;</span>MouseEvent.<span class="me1">MOUSE_UP</span>, endDrag<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">private</span> <span class="kw2">function</span> dragHandle<span class="br0">&#40;</span>event:MouseEvent<span class="br0">&#41;</span>:<span class="kw3">void</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; seeking = <span class="kw2">true</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; seekHandle_mc.<span class="kw3">startDrag</span><span class="br0">&#40;</span><span class="kw2">true</span>, bounds<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">private</span> <span class="kw2">function</span> endDrag<span class="br0">&#40;</span>event:MouseEvent<span class="br0">&#41;</span>:<span class="kw3">void</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; seeking = <span class="kw2">false</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; seekHandle_mc.<span class="kw3">stopDrag</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fillBar_mc.<span class="me1">x</span> = seekHandle_mc.<span class="me1">x</span> &#8211; fillBar_mc.<span class="kw3">width</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; percent = seekHandle_mc.<span class="me1">x</span>/<span class="nu0">100</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dispatchEvent<span class="br0">&#40;</span><span class="kw2">new</span> Event<span class="br0">&#40;</span><span class="st0">&quot;seek_change&quot;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">public</span> <span class="kw2">function</span> moveSeekPos<span class="br0">&#40;</span>perc:<span class="kw3">Number</span><span class="br0">&#41;</span>:<span class="kw3">void</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fillBar_mc.<span class="me1">x</span> = <span class="br0">&#40;</span>fillBar_mc.<span class="kw3">width</span> * perc<span class="br0">&#41;</span> &#8211; fillBar_mc.<span class="kw3">width</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span>!seeking<span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; seekHandle_mc.<span class="me1">x</span> = <span class="nu0">100</span> * perc;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></div>
<p>The biggest difference here is that I don&#8217;t want a live update while dragging the pointer around.  Otherwise, a few values have changed, some var names have changed, but it should look familiar (well it would if you had written this code yourself).</p>
<p>The <b>moveSeekPos()</b> method is going to be updated constantly from our main class.  This is here so that we can see the play progress when listening to a song.</p>
<p>Getting back to the main class again, we have to write a thing or two about our seek bar there:</p>
<div class="dean_ch" style="white-space: wrap;"><span class="coMULTI">/*Remember that <br />
this.addEventListener(Event.ENTER_FRAME, updateSeek);<br />
is added when we start to load a new song.*/</span><br />
seek_mc.<span class="me1">addEventListener</span><span class="br0">&#40;</span><span class="st0">&quot;seek_change&quot;</span>, changeSeekBar<span class="br0">&#41;</span>;<br />
&#8230;<br />
<span class="kw3">private</span> <span class="kw2">function</span> changeSeekBar<span class="br0">&#40;</span>event:Event<span class="br0">&#41;</span>:<span class="kw3">void</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">position</span> = song.<span class="kw3">length</span> * seek_mc.<span class="me1">percent</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span>playPause_mc.<span class="me1">playing</span><span class="br0">&#41;</span><span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; channel.<span class="kw3">stop</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; channel = song.<span class="kw3">play</span><span class="br0">&#40;</span><span class="kw3">position</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; channel.<span class="me1">soundTransform</span> = <span class="br0">&#40;</span>mute_mc.<span class="me1">isMute</span><span class="br0">&#41;</span>? <span class="kw2">new</span> SoundTransform<span class="br0">&#40;</span><span class="nu0">0</span><span class="br0">&#41;</span> : sndTrans;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; channel.<span class="me1">addEventListener</span><span class="br0">&#40;</span>Event.<span class="me1">SOUND_COMPLETE</span>, soundComplete<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span><br />
&#8230;<br />
<span class="kw3">private</span> <span class="kw2">function</span> updateSeek<span class="br0">&#40;</span>event:Event<span class="br0">&#41;</span>:<span class="kw3">void</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span>playPause_mc.<span class="me1">playing</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">var</span> perc:<span class="kw3">Number</span> = channel.<span class="kw3">position</span> / song.<span class="kw3">length</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; seek_mc.<span class="me1">moveSeekPos</span><span class="br0">&#40;</span>perc<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; display_mc.<span class="me1">timeDisp</span>.<span class="kw3">text</span> = formatTime<span class="br0">&#40;</span>channel.<span class="kw3">position</span><span class="br0">&#41;</span> + <span class="st0">&quot;/&quot;</span> + formatTime<span class="br0">&#40;</span>song.<span class="kw3">length</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span><br />
&#8230;<br />
<span class="kw3">public</span> <span class="kw2">function</span> formatTime<span class="br0">&#40;</span><span class="kw3">time</span>:<span class="kw3">Number</span><span class="br0">&#41;</span>:<span class="kw3">String</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">var</span> <span class="kw3">min</span>:<span class="kw3">String</span> = <span class="kw3">Math</span>.<span class="kw3">floor</span><span class="br0">&#40;</span><span class="kw3">time</span>/<span class="nu0">60000</span><span class="br0">&#41;</span>.<span class="kw3">toString</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">var</span> sec:<span class="kw3">String</span> = <span class="br0">&#40;</span><span class="kw3">Math</span>.<span class="kw3">floor</span><span class="br0">&#40;</span><span class="br0">&#40;</span><span class="kw3">time</span>/<span class="nu0">1000</span><span class="br0">&#41;</span>%<span class="nu0">60</span><span class="br0">&#41;</span> &lt; <span class="nu0">10</span><span class="br0">&#41;</span>? <span class="st0">&quot;0&quot;</span> + <span class="kw3">Math</span>.<span class="kw3">floor</span><span class="br0">&#40;</span><span class="br0">&#40;</span><span class="kw3">time</span>/<span class="nu0">1000</span><span class="br0">&#41;</span>%<span class="nu0">60</span><span class="br0">&#41;</span>.<span class="kw3">toString</span><span class="br0">&#40;</span><span class="br0">&#41;</span> : <span class="kw3">Math</span>.<span class="kw3">floor</span><span class="br0">&#40;</span><span class="br0">&#40;</span><span class="kw3">time</span>/<span class="nu0">1000</span><span class="br0">&#41;</span>%<span class="nu0">60</span><span class="br0">&#41;</span>.<span class="kw3">toString</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span><span class="br0">&#40;</span><span class="kw3">min</span>+<span class="st0">&quot;:&quot;</span>+sec<span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span></div>
<p>I went ahead and added the code here to display the current play time and the total play time in a dynamic text field on the stage.  That seemed to be closely related to the seek bar, so I figured showing it here is suitable.</p>
<h3>A final note:</h3>
<p>Now would be a good time to mention that I didn&#8217;t bother fixing a “minor” bug.  While a song is still streaming (loading), Flash doesn&#8217;t know how long it is.  Thus, <b>song.length</b> will be growing over time till it&#8217;s done loading.  This is only an issue for our seek bar, because it uses the ratio between the current position and the total length to decide how to display itself.</p>
<p>To work around this issue (which is really only an issue for slower internet connections), you will need to pass Flash the songs length along with the file path in the XML doc.  It seemed like a lot of trouble to me and since this isn&#8217;t for a client, I figured I would just tell you about it instead of do it for you.</p>
<p>Wow.  This was so long that I wouldn&#8217;t be surprised to find later that I forgot something.  If I find anything I will make sure to update this post and make a note of what I changed.</p>
<p>Anyways, that is it! I think&#8230;   </p>
<p>Let me know what you think about it and also let me know if you even use my Player on your site somewhere.  I would love to see my work being used.</p>
<p>You can get the source files for this tutorial and the example at the beginning to the page here: <a href='http://www.clickpopmedia.com/wp-content/uploads/2008/07/mp3-player-source-code.rar'>MP3 Player Source Code.rar</a></p>
<p><em><strong>UPDATE:</strong> I have updated the source file to include changes which will fix a few bugs pointed out to me over time.  I also added a few new lines to the disclaimer at the beginning of my code which explains that WE DO NOT PROVIDE TECHNICAL SUPPORT FOR OUR EXAMPLE CODE.  It is there for reference only.  We would hope that you would use our code in conjunction with the tutorial to learn how to do it yourself.<br />
We understand that most people will just download and use the code without much effort towards learn new skills, but we aren&#8217;t going to answer your questions in that case. If you notice something wrong with the code and want to bring it to our attention that would be appreciated, but any questions on how to use the code will be answered with &#8220;Read the tutorial.&#8221;</em></p>
]]></content:encoded>
			<wfw:commentRss>http://www.clickpopmedia.com/2008/04/15/making-an-mp3-player-in-as3/feed/</wfw:commentRss>
		<slash:comments>115</slash:comments>
		</item>
		<item>
		<title>Internal PreLoading in ActionScript 3</title>
		<link>http://www.clickpopmedia.com/2008/04/07/internal-preloading-in-actionscript-3/</link>
		<comments>http://www.clickpopmedia.com/2008/04/07/internal-preloading-in-actionscript-3/#comments</comments>
		<pubDate>Mon, 07 Apr 2008 17:26:42 +0000</pubDate>
		<dc:creator>Paul</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Paul]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[PreLoader]]></category>

		<guid isPermaLink="false">http://www.clickpopmedia.com/?p=239</guid>
		<description><![CDATA[
I&#8217;m going to walk through the steps of making a preloader for your animation, game, or application and provide easily implemented source files that you can copy and change to fit your own project. Making a preloader in ActionScript 3 is more difficult to figure out then it was in ActionScript 2, but the code [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: center;"><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="275" height="200" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="src" value="http://www.clickpopmedia.com/wp-content/uploads/2008/04/clickpop-preloader.swf" /><embed type="application/x-shockwave-flash" width="275" height="200" src="http://www.clickpopmedia.com/wp-content/uploads/2008/04/clickpop-preloader.swf"></embed></object></p>
<p>I&#8217;m going to walk through the steps of making a preloader for your animation, game, or application and provide easily implemented source files that you can copy and change to fit your own project. Making a preloader in ActionScript 3 is more difficult to figure out then it was in ActionScript 2, but the code itself is not any harder.<br />
<span id="more-239"></span><br />
To start off, you can access the stage&#8217;s <strong>bytesLoaded</strong> and <strong>bytesTotal</strong> through it&#8217;s <strong>loaderInfo</strong>.<br />
Once you know that, building a preloader becomes very easy:</p>
<div class="dean_ch" style="white-space: wrap;">package <span class="br0">&#123;</span><br />
<span class="kw3">import</span> flash.<span class="me1">display</span>.<span class="kw3">MovieClip</span>;<br />
<span class="kw3">import</span> flash.<span class="kw3">text</span>.<span class="kw3">TextField</span>;<br />
<span class="kw3">import</span> flash.<span class="me1">events</span>.<span class="me1">*</span>;</p>
<p><span class="kw3">public</span> <span class="kw2">class</span> ClickPopPreLoader <span class="kw3">extends</span> <span class="kw3">MovieClip</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">var</span> initX:<span class="kw3">Number</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">public</span> <span class="kw2">function</span> ClickPopPreLoader<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; initX = loadProg_MC.<span class="me1">x</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; addEventListener<span class="br0">&#40;</span>Event.<span class="me1">ENTER_FRAME</span>, handleProgress<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">private</span> <span class="kw2">function</span> handleProgress<span class="br0">&#40;</span>event:Event<span class="br0">&#41;</span>:<span class="kw3">void</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">var</span> <span class="kw3">loaded</span>:<span class="kw3">Number</span> = <span class="kw3">stage</span>.<span class="me1">loaderInfo</span>.<span class="kw3">bytesLoaded</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">var</span> total:<span class="kw3">Number</span> = <span class="kw3">stage</span>.<span class="me1">loaderInfo</span>.<span class="kw3">bytesTotal</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">var</span> percent:<span class="kw3">Number</span> = <span class="kw3">loaded</span>/total;<br />
<span class="co1">//&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; trace(Math.floor(percent*100)+&quot;%&quot;);</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; UpdateProgress<span class="br0">&#40;</span>percent<span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span><span class="kw3">loaded</span> &gt;= total<span class="br0">&#41;</span><span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; removeEventListener<span class="br0">&#40;</span>Event.<span class="me1">ENTER_FRAME</span>, handleProgress<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="coMULTI">/*stage.getChildAt(0) can cause an error message that is<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; really a warning. &nbsp;Turn off Strict mode in &quot;Publish Settings&quot;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; and it will compile just fine.*/</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">stage</span>.<span class="me1">getChildAt</span><span class="br0">&#40;</span><span class="nu0">0</span><span class="br0">&#41;</span>.<span class="kw3">gotoAndPlay</span><span class="br0">&#40;</span><span class="st0">&#8216;begin&#8217;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">private</span> <span class="kw2">function</span> UpdateProgress<span class="br0">&#40;</span>prog:<span class="kw3">Number</span><span class="br0">&#41;</span>:<span class="kw3">void</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; loadProg_MC.<span class="me1">x</span> = initX &#8211; <span class="br0">&#40;</span>initX * prog<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; output_TF.<span class="kw3">text</span> = <span class="kw3">String</span><span class="br0">&#40;</span><span class="kw3">Math</span>.<span class="kw3">floor</span><span class="br0">&#40;</span>prog * <span class="nu0">100</span><span class="br0">&#41;</span>+<span class="st0">&quot;%&quot;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></div>
<p><em>The code is so short I would feel silly cutting it up.</em></p>
<p>The code above would be a class file for a MovieClip that contains another MovieClip called <strong>loadProg_MC</strong> that acts as a mask for some kind of load bar (in my example it&#8217;s unmasking the glow behind the words &#8220;Click Pop Media&#8221;).  The MovieClip would also have a TextField called <strong>output_TF</strong> to display the % loaded.</p>
<p>Notice that I&#8217;m using the ENTER_FRAME event rather then the PROGRESS event.  You really could use the PROGRESS and COMPLETE events, but I&#8217;ve heard that it doesn&#8217;t work consistently in IE6 on PCs (supposedly that has been fixed in Flash Player 9.0.115).  I haven&#8217;t really checked this myself because I don&#8217;t have Internet Explorer 6, but I just take their word for it because ENTER_FRAME works just as well.</p>
<p>Once you get the <strong>stage.loaderInfo</strong> what you do with it and how you display the load progress is up to you.  That&#8217;s actually not the part that raises questions for most people.  What usually happens is that they will add all the right code and make a really good preloader animation and then when they test it out&#8230;  the preloader doesn&#8217;t show up until AFTER everything is loaded.</p>
<p>The problem that we run into is that Flash Player loads a movie starting with frame one and then works its way up.  Although you may only have your preloader on frame one you need to take a look at the &#8220;linkage&#8221; of each of your library items.</p>
<p><a href="http://www.clickpopmedia.com/wp-content/uploads/2008/04/linkageprop.png"><img class="aligncenter size-full wp-image-241" title="linkageprop" src="http://www.clickpopmedia.com/wp-content/uploads/2008/04/linkageprop.png" alt="Linkage Properties" width="443" height="229" /></a><br />
The checkbox, which by default is selected, called &#8220;Export in first frame&#8221; needs to be unchecked for as many, if not all, of your library symbols (especially larger items like sounds and music).  &#8220;Export in first frame&#8221; means that until this item is loaded the first frame can&#8217;t play.  Thus, if everything is exported in the first frame then your preloader won&#8217;t have a chance to show anything loading before it&#8217;s already done.</p>
<p>This leads to another problem though! If you are creating instances of your library symbols dynamically using ActionScript and that code loads before the symbol does, then you will get runtime errors.  This isn&#8217;t really a problem if you have a pure animation without any code.  For a game though, avoiding this little problem is messy, but it can be done easily enough and without being noticed by the end users.</p>
<p>What you have to do is, in a frame BEFORE any of your main code, create one instance of each library item that is going to be instantiated dynamically&#8230; but still after your preloader.  A good practice would be to have your preloader on frame 1 and then all your instances on frame 2.  When your preloader is done (and maybe the play button is clicked) it will jump PAST frame 2 and go straight to the beginning of your animation/game/app.</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-</p>
<p>For those of you who either don&#8217;t want to turn off strict mode or want to avoid OOP (Object Oriented Programming), here is the same code I gave you before, but modified to reside on the stage instead of in a class.</p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw2">var</span> initX:<span class="kw3">Number</span> = PreLoader.<span class="me1">loadProg_MC</span>.<span class="me1">x</span>;<br />
addEventListener<span class="br0">&#40;</span>Event.<span class="me1">ENTER_FRAME</span>, handleProgress<span class="br0">&#41;</span>;</p>
<p><span class="kw2">function</span> handleProgress<span class="br0">&#40;</span>event:Event<span class="br0">&#41;</span>:<span class="kw3">void</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">var</span> <span class="kw3">loaded</span>:<span class="kw3">Number</span> = <span class="kw3">stage</span>.<span class="me1">loaderInfo</span>.<span class="kw3">bytesLoaded</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">var</span> total:<span class="kw3">Number</span> = <span class="kw3">stage</span>.<span class="me1">loaderInfo</span>.<span class="kw3">bytesTotal</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">var</span> percent:<span class="kw3">Number</span> = <span class="kw3">loaded</span>/total;<br />
<span class="co1">//&nbsp; &nbsp; &nbsp; trace(Math.floor(percent*100)+&quot;%&quot;);</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; UpdateProgress<span class="br0">&#40;</span>percent<span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span><span class="kw3">loaded</span> &gt;= total<span class="br0">&#41;</span><span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; removeEventListener<span class="br0">&#40;</span>Event.<span class="me1">ENTER_FRAME</span>, handleProgress<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">gotoAndPlay</span><span class="br0">&#40;</span><span class="st0">&#8216;begin&#8217;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span><br />
<span class="kw2">function</span> UpdateProgress<span class="br0">&#40;</span>prog:<span class="kw3">Number</span><span class="br0">&#41;</span>:<span class="kw3">void</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; PreLoader.<span class="me1">loadProg_MC</span>.<span class="me1">x</span> = initX &#8211; <span class="br0">&#40;</span>initX * prog<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; PreLoader.<span class="me1">output_TF</span>.<span class="kw3">text</span> = <span class="kw3">String</span><span class="br0">&#40;</span><span class="kw3">Math</span>.<span class="kw3">floor</span><span class="br0">&#40;</span>prog * <span class="nu0">100</span><span class="br0">&#41;</span>+<span class="st0">&quot;%&quot;</span><span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span></div>
<p>You can also make a document class if you wanted (for those who like OOP, but still want to keep Strict mode for some reason), but I&#8217;m not going to get into that here.</p>
<p><strong>As promised, here is the source files to a working preloader:</strong> <a href="http://www.clickpopmedia.com/wp-content/uploads/2008/04/clickpopmedia-preloader.zip">ClickPopMedia Preloader</a><br />
There is instructions all over the place within those files.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.clickpopmedia.com/2008/04/07/internal-preloading-in-actionscript-3/feed/</wfw:commentRss>
		<slash:comments>23</slash:comments>
		</item>
	</channel>
</rss>

