Sunday, 8 November 2009

Load external SWF in AS3

In this Flash tutorial you will learn how to load an external SWF file using Actionscript 3.0. In Actionscript 2.0 you could use the MovieClipLoader class or the loadMovie command to load an external SWF file, but now in AS3 you have to use the Loader class. The Loader class was also used in a previous tutorial which loaded external text into a dynamic text field.

There are several advantages of loading external SWF files. Firstly, a large project can be broken into multiple movies, each of the SWF files will be smaller and therefore more memory efficient and faster. Also, multiple SWF files can be played in series without the need to refresh the html page.

You will need a SWF file for this tutorial. This file should be placed in the same directory as your fla file.


Load external SWF in AS3

Open up a Flash AS3 file and then select first frame on the timeline. Open up the Actions panel and enter the following code. Make sure the SWF you want to load is in the same directory as your fla file.

//This creates a new instance of the loader object and adds it to the stage.
var my_Loader:Loader = new Loader();
addChild(my_Loader);

//This creates a new instance of the URLRequest object that contains the path
//to the external swf. The load method then loads the SWF file into the loader
//object.
var my_url:URLRequest=new URLRequest("example.swf");
my_Loader.load(my_url);

The code above loads the SWF file to the default location (0, 0) on the stage. If you want to change the position of the SWF, you can adjust the x and y properties of the loader instance.
This is because the Loader class is a subclass of the DisplayObject which means you access the properties.
//This will offset the load SWF file, so be appear at (20, 20) on the stage.
my_Loader.x = 20;
my_Loader.y = 20;

The above method for loading an external SWF file is not the best as it does not wait until the file is completed loading. So, to fix this problem the code will need to be modified. An event listener will have to be attached to the loader instance to listen to when the file has completed loading. I have also added a listener to detect any errors when loading a file, so a message will be displayed if the file is not found.
//This creates a new instance of the loader object.
var my_Loader:Loader = new Loader();

//This creates a new instance of the URLRequest object that contains the
//path to the external SWF.
var my_url:URLRequest=new URLRequest("example.swf");

//These listeners detect when the file has finished loading, and if the
//correct file is loaded.
my_Loader.contentLoaderInfo.addEventListener(Event.COMPLETE, finishLoading);
my_Loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, errorHandler);

//The load method then loads the SWF file into the loader object.
my_Loader.load(my_url);

//This function adds the external SWF to the stage.
function finishLoading(loadEvent:Event) {
addChild(loadEvent.currentTarget.content);
}

//This function displays a message if the file is not found.
function errorHandler(errorEvent:Event):void {
trace("file missing");
}

Note: The loaded SWF file will have the same fps as the parent movie. So, if the parent movie is 25fps the loaded SWF will also be 25fps.

9 comments:

Anonymous 22 November 2009 11:47  

how will the loaded swf communicate to movieclip in the main movie.

iliketo 23 November 2009 03:37  

You have to create a new instance of the loaded swf, so your code would look like

var loadedSWF = Object( my_Loader.content );

Garrett 24 November 2009 06:05  

so how would you take the top code

//This creates a new instance of the loader object and adds it to the stage.
var my_Loader:Loader = new Loader();
addChild(my_Loader);

//This creates a new instance of the URLRequest object that contains the path
//to the external swf. The load method then loads the SWF file into the loader
//object.
var my_url:URLRequest=new URLRequest("example.swf");
my_Loader.load(my_url);


and load it into a movie clip?
thanx!

iliketo 24 November 2009 13:21  

@Garrett

var my_mc:MovieClip = new MovieClip();
var my_Loader:Loader = new Loader();
my_mc.addChild(my_Loader);
addChild(my_mc);

var my_url:URLRequest=new URLRequest("example.swf");
my_Loader.load(my_url);

natalie 1 December 2009 15:22  

Hi, I am VERY new at AS and working with Flash. I have done the following:

1. created a folder with the .fla file holding the AS to load the external .swf

2. loaded the .swf into the same folder

3. changed your text
var my_url:URLRequest=new URLRequest("example.swf");
to show the name of my .swf file where "example.swf" is

4. then I created my .swf from the .fla file and I get the error "file missing"


I would like to create a portfolio that includes buttons that open up different .swf files when clicked. Is this possible? Is this the best method?

Thanks so so so so much for a speedy response (I have to get this started tomorrow AM).

natalie 1 December 2009 15:26  

OK, sorry, I just sent you a message. I forgot to save the .fla and put it in the same folder, so NOW it does work.

So my real question is: how would I call this .swf with a button click?

My other question is: How would I call numerous .swf files using different buttons from the same main timeline?

Thanks again!!!

iliketo 2 December 2009 04:37  

@natalie

You need to add an event listener to the button.

analytics 20 January 2010 08:13  

so, for making the loaded Movie (example.swf) access a movieclip in main file, let´s say a mc "trigger", How would the listener code look like?
Please complement the code presented in this article for expanding comunication between loaded objects & main stage

iliketo 20 January 2010 10:14  

@analytics

I don't understand your question, can you provide a clearer explanation of your problem.

  COPYRIGHT © 2010 · ILIKE2FLASH · Theme by Ourblogtemplates

Back to TOP