Loading an External swf or jpg File with a Loader

Loading an External swf or jpg File with a Loader

Flash CS4 ActionScript 3.0 Tutorial

 

How to Bring External Files to the Main Stage in Flash

 

 

External files can be imported multiple ways in Flash. You can use a Loader object, UILoader Component or import an external file into a Movie Symbol. In this example, we will import an external file using a Loader object.

During the loading process you may wish to use a preloader to let the user know that something is being downloaded. For information on importing images with preloaders, see topics, Loading External File with UILoader and Preloader and Preloader for External swf or Image File.

Tutorial Elements

External swf file with instance name External.swf. For still images you could substitute this file with a jpg file

 

Step One

Open a new Flash document and name the default layer Actions.

Step Two

Save your file in a Project folder and name the file Main.fla.

Step Three

Open a new Flash document and name it External.fla. Save it in the same Project folder.

Step Four

Add an animation or series of images to the External.fla file and publish the file (Cmd/Return (PC - Ctrl/Enter)

Step Five

Return to the Main.fla file and Option double-click (PC - Alt double-click) frame one of the Actions layer.

Step Six

Copy/Paste the following code to the ActionScript panel.

var loader01:Loader = new Loader();

loader01.load(new URLRequest("External.swf"));

addChild(loader01);

Step Seven

Publish your movie by pressing Cmd/Return (PC - Ctrl/Enter). The external file appears on the main stage.

Dissecting the Code

The first line of the code at Step Six establishes a new Loader variable called loader01. Line two 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".

Line three adds loader01 to the stage.

How About the Background and Where Does It Land?

Whenever you load an external file, the external file's background becomes transparent. In other words, if it has two objects and a blue background, the two objects import but not the blue background.

The external file aligns to the host file's upper left corner.