Level: Intermediate, Language: AS 2.0
In this tutorial I am going to show you how to use the MovieClipLoader Class to load a Flash movie into another Flash movie. This is a very useful Class and you can use it to load jpg, gif and png files as well
Download the example source files here.
I created two Flash movies for this example. The one named main.swf will be the main flash movie that you will embed in a website. The other movie called content.swf will be loaded into main.swf.
In main.swf, you need to create a button, give it an instance name loader_btn.
Open the action panel and enter the following code on the first frame:
//create an instance of the MovieClipLoader Class
var clipLoader:MovieClipLoader = new MovieClipLoader();
//create a container and set it’s depth.
this.createEmptyMovieClip(“MyContainer_mc”,_root.getNextHighestDepth());
loader_btn.onRelease = function() {
clipLoader.loadClip(“content.swf”, MyContainer_mc);
}
Tip: You can create a function to unload the content.swf clip
Add to the first frame the following code
function unLoadingClips() {
clipLoader.unloadClip(_root.getNextHighestDepth());
}
You can now call the function on a particular frame or button of your main.swf movie:
unLoadingClips();
One of the advantage in using the MovieClipLoader Class is the ability to add a listener to your MovieClipLoader. That way you can add a preloader and listen to the loading start, the progress and when the Flash movie is fully loaded. Following is an example:
var clipLoader:MovieClipLoader = new MovieClipLoader();
this.createEmptyMovieClip(“MyContainer_mc”,_root.getNextHighestDepth());
loader_btn.onRelease = function() {
clipLoader.loadClip(“content.swf”,_root.getNextHighestDepth());
}
var preloader:Object = new Object();
clipLoader.addListener(preloader);
preloader.onLoadStart = function(target){
target.createTextField(“my_txt”,target.getNextHighestDepth,0,0,100,20);
target.my_txt.selectable = false;
}
preloader.onLoadProgress = function (target, loadedBytes, totalBytes){
target.my_txt.text = Math.floor((loadedBytes/totalBytes)*100)+”%”
target_mc._visible = true;
}
preloader.onLoadComplete = function(target){
target.my_txt._visible = false;
}
stop();
What I have done here is set a Textfield called my_txt on top of the loaded clip to give indication of how much data was downloaded at runtime.
You can easily replace the text field by a movieClip as your preloader.
Tip: You can invoke the onLoadInit method to the MovieClipLoader to be called when the first frame of your content.swf has been loaded.



0 Responses to “Loading a flash movie using the MovieClipLoader Class”