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.

Update: You can now load external swf using SWF loader.

If you want to call a function from an externally loaded swf, checkout this tutorial. And if you want to load mutiple external swf's take a look at this tutorial.

47 comments:

Anonymous 22 November 2009 at 11:47  

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

iliketo 23 November 2009 at 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 at 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 at 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 at 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 at 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 at 04:37  

@natalie

You need to add an event listener to the button.

analytics 20 January 2010 at 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 at 10:14  

@analytics

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

Lani 3 March 2010 at 13:55  

First of all ... This is the first comprehensive explanation of loading an external SWF within an SWF in CS3 I have found. Thank you, thank you, thank you!

I am, however having difficulty once it is uploaded to the server.

The external SWF is a photo gallery on the media page of my website. The first time you navigate to the page, the SWF pulls up perfectly. However, if you navigate away from and then back to the media page, the gallery does not load up.

Could I be missing something in my script?

Thanks!

iliketo 5 March 2010 at 10:22  

@Lani

It could be your code. Is it possible for you to post your code?

Lani 5 March 2010 at 11:48  

@iliketo

This is the script I am using for the button event listeners:

[AS]

homeBtn.addEventListener(MouseEvent.CLICK, clickHome);
function clickHome (event:Event):void{
gotoAndPlay("home");
}

eventsBtn.addEventListener(MouseEvent.CLICK, clickEvents);
function clickEvents (event:Event):void{
gotoAndPlay("events");
}

bioBtn.addEventListener(MouseEvent.CLICK, clickBio);
function clickBio (event:Event):void{
gotoAndPlay("biography");
}

mediaBtn.addEventListener(MouseEvent.CLICK, clickMedia);
function clickMedia (event:Event):void{
gotoAndPlay("media");
}

linksBtn.addEventListener(MouseEvent.CLICK, clickLinks);
function clickLinks (event:Event):void{
gotoAndPlay("links");
}

contactBtn.addEventListener(MouseEvent.CLICK, clickContact);
function clickContact (event:Event):void{
gotoAndPlay("contact");
}

[/AS]

As you can see each Event Listener plays a portion of the timeline (named "home", "events" etc.) The external SWF is located on a separate Movie Clip timeline within the "media" portion of the main timeline.

Here is the script I am using within the Movie Clip timeline:

[AS]


var my_Loader:Loader = new Loader();
addChild(my_Loader);

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

my_Loader.x = 90;
my_Loader.y = 135;

[/AS]

I am brand new to Flash, so I hope I have explained everything well enough. I would appreciate any help. Thank you!

iliketo 8 March 2010 at 10:56  

Are you receiving any error messages ?

Daniel 12 March 2010 at 01:38  

Hi,

I am trying to load more than one swf into a fla file but whenever I try this I end up having all playing on top of each other. I have duplicated the events, the functions, the instances...everything but I can't make it work for some reason. Could anyone help me pls? tnx

iliketo 13 March 2010 at 05:13  

@Daniel

Have you tried changing the positions of the loaded SWf's?

Lani 16 March 2010 at 09:08  

I am not receiving any error messages, but I figured out the problem. The external SWF I was using was given to me without the FLV file. It must have been created in AS2, whereas the flash site I created was in AS3. (I discovered in my searches at AS2 and AS3 do not work well together.) So, I had to create a gallery from scratch in AS3 ... but it works now. Thanks for your help!

Phillip 18 March 2010 at 14:16  

Could someone please tell me how to code the loader so that it is behind drop menus that hold the buttons that action swfs to come into the loader. Everything I've done works fine. The buttons call the swf and place it into the loader, it's just that the content of the called swf now covers up the drop menus in the nav bar. Thanks in advance
Phil

Kenney 20 March 2010 at 07:12  

I have no problem with the coding above but for some reason the functionality from the original swf does not work?? Do I have to add an event listener for the stage and if so how??

iliketo 20 March 2010 at 09:19  

@kenney

What sort of functionality does your original swf have?

Kenney 21 March 2010 at 03:53  

thankyou for your quick reply. The swf I'm uploading is a sliding image gallery controlled by buttons of image 1 to image 6. It uploads fine to the main fla...but just won't allow for any interaction.I'm importing a tweener for the image transitions. Does that have to do with it as well. Event listener for stage maybe? I didn't think that should be a problem...I seriously can't figure it out! Please help!

