Notes on loading external swf files

It may be the case that you want to build a Flash application that brings in Flash movies, that is, other Flash applications. Rather than copying and pasting to make one new .fla file, you instead can load the published (.swf) files into your application using code. The sample shown on this site shows the player two buttons. The action on clicking the buttons is to load either a rock-paper-scissors application or a bouncing ball application.

The first step is to use the addEventListener method to associate a call to a function with the CLICK event for each buton. The code to do this for a button named rpsbtn is:

rpsbtn.addEventListener(MouseEvent.CLICK, playrps);

The coding for playrps will be explained next.

The critical constructs in Flash for bringing in an external Flash movie are the URLRequest and the Loader objects. The URLRequest object creates an object based on a file (at a URL address). This, in turn, is loaded into a Loader object and this Loader object is added to the display list using addChild.

function playrps(ev) {

var mm:URLRequest = new URLRequest("rps3action.swf");

var loader:Loader = new Loader()

loader.load(mm);

addChild(loader);

}

You can position the loader file by setting the loader.x and loader.y attributes.

Because this example uses two buttons and I want to demonstrate clicking them both, I need to modify this code. I move the declaration of loader outside the function and set up a Boolean to indicate if anything has been loaded. I write code to check the variable and remove the loader before adding it again.

The Flash application has two buttons on the Stage. I made them using buttons in the Common Library and changing the text. The names of the buttons are rpsbtn and bbbtn.

The code below associates each of these buttons with a specific swf file.

The files referenced must be in the same folder as the menu.swf file produced by publishing your Flash document (or the menu.fla file while being tested in the Flash environment) OR you need to provide a correct relative URL or a complete URL.

The code is

rpsbtn.addEventListener(MouseEvent.CLICK, playrps);
bbbtn.addEventListener(MouseEvent.CLICK, playbb);

var loader:Loader=new Loader();

var loaded:Boolean = false;

function playrps(ev) {

var mm:URLRequest = new URLRequest("rps3action.swf");

if (loaded) {

removeChild(loader);}

loaded = true;

loader.load(mm);

loader.x = 0; // may not be necessary

loader.y = 20;

addChild(loader);

}

function playbb(ev) {

var mm:URLRequest = new URLRequest("rotate2moons.swf");

if (loaded) {

removeChild(loader);}

loaded = true;

loader.load(mm);

loader.x = 0; // may not be necessary

loader.y = 20;

addChild(loader);

}

You do need to make sure that the Document of the original Flash file is big enough for the loaded files. You change the dimensions of the Document by clicking on the Stage NOT on any object on the Stage and in the Properties change the width and height.