<?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>Untitled &#187; as1</title>
	<atom:link href="http://ammonlauritzen.com/blog/tag/as1/feed/" rel="self" type="application/rss+xml" />
	<link>http://ammonlauritzen.com/blog</link>
	<description>and still for good reason.</description>
	<lastBuildDate>Tue, 13 Dec 2011 21:29:14 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.2</generator>
		<item>
		<title>as1 swf loader</title>
		<link>http://ammonlauritzen.com/blog/2007/05/22/as1-swf-loader/</link>
		<comments>http://ammonlauritzen.com/blog/2007/05/22/as1-swf-loader/#comments</comments>
		<pubDate>Tue, 22 May 2007 20:33:16 +0000</pubDate>
		<dc:creator>Ammon</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[as1]]></category>
		<category><![CDATA[flash]]></category>
		<category><![CDATA[howto]]></category>

		<guid isPermaLink="false">http://ammonlauritzen.com/blog/2007/05/22/as1-swf-loader/</guid>
		<description><![CDATA[So I find myself writing a whole bunch of AS1 recently (upgrading another old game). In this case, the project was originally done in a single ginormous FLA file. No separate assets file, no external .as files. We've broken lots of timeline code out into source files, but have yet to break any of the [...]]]></description>
			<content:encoded><![CDATA[<p>So I find myself writing a whole bunch of AS1 recently (upgrading another old game). In this case, the project was originally done in a single ginormous FLA file. No separate assets file, no external .as files. We've broken lots of timeline code out into source files, but have yet to break any of the assets out. That's my current project.</p>
<p>I'm actually splitting assets up into several small files and then loading them up into an invisible container clip.</p>
<p>When I started writing the loading code, I forgot that AS1 doesn't have the MovieClipLoader class. That's a Flash 7 invention. So, I got to do things the really old fashioned way, by listening to bytes loaded and all that. I figured since it is potentially a common problem (for those who wind up having to go back into the stone ages, that is), I may as well share the code with folks.</p>
<p>Please note that this code is a skeleton. It does not do a whole lot as it is. It just gives you a starting framework to edit to your individual application.</p>
<p>[as3]<br />
FakeMCL = function( parent, depth ) {<br />
	if( !parent )<br />
		parent = _root;<br />
	this.parent = parent;	// parent clip of container<br />
	this.loadQueue = [];	// queue of clips to be loaded<br />
	this.container = null;	// clip into which all children are loaded<br />
	this.init( depth );<br />
};// end: &quot;constructor&quot;</p>
<p>FakeMCL.prototype.init = function( depth ) {<br />
	if( !depth )<br />
		depth = this.parent.getNextHighestDepth();<br />
	// create an invisible container clip at parent._fakemcl<br />
	this.container = this.parent.createEmptyMovieClip( &quot;_fakemcl&quot;, depth );<br />
	this.container._visible = false;<br />
	trace(&quot;&lt;fakemcl&gt; container &quot;+this.container+&quot; is at depth &quot;+depth);<br />
}// end: init()</p>
<p>FakeMCL.prototype.loadMovie = function( url ) {<br />
	this.loadQueue.push( url );<br />
	if( !this.loadInterval ) {<br />
		trace(&quot;&lt;fakemcl&gt; starting interval&quot;);<br />
		// try to tick once every 25ms (that's once per frame at 40fps), we're<br />
		// probably not running that fast, so it will just execute once per frame<br />
		// NOTE: you can (and might want to) throttle this back<br />
		this.loadInterval = setInterval( this.onInterval, 25, this );<br />
	}// end: start interval<br />
}// end: loadMovie()</p>
<p>FakeMCL.prototype.onInterval = function( mcl ) {<br />
	// stop iterating if we're completely out of clips to load<br />
	if( !mcl.loadQueue.length &amp;&amp; !mcl.loadingClip ) {<br />
		trace(&quot;&lt;fakemcl&gt; queue is empty&quot;);<br />
		clearInterval( mcl.loadInterval );<br />
		// NOTE: you might want to do something here now that the whole queue is loaded<br />
		return;<br />
	}// end: queue is empty<br />
	// start loading a clip if we aren't already<br />
	if( !mcl.loadingClip ) {<br />
		var clipURL = mcl.loadQueue.shift();<br />
		var instanceName = &quot;_&quot;+clipURL.substr( 0, clipURL.indexOf(&quot;.&quot;) );<br />
		trace(&quot;&lt;fakemcl&gt; loading &quot;+clipURL+&quot; (&quot;+instanceName+&quot;)&quot;);<br />
		mcl.loadingClip = mcl.container.createEmptyMovieClip( instanceName, mcl.container.getNextHighestDepth() );<br />
		mcl.loadingClip.loadMovie( clipURL );<br />
		// NOTE: clip init goes here<br />
	}// end: not loading anything<br />
	else {<br />
		// check status of currently loading movie<br />
		var percentLoaded = mcl.loadingClip.getBytesLoaded() / mcl.loadingClip.getBytesTotal();<br />
		trace(&quot;&lt;fakemcl&gt; loaded &quot;+mcl.loadingClip.getBytesLoaded()+&quot;/&quot;+mcl.loadingClip.getBytesTotal());<br />
		if( percentLoaded&gt;= 1.0 &amp;&amp; mcl.loadingClip.getBytesTotal() ) {<br />
			// we're done loading<br />
			trace(&quot;&lt;fakemcl&gt; done, &quot;+mcl.loadingClip);<br />
			mcl.loadingClip = null;<br />
			// NOTE: callback for individual clip loading goes here<br />
		}<br />
	}// end: currently loading<br />
}// end: onInterval()<br />
[/as3]</p>
<p>Again, this is skeleton code and isn't terribly useful as is. I've put some notes in the code where you might want to do more interesting things to adapt the durned thing to your application. Also, feel free to yoink the trace statements, they can get awful spammy <img src='http://ammonlauritzen.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>This skeleton supports queuing up a whole list of swf's for loading, but in actual use, it is probably going to be more useful for you to just load one swf at a time. Shrug.</p>
<h3>example</h3>
<p>Put the following code on a frame of your timeline:<br />
[as3]<br />
#include &quot;FakeMCL.as&quot;</p>
<p>var mcl = new FakeMCL( this, 10 );<br />
mcl.loadMovie(&quot;fish.swf&quot;);<br />
mcl.loadMovie(&quot;bird.swf&quot;);<br />
[/as3]<br />
And hopefully, you'll get some output like this:</p>
<div class="syntax_hilite">
<div id="code-2">
<div class="code">&amp;lt;fakemcl&amp;gt; container _level0._fakemcl is at depth <span style="color:#800000;">10</span><br />
&amp;lt;fakemcl&amp;gt; starting interval<br />
&amp;lt;fakemcl&amp;gt; loading fish.<span style="">swf</span> <span style="color:#006600; font-weight:bold;">&#40;</span>_fish<span style="color:#006600; font-weight:bold;">&#41;</span><br />
&amp;lt;fakemcl&amp;gt; loaded <span style="color:#800000;">0</span>/-<span style="color:#800000;">1</span><br />
&amp;lt;fakemcl&amp;gt; loaded <span style="color:#800000;">1465744</span>/<span style="color:#800000;">1465744</span><br />
&amp;lt;fakemcl&amp;gt; done, _level0._fakemcl._fish<br />
&amp;lt;fakemcl&amp;gt; loading bird.<span style="">swf</span> <span style="color:#006600; font-weight:bold;">&#40;</span>_bird<span style="color:#006600; font-weight:bold;">&#41;</span><br />
&amp;lt;fakemcl&amp;gt; loaded <span style="color:#800000;">1427856</span>/<span style="color:#800000;">1427856</span><br />
&amp;lt;fakemcl&amp;gt; done, _level0._fakemcl._bird<br />
&amp;lt;fakemcl&amp;gt; queue is empty</div>
</div>
</div>
<p>
When all is done running, you've got those clips at _fakemcl._bird and _fakemcl._fish that you can toy with however you need. Chances are, you probably want to know when the loading is actually done, however. That'll take some editing of this code. Put a stop() on the frame you call FakeMCL.loadMovie() from and then put "mcl.parent.play()" in the load complete block, etc...</p>
]]></content:encoded>
			<wfw:commentRss>http://ammonlauritzen.com/blog/2007/05/22/as1-swf-loader/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