iliketo 21 March 2010 at 13:47  

@Kenney

Firstly, you need to establish if the buttons from the loaded SWF work.

Web 23 March 2010 at 17:53  

Hi

I am absolutely clueless with as3 and web, so this is probably a basic question.

I have tried the code above and it works great. How would I call this function to load external swf files from an html link?

Thanks in advance

M_art 25 March 2010 at 03:19  

Hello all,

I am working on an array of swfs that load onto my stage.

I have set the code (AS3) up so that (in theory) two buttons would control the swfs that appear on the stage.

I have managed to make the forward button move through the array and display swfs in order, however the back button I cannot make work.

What I want is to be able to load an array and use to buttons "next" and "back" to do as they suggest.

Does anyone know the solution to my problem? Or a better way to achieve my goal?

Many thanks for reading!

Code below



var clips:Array = ["box.swf", "content1.swf", "white.swf"];
var index:int = 0;
var arraychecker: Number = 0;

var myLoader:Loader=new Loader ();

var myURL:URLRequest=new URLRequest(clips[index]);
myLoader.load(myURL);

myLoader.x=300;
myLoader.y=150;
addChild(myLoader);


advance_btn.addEventListener(MouseEvent.CLICK, forward);

function forward(event:MouseEvent):void {

trace(clips[arraychecker]);
myLoader.load( new URLRequest(clips[arraychecker]) );
arraychecker = (arraychecker+1)%(clips.length);



back_btn.addEventListener(MouseEvent.CLICK, back);

function back(event:MouseEvent):void {

trace(clips[arraychecker]);
myLoader.load( new URLRequest(clips[arraychecker]) );
arraychecker = (clips.currentFrame)-(arraychecker);


}

iliketo 30 March 2010 at 13:46  

@Web

What do you mean call this function from a html link? Is this even possible?

Web 31 March 2010 at 21:32  

Sorry, I should have explained myself a bit better

I've played around with the code and got it so that the external swf loads after the user clicks a button in flash.

I'd like to be able to get the user to load the external swf by clicking on a link on the HTML file.

I assumed it would be using flashvars and javascript, but am not sure if that's the way to go...

The AS code for the button is below:

function btnClick(event:MouseEvent):void{
SoundMixer.stopAll();
removeChild(loader);

loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onSWFLoaded);
loader.load(movieSWF)
loader.x=Xpos
loader.y=Ypos
addChild(loader)
swfBTN.addEventListener(MouseEvent.CLICK,btnClick);

ivydesign 19 April 2010 at 05:40  

@Web
Can you show how you managed to assign a button to load an external swf?

I`m having great trouble trying to figure this one out.
I miss as2. AS3 in giving me plenty of trouble.

rglittle 3 July 2010 at 07:48  

I am having the same problem as Phillip. When I load a "page" which is an external .swf (AS3, same size as the stage) it loads in front of everything else. Because of this, I can't access any of the buttons once the swf is loaded. Is there a way to set the order of the loaded swf to be behind the navigation?

iliketo 3 July 2010 at 14:22  

@rglittle

You can use the addChildAt() method to add the external swf to a particular depth.

immo 2 January 2011 at 04:34  

hello, how can i removeChild from loaded .swf (child)???
[...not from main .swf that loads the child where removeChild() or unload() is possible...]
i mean something like when buttonCLICKed at child's window... thx

32fe66b8-4ad8-11e0-8e05-000bcdcb471e 9 March 2011 at 21:35  

Hey how do i stop the loaded flash file to appear in all of my pages. Thanks in advance!

iliketo 12 March 2011 at 01:25  

@Above

You can either remove the swf using the removeChild() method or you can set the swf to invisible using the visible property.

iliketo 27 April 2011 at 10:22  

@M_art

Take a look at this tutorial

iliketo 27 April 2011 at 10:27  

@immo

Do you want to remove a child from the loaded swf?

桐维.柳叶 17 August 2011 at 07:31  

i have a question. let say i have file1.swf , file2.swf, and file3.swf, how can i link this 3 files together,

let say suppose file1.swf is the main content file, but as i load file2.swf, i want the file1.swf get replaced by file2.swf. How could i unload file1.swf at that time, so that my file2.swf will become the main content file?

and again, when my current position at file2.swf, and i want it get replaced by file3.swf by clink on a button. so that the i can unload the file2.swf and let the file3.swf become the main content file..
any one have idea on it??
thank you..

iliketo 18 August 2011 at 02:25  

@桐维.柳叶

Take a look at this post.

Piloen 9 October 2011 at 22:32  

Hi, i am newbees in actionscript.
I have a question almost like 桐维.柳叶
BUT without any button trigger.. let say after 10sec the external swf file will be replaced with another external swf file.
Like slide show with external swf's as a content
Thank you

iliketo 16 October 2011 at 01:44  

@Piloen,

Take a look at this post for setting a time delay in Actionscript 3.

LostInFlash999 9 November 2011 at 13:29  

Great instructions, thank you:-)

However I am trying to set the position of the loading .swf by setting the .x and .y values, placing the following lines

my_Loader.x=200;
my_Loader.y=300;

immediately after the declaration of the variable var my_Loader. This has absolutely no effect - the .swf loads into the top left corner and refuses to budge.

What am I missing here?

Thanks for any help.

iliketo 11 November 2011 at 02:06  

It should work. Make sure you don't have a typo.

Maciej 22 November 2011 at 14:40  

Hi,
I've been searching for ages how to create close button for external swf file and I thought that you can help me with this

I know how to load external swf from url (thanks to your previouse posts) but I don't know how to close (unload) this external file.

Thanks

iliketo 23 November 2011 at 02:00  

@Macie J,

You can use the removeChild() method.

Maciej 23 November 2011 at 09:54  

I tried many times the removeChild() method it doesn't work

But i got it done using:
function removeF()

Thanks for your replay anyway

iliketo 24 November 2011 at 03:00  

@Macie J,

You need to add the contents of the loader into a container sprite, then remove the container sprite. Here is an example:


var container:Sprite = new Sprite();
addChild(container);


var my_Loader:Loader = new Loader();
my_Loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loaded);
my_Loader.load(new URLRequest('example.swf'));


function loaded(e:Event):void{
container.addChild(e.target.content);
}



Use, removeChild(container); to remove the swf.

529aabfa-1b7d-11e1-8d2a-000bcdcb2996 30 November 2011 at 10:01  

How do i access the functions that are inside the loaded SWF's document class?

In particular, id like to change a variable value and then call a function.

public var moveDirection:Number = 0;

function moveSprite();

iliketo 1 December 2011 at 03:37  

@Above,

Take a look at this tutorial.

Jerry 13 August 2013 at 23:08  

I'm having an issue with playing an external swfs inside the main swf. Everything loads great, except not on cue. I'm using a simple hack delay actionscript to pause the main timeline until the external swfs load, but it doesn't always sync up when testing in browsers. Is there an AS3 code that I can use to pause the main timeline until the external swf is finish loading and playing? I need to do this multiple times through the movie, btw... Below is the delay and the loadmovie array I'm using. Any help is greatly appreciated!

<--------------//Timer//--------------->

var timer4:Timer = new Timer(1500, 1);
timer4.addEventListener(
TimerEvent.TIMER,
function(evt:TimerEvent):void {
play();
}
);
timer4.start();




<--------------//loadMovie//--------------->

function startLoad()
{
var mLoader:Loader = new Loader();
var mRequest:URLRequest = new URLRequest('flip/note_flip.swf');
mLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onCompleteHandler);
mLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgressHandler);
mLoader.load(mRequest);
}

function onCompleteHandler(loadEvent:Event)
{
addChild(loadEvent.currentTarget.content);
}
function onProgressHandler(mProgress:ProgressEvent)
{
var percent:Number = mProgress.bytesLoaded/mProgress.bytesTotal;
trace(percent);
}
startLoad();

iliketo 14 August 2013 at 14:04  

@Jerry,

Instead of pausing the timeline until the external swfs loads – You should only play the timeline when the swfs have loaded.

  COPYRIGHT © 2014 · ILIKE2FLASH · Theme by Ourblogtemplates

Back to TOP