Loading an External swf or jpg File with a UILoader

Loading an External swf or jpg File with a UILoader

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 UILoader component.

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.

A UILoader Component

 

Step One

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

Step Two

Add a new layer above the Content layer named Actions.

Step Three

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

Step Four

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

Step Five

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

Step Six

Return to the Main.fla file, Content layer and add a UILoader component by dragging it from the Components panel (Window > Components) to the stage.

Step Seven

Select the component icon and press Delete. Since the component was added to the Library in Step Six we no longer need it on the stage.

Step Eight

Option double-click (PC - Alt double-click) frame one of the Actions layer.

Step Nine

Copy/Paste the following code to the ActionScript panel.

import fl.containers.UILoader;

var loader01:UILoader = new UILoader();

loader01.source="External.swf";

loader01.scaleContent=false;

loader01.x=0;

loader01.y=0;

addChild(loader01);

Step Ten

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 Eight imports the UILoader class to the file. Line two establishes a new UILoader variable called loader01. Line three sets the target for the external source 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 four tells Flash not to scale the external content. Lines five and six set the external file's location on the main stage.

The final line adds loader01 to the stage.