Wednesday, 27 April 2011

Load multiple external swf in AS3

I previously wrote a post about loading a single external swf in Actionscript 3. In this post I will write about loading multiple external swf’s in Actionscript 3. A common problem you face when loading more than one swf is that multiple copies can potentially be loaded. Look at the example below.

btn1.addEventListener(MouseEvent.CLICK, loadSwf1);
btn2.addEventListener(MouseEvent.CLICK, loadSwf2);

function loadSwf1(e:MouseEvent):void{
 var loader1:Loader  = new Loader(); 
 loader1.load(new URLRequest("exampleA.swf"));
 addChild(loader1);
}


function loadSwf2(e:MouseEvent):void{
 var loader2:Loader = new Loader(); 
 loader2.load(new URLRequest("exampleB.swf"));
 addChild(loader2);
 loader2.x = 100; 
}

The two external swf’s are being loading in from a button click. The code above will work, but multiple copies of the swf will be loaded every time you press the buttons which is no good. The solution is to load the swf’s into container sprites and perform a check to see how many child objects exist before loading the swf. In the example below I have loaded the swf’’s into separate containers and added an If statement to check if the number of children is less than one. This means that only one copy of the swf can exist inside the container.

var container1:Sprite = new Sprite();
var container2:Sprite = new Sprite();
addChild(container1);
addChild(container2);

btn1.addEventListener(MouseEvent.CLICK, loadSwf1);
btn2.addEventListener(MouseEvent.CLICK, loadSwf2);

function loadSwf1(e:MouseEvent):void{
 if(container1.numChildren < 1){
  var loader1:Loader = new Loader(); 
  loader1.load(new URLRequest("exampleA.swf"));
  container1.addChild(loader1);
 }
 trace("Number of children in container1 is:" + container1.numChildren);
}


function loadSwf2(e:MouseEvent):void{
 if(container2.numChildren < 1){
  var loader2:Loader = new Loader(); 
  loader2.load(new URLRequest("exampleB.swf"));
  loader2.x = 100;
  container2.addChild(loader2);
 }
 trace("Number of children in container2 is:" + container1.numChildren);
}

Alternatively, if you wish to remove an external swf before loading a new one in. You can repeat the same as above, but you will only need to use one container. Take a look the example below. As you can see I have used a While statement inside the clickHandler function to check that only one child exists inside the container.

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

btn1.addEventListener(MouseEvent.CLICK, clickHandler);
btn2.addEventListener(MouseEvent.CLICK, clickHandler);

function clickHandler(e:Event):void{
 
 while(container.numChildren == 1) container.removeChildAt(0);
 
 switch(e.target.name){
  case "btn1": loadSwf("exampleA.swf"); break;
  case "btn2": loadSwf("exampleB.swf"); break;
 } 
 
 trace("Number of children in container is:" + container.numChildren);
}

function loadSwf(swfName:String):void{
 var loader:Loader = new Loader(); 
 loader.load(new URLRequest(swfName));
 container.addChild(loader);
}

The code below is another method of adding in new swf’s. This time I have not used a container to hold the swfs. The new loaded swf will get overwritten by the old one, so you don’t need to check if there are multiple copies.

btn1.addEventListener(MouseEvent.CLICK, clickHandler);
btn2.addEventListener(MouseEvent.CLICK, clickHandler);

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

function clickHandler(e:Event):void{

  switch(e.target.name){
    case "btn1": loader.load(new URLRequest("exampleA.swf")); break;
    case "btn2": loader.load(new URLRequest("exampleB.swf")); break;
  } 
}

20 comments:

Sonia 22 May 2011 at 07:17  

Great Tutorial! Let's say you have only one button that when clicked will load a movie. You want to make sure it's loads only once. So if I try to click on that button again that movie will not load if it is already display. But in that loaded movie you have a close button. Can you show an example of that?

iliketo 23 May 2011 at 11:24  

@Sonia

The second and third code examples, only load the external swf once.

Dfilmon 22 June 2011 at 07:58  

Nice Iliketo
searched online a lot to find something on this topic, I'm new to as3 and am lovin it, clean resources are hard to come across, you should do video tuts too :)

teokon 27 July 2011 at 08:04  

