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;
  } 
}

9 comments:

Sonia 22 May 2011 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 11:24  

@Sonia

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

Dfilmon 22 June 2011 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 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 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 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 03:37  

@Konamedia,

Do this

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

Murali 19 January 2012 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 02:44  

@Murali,

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

  COPYRIGHT © 2011 · ILIKE2FLASH · Theme by Ourblogtemplates

Back to TOP