Preloader for External swf or Image File
Flash CS4 ActionScript 3.0 Tutorial
How to Add a Movie Symbol Preloader with Flash ActionScript
External files can be imported multiple ways in Flash. You can simply add the object to the stage or add the object with a preloader to let the user know about the downloading process.
Preloaders can be added at the start of your host file or used to count down the loading of an external swf or image file.
The following is an example of a preloader that displays whenever an external file is added to the stage.
External swf file with instance name External.swf. For still images you could substitute this file with a jpg file.
Open a new Flash document and add a new layer.
Name the top layer Actions and the lower layer Preloader.
Save your file in a Project folder and name the file Main.fla.
Open a new Flash document and name it External.fla. Save it in the same Project folder.
Add an animation or series of images to the External.fla file and publish the file (Cmd/Return (PC - Ctrl/Enter)
Return to the Main.fla file and click frame one of the Preloader layer. Add a rectangle with the Rectangle tool.
Select the rectangle and convert it to a Movie Symbol by pressing F8. The registration should be set to the upper left corner.
Select the rectangle and assign it an instance name of bar_mc in the Properties panel.
Option double-click (PC - Alt double-click) frame one of the Actions layer.
Copy/Paste the following code to the ActionScript panel.
var loader01:Loader = new Loader();
Click the Auto Format icon at the top of the ActionScript panel.
Test your movie by pressing Cmd/Return (PC - Ctrl/Enter). Press Cmd/Return (PC - Ctrl/Enter) a second time to simulate a specified download speed. The speed can be changed by choosing View > Download Settings. The bar expands to show the download progress. Once the external file loads, the bar disappears.
Dissecting the Code
The first line of the code at Step Ten establishes a new Loader variable called loader01. Lines two and three add event listeners for the download progress and to see if the download is complete.
Line four makes a URLRequest to import the external file. If the external file were inside a folder (named SwfFiles) next to the host file the code in parentheses would read, "SwfFiles/External.swf".
If the download is in progress, then the loop function is run. The variable perc is added to track the download from zero to one by calculating bytesLoaded by bytesTotal.
The value of perc is assigned to the bar_mc's scale in the x direction.
The done function adds the loader01 contents to the stage and makes the bar_mc invisible.