Script for a pre-loader when importing / loading flash movies (swf) into main movie

Submitted by Local Tree Child on Mon, 12/10/2007 - 13:48.

I decided for once I would do my own portfolio in Flash, because I wanted to check out the new features that had arisen since I used it really thoroughly back in 2002. Web banners, which is the only Flash stuff I've done in the recent time, usually don't pose a problem as they are of low technical quality more animation challenges there.

Anyways I tied this together using several tutorials. There were some pitfalls which I will go trough but I'll try to be brief as I hate long-winded tutorials.

So what wanted to do was basically import some Flash movies into my main Flash movie. This is good because if you have many similar objects you can just create separate movies for each object and then import them. This will keep the movies low in complexity and you don't have to load all content directly, the user can himself download what he wants which makes for a much smoother experience.

The basic idea is this, user presses button, load animation comes up, when finished loading data, the load movie fades in.

First create a clip to hold the loaded SWF

var myClip = this.createEmptyMovieClip("myClip", this.getNextHighestDepth());

To load movies, images etc. in Flash you use the MovieClipLoader object when you want complete control over the loading process.

var myLoader:MovieClipLoader = new MovieClipLoader();

The movie clip loader object responds to different events much like Java or such we have event listeners, thus we create a listener and add it to our MovieClipLoaderObject

var myListener = new Object();
myLoader.addListener(myListener);

We now have the basic setup of objects and now can divide time in to three events, on start, when loading and then when finished.

We start with on start even

myListener.onLoadStart = function(targetMC){
loadEvent = true;
myClip._visible = false;
preloader._visible = true;
};

The output of this is just to set some variables. The loadEvent variable is just to memory variable so we know that we are loading a movie. This may or may not be necessary I keep it for users not to be able to click to many button at the same time because Flash (or my code) is still a bit unstable. The myClip._visible, preloader._visible is just to tell our movie clip container not to show and the pre loader movieclip to show it self.

We continue with the load in progress event, which is triggered every time some data is loaded.

myListener.onLoadProgress = function(targetMC:MovieClip, lBytes, tBytes) {
preloader.bar._width = int((lBytes/tBytes)*100);
};

Basically what the code does is to change the length of a rectangle signifying the loading process. lBytes is a loaded bytes and tBytes is total bytes.

When the load is complete

myListener.onLoadComplete = function (targetMC) {
// Set position an visibility of our loaded clip
myClip._x = 263;
myClip._y = 75;
// Fade preloader
preloader.play();
// Call function for test if preload clip has faded
// and proceeds to fade in the loaded clip. Nice!
fadeFinish = setInterval(testFade, 50, preloader, myClip);
};

The code sets the position of our movie clip and then tell the pre-load clip to play its fade out after which the loaded movie fades in.

The function called sets our movie clip alpha to zero and makes it visible again then calls our fade in function.

function testFade(target_mc:MovieClip,target_mc2:MovieClip ):Void {
if ( target_mc._currentframe === 9 ) {
// Set alpha for loaded object to zero so we can fade it in
target_mc2._alpha = 0;
target_mc2.stop();
target_mc2._visible = true;
target_mc._visible = false;
target_mc.gotoAndStop(1);
// Fade in the loaded clip!
fadeIn = setInterval(fadeInClip, 50, myClip);
}
}
function fadeInClip(target_mc:MovieClip):Void {
target_mc._alpha += 5;
if (target_mc._alpha > 100) {
clearInterval(fadeIn);
clearInterval(fadeFinish);
loadEvent = false;
target_mc._alpha = 100;
}
}
/* Clean up all garbage intervals that
* was left when loading a new movieclip
*/
function clearAllIntervals () {
i = setInterval(function(){},100) + 1
while(i--){ clearInterval(i); }
}

The clearAllIntervals function kills all setInterVal functions as they can seriously screw up your code. In the beginning of the code writing I had on setInterval function call a another setInterval in it pretty much same as the above, though the code also did a setInterval on itself, thus destroying the called setInterval variable and leaving me with a very annoying loop that practically set the alpha value to millions. So just for safety I always call the destroyer to get rid of unwanted loops when there should be none. Read some good stuff about the setInterval here.

We then continue to create a button function that is called every time a button is called.

function buttonLoadClip(movie:String){
if(!loadEvent) {
clearAllIntervals();
myLoader.unloadClip(myClip);
myLoader.loadClip("flash/"+movie+".swf", myClip);
}
}

The function, if we are no in loading clears all setInterval functions, unloads our current movie clip that we have loaded and then loads the clip.

Then we add actions to all our buttons

button1.onPress = function(){ buttonLoadClip('movie1') };

Finally a thing to look out for is that if you have code in the movie you loaded you must use this._lockroot = true; in the root of clip you are loading, if you don't the consequences seem to be that main movie thinks you are talking to it, and if you order for the loaded clip to play with for example in my case with setInterval the main movie starts jerking in some semi-mode between playing and being still. So beware! I don't want you up all night debugging because of this bug/logical error in Flash.

urchinTracker(); // -->