Tag Archives: as1

as1 swf loader

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.

I’m actually splitting assets up into several small files and then loading them up into an invisible container clip.

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.

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.

FakeMCL = function( parent, depth ) {
if( !parent )
parent = _root;
this.parent = parent; // parent clip of container
this.loadQueue = []; // queue of clips to be loaded
this.container = null; // clip into which all children are loaded
this.init( depth );
};// end: "constructor"

FakeMCL.prototype.init = function( depth ) {
if( !depth )
depth = this.parent.getNextHighestDepth();
// create an invisible container clip at parent._fakemcl
this.container = this.parent.createEmptyMovieClip( "_fakemcl", depth );
this.container._visible = false;
trace("<fakemcl> container "+this.container+" is at depth "+depth);
}// end: init()

FakeMCL.prototype.loadMovie = function( url ) {
this.loadQueue.push( url );
if( !this.loadInterval ) {
trace("<fakemcl> starting interval");
// try to tick once every 25ms (that’s once per frame at 40fps), we’re
// probably not running that fast, so it will just execute once per frame
// NOTE: you can (and might want to) throttle this back
this.loadInterval = setInterval( this.onInterval, 25, this );
}// end: start interval
}// end: loadMovie()

FakeMCL.prototype.onInterval = function( mcl ) {
// stop iterating if we’re completely out of clips to load
if( !mcl.loadQueue.length && !mcl.loadingClip ) {
trace("<fakemcl> queue is empty");
clearInterval( mcl.loadInterval );
// NOTE: you might want to do something here now that the whole queue is loaded
}// end: queue is empty
// start loading a clip if we aren’t already
if( !mcl.loadingClip ) {
var clipURL = mcl.loadQueue.shift();
var instanceName = "_"+clipURL.substr( 0, clipURL.indexOf(".") );
trace("<fakemcl> loading "+clipURL+" ("+instanceName+")");
mcl.loadingClip = mcl.container.createEmptyMovieClip( instanceName, mcl.container.getNextHighestDepth() );
mcl.loadingClip.loadMovie( clipURL );
// NOTE: clip init goes here
}// end: not loading anything
else {
// check status of currently loading movie
var percentLoaded = mcl.loadingClip.getBytesLoaded() / mcl.loadingClip.getBytesTotal();
trace("<fakemcl> loaded "+mcl.loadingClip.getBytesLoaded()+"/"+mcl.loadingClip.getBytesTotal());
if( percentLoaded >= 1.0 && mcl.loadingClip.getBytesTotal() ) {
// we’re done loading
trace("<fakemcl> done, "+mcl.loadingClip);
mcl.loadingClip = null;
// NOTE: callback for individual clip loading goes here
}// end: currently loading
}// end: onInterval()

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 😉

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.


Put the following code on a frame of your timeline:
#include "FakeMCL.as"

var mcl = new FakeMCL( this, 10 );
And hopefully, you’ll get some output like this:
<fakemcl> container _level0._fakemcl is at depth 10
<fakemcl> starting interval
<fakemcl> loading fish.swf (_fish)
<fakemcl> loaded 0/-1
<fakemcl> loaded 1465744/1465744
<fakemcl> done, _level0._fakemcl._fish
<fakemcl> loading bird.swf (_bird)
<fakemcl> loaded 1427856/1427856
<fakemcl> done, _level0._fakemcl._bird
<fakemcl> queue is empty
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…