<?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; PreLoader</title>
	<atom:link href="http://www.clickpopmedia.com/tag/preloader/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>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>

