Flash AS Tutorial: Load External Image to Empty MovieClip (2)
Sometimes you have some external images loading to the same movieclip of Flash Movie. Then you should consider using a re-usable function.
Flash Tutorial Content:
In this Flash ActionScript tutorial, we use a more flexible or a more re-usable function to load the external image. This is useful if you have more than one external images to load to the same empty MovieClip (e.g. Photo Show). In this situation, the function can be re-used again.
The complete Flash Movie is shown as above, you may try how it works before you start this tutorial. A simple Text Preloader is also added to the loading progress.
Flash ActionScript Codes:
//Declare the loading percentage
var percent:Number;
//Declare a new loader with the name "loader" to use later
var loader:Loader;
//Load the external image
loadExternalImage("island.png");
function loadExternalImage(fileURL:String):void {
loader = new Loader();
//Load the external image into the Loader
loader.load(new URLRequest(fileURL));
//Listen to the progress of the loading
loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, loadingProgress);
// Listen when the loading of image is completed
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadingComplete);
}
function loadingProgress(evt:ProgressEvent) {
percent = Math.round((evt.bytesLoaded/evt.bytesTotal) * 100);
//Display the loading percentage to visitors
//Actually you can use AS to add the TextField dynamically
percentLoaded_txt.text = "Loading picture.... " + String(percent) + "% loaded";
}
function loadingComplete(evt:Event) {
//The holder_mc (new symbol) was created before by:
/*
Insert -> New Symbol -> Select MovieClip
Give this new symbol any name you like (e.g. MyImage)
Return to the scene.
Drag the MovieClip (e.g. MyImage) into your scene from the Library window
Give the instance name of this MovieClip "holder_mc"
*/
holder_mc.addChild(loader);
//Hide the loading progress text that no longer in use
percentLoaded_txt.visible = false;
}
Download Flash Source File:
Remarks:
This Flash ActionScript tutorial show how to use a more flexible and re-usable function to load external image.