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