Awesome tutorial made so simple and easy to understand. What i want to do is add a loading progress bar or text (either will do) with percentage to show when loading the external .swf.
Can anyone point me in the right direction?

iliketo 22 October 2011 at 00:19  

@teokon,

Sorry, late reply. This code should get you started.

var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loaded);
loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, progressHandler);
loader.load(new URLRequest('imageNameHere'));


function progressHandler(e:Event):void {

var percentage:int = (e.target.bytesLoaded / e.target.bytesTotal) * 100;

trace(percentage);

}

function loaded(e:Event):void{
//add code here
}

Konamedia 29 December 2011 at 22:07  

Using your example...

// begin example

btn1.addEventListener(MouseEvent.CLICK, clickHandler);
btn2.addEventListener(MouseEvent.CLICK, clickHandler);

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

function clickHandler(e:Event):void{

switch(e.target.name){
case "btn1": loader.load(new URLRequest("exampleA.swf")); break;
case "btn2": loader.load(new URLRequest("exampleB.swf")); break;
}
}

// end example

How would I make this "loader" automatically start with "exampleA.swf" loaded?

Thanks!

iliketo 30 December 2011 at 03:37  

@Konamedia,

Do this

loader.load(new URLRequest("exampleA.swf"));

Murali 19 January 2012 at 00:08  

Thanks a lot,works perfectly!!!!
Will the last code work for any no.of buttons...

btn1.addEventListener(MouseEvent.CLICK, clickHandler);
btn2.addEventListener(MouseEvent.CLICK, clickHandler);

iliketo 22 January 2012 at 02:44  

@Murali,

Yes, as long as you have the buttons on the stage.

@MRD 29 March 2012 at 11:08  

I've been looking all morning for how to do this - works great! Thank you

bassmaz 31 March 2012 at 01:45  

great tutorial but i have one problem that is the stage move left when the swf loaded
how can i fix this

iliketo 31 March 2012 at 02:39  

@bassmaz,

I not sure why the stage would move when you load an external swf. Can you provide some more information?

bassmaz 31 March 2012 at 03:43  

i think its the swf it self i check it and it move left wen you make it full screen its a free xml gallery

Mighty Flash Gordon 20 April 2012 at 00:15  

Hi,
I was wondering how one would communicate between loaded swf's and thier movie clips.

Lets say I have a swf (_Main.swf) thats loads in five other swf's with mc'sin them. How would I communicated between them.

e.g.
_Main.swf - interface
which has 5 buttons that when a button is pressed tells the loaded swfs to tween thier mc's onto the stage then when another button is pressed tells the loaded swf's mc's to tween out from the stage unload the swf then load in the next swf and tween in the mc's (how do i do this?)
the other swf's would be :-

_Home.swf - home page
which has a text feild and 2 mc's on the stage.

_AboutUs.swf - about us page
which has a 3 text feilds and 2 mc's on the stage.

_Services.swf - services page
which has a 2 text feilds and 4 mc's on the stage.

_Portfolio.swf - portfolio page
which has a 2 text feilds and 10 mc's on the stage.

_ContactUs.swf - contact us page
which has a 5 text feilds and 2 mc's on the stage.

How do I go about communicating between them.....???

iliketo 21 April 2012 at 03:46  

@Mighty Flash Gordon

Take a look at this post .

If it's easier have only one swf to contain everything instead of multiple ones.

Ysong 6 November 2012 at 23:24  

Hi! This is a great tutorial..

I am doing like this..

I have 2 main buttons to my main file as long as I click one of the buttons it will slide from right-to-left then the externally swf will be loaded with transition.

I hope that you could help me as soon as possible..

thanks

iliketo 7 November 2012 at 13:01  

@Ysong,

Yes, this is possible.

eni ismayani 8 January 2013 at 06:11  

woww.. its really help me. thanks for your tutorial. god bless

carlos pinedo 8 November 2013 at 10:02  

Awesome tutorial. Thanks a lot. Still, I have a question, What about the external swf with sound, they keep sounding after you click another button. What can we do to fix this?

iliketo 8 November 2013 at 12:11  

@Carlos,

Try stopping the current sound before you click another buttton.

  COPYRIGHT © 2014 · ILIKE2FLASH · Theme by Ourblogtemplates

Back to TOP