The correct way to load swf files!

March 6th, 2009

Posted by admin

No Comments »

I would like to address the ideal way, of structuring your swf files to load in your fla (Production file). We will cover the MovieClipLoader class, Object listeners, addListener(), call back functions, the trace() method, and the loadClip() method.

Okay first off we declare (create) our MovieClipLoader class object to access our Methods (Behaviors)

var mcl:MovieClipLoader  = new MovieClipLoader();

Declare (create) an empty movie clip, assign a instance name of empty_mc, with a depth of 1 or using the getNextHighestDepth method like so below.

“Doing so will create a target for our swf to load into.”

this.createEmptyMovieClip(“empty_mc”, this.getNextHighestDepth());

We then create our Object listener, to listen for call back functions such as onLoadStart,onLoadProgress, and onLoadComplete.

var mclListener:Object = new Object();

Assign our listener to our named callback function onLoadStart, which will be fired when our swf file is being loaded.

mclListener.onLoadStart = onLoadStartHandler;

function onLoadStartHandler(target_mc:MovieClip):Void
{

Then we do a trace to output a message stating “File being loaded:” and concatenate (add)  target_mc._url property, to display our files name.
trace(“File being loaded: “  +  target_mc._url);
}

Okay so we then want to get an output of exactly how many bytes are being loaded when the swf file is being called from the end-users machine. And a output of the
percentage being loaded!.  We declare the call back function onLoadProgress using a named function as we did before, this function will be called every time bytes are being loaded into your target.

mclListener.onLoadProgress = onLoadProgressHandler;

function onLoadProgressHandler(target_mc:MovieClip, bytesLoaded:Number,totalBytes:Number):Void
{
// We assign our Math.round method to our variable, and then we data type it to a Number which will give our percentage output in our trace.
var percent:Number = Math.round(bytesLoaded/bytesTotal * 100);
trace(“Percentage being loaded: ” + percent);


To give a trace of how many bytes are being loaded we use the bytesLoaded variable that we’ve declared in our onLoadProgressHandler function above. And to display our total amount of bytes our swf contains we declare our totalBytes variable like so above.
trace(“Bytes Loaded: ” + bytesLoaded + ” out of ” + totalBytes);
}

The onLoadComplete Handler will basically be fired when the swf file completely finishes loading. So if we would like to display a message or trace() in our output window stating “Done Loading”.

mclListener.onLoadComplete = onLoadCompleteHandler;

function onLoadCompleteHandler(target_mc:MovieClip):Void
{
trace(“Done Loading: ” + target_mc._url);
}

Make sure you assign the addListener method to your MovieClipLoader class object to be able to fire all callback functions.

mcl.addListener(mclListener);

And finally we assign our loadClip() method to our MovieClipLoader class object to load our corresponding swf file to our empty movie clip target(empty_mc).

mcl.loadClip(“your_swf_file_name.swf”, empty_mc);


I hope this brief tutorial was helpful to you, please feel free to e-mail me at fgudino@sensisagency.com.

I have uploaded my files, below for review.
http://senduit.com/a5698b

Good Luck coding!.

Abel Gudino (Flash Designer/Flash Developer)

Share This Post

Leave a Reply





Back to Top