In this tutorial I will show you how to load several external swf files and then move between them with forward and backward button. First we have to import UILoader. Add following code at the beginning:
import fl.containers.UILoader;
Next we will define an array of external swf files. Add following code:
var externalSWF:Array = ["test.swf",
"test1.swf",
"test2.swf",
"test3.swf"
];
Please note that in my case external swf files are in the same directory as the fla file we are creating. If your files are in subdirectories you have to provide a full path (“dir/test.swf”). Next we will define a variable that will tell which swf is current one.
var i:int = 0;
Value is set to 0 as that is our first swf file. Now we will define UIloader, sprite that will hold our swf’s and a movieclip that will hold current swf file that is on the stage.
var thisUILoader:UILoader = new UILoader();
thisUILoader.addEventListener(Event.INIT, loadFinish);
var theLoaderContext:LoaderContext;
var slideshow:Sprite = new Sprite();
var MC:MovieClip = new MovieClip();
After load is finished we will call function loadFinish. But first we have to put our sprite and movieclip to the stage.
addChild(slideshow);
slideshow.addChild(thisMC);
Now we can add function loadFinish.
function loadFinish(e:Event):void {
MC = MovieClip(thisUILoader.content);
thisUILoader.unload();
slideshow.addChild(thisMC);
setChildIndex(slideshow, 0);
backward.addEventListener(MouseEvent.CLICK, backwardF);
forward.addEventListener(MouseEvent.CLICK, forwardF);
}
First we load content of swf file then we add it to slideshow and set index to 0. Now create two simple movieclips named backward and forward. Don’t forget to set instance names of these movieclips to backward and forward. We will use these buttons to move between swf files.
Now we need to add functions for moving forward and backward and we have to add another variable that will count swf files. Add following code to the start of the file where variables are.
var counter:int = 0;
Now add functions for forward and backward.
function backwardF(e:MouseEvent){
i--;
if (i < 0)
i = 5;
nextSWF();
counter++;
}
function forwardF(e:MouseEvent){
if (i == 5)
i = -1;
i++;
nextSWF();
counter++;
}
Now we only have to add function nextSWF.
function nextClip():void {
theLoaderContext = new LoaderContext();
theLoaderContext.applicationDomain = new ApplicationDomain();
thisUILoader.load( new URLRequest(clips[i]),theLoaderContext);
}
This function loads the current swf file. Starting value of i is 0 (first swf in array).
Now add following code at the end to start loading.
nextClip();
And we are finished. You can now load as many swf files as you wish and move between them.
Thank you for reading