This one-of-will-be-many-articles is covering the first steps of loading and unloading SWFs properly in Actionscript 3. The follow-up articles will discuss dynamical loading-managements, bulk loading and general questions about caching, the garbage collector and the likes. Well, as said we'll just talking about simple load and a clean unload, as I want to give beginners the chance to follow the upcoming ones and maybe get interested in improving their loading-techniques - as there is much you don't know yet about behind the scenes ;-)
For those interested in loading multiple SWFs and displaying it on click check out #2 of this article series.
The finer Art of Loading (#2): Simple load and display multiple SWFs
So without many words here's the snippet - I hope it is pretty self-explaining. It's written to be directly on a frame in your timeline - as I guess the guys using external classes can adept this snippet for their use, don't they? :) Since FP10 has released many things have been simplified as the unloadAndStop()-method does a good job.
Here we go:
-
var _swfLoader:Loader;
-
var _swfContent:MovieClip;
-
-
loadSWF("01.swf");
-
-
function loadSWF(path:String):void {
-
var _req:URLRequest = new URLRequest();
-
_req.url = path;
-
-
_swfLoader = new Loader();
-
setupListeners(_swfLoader.contentLoaderInfo);
-
-
_swfLoader.load(_req);
-
}
-
-
function setupListeners(dispatcher:IEventDispatcher):void {
-
dispatcher.addEventListener(Event.COMPLETE, addSWF);
-
dispatcher.addEventListener(ProgressEvent.PROGRESS, preloadSWF);
-
}
-
-
function preloadSWF(event:ProgressEvent):void {
-
var _perc:int = (event.bytesLoaded / event.bytesTotal) * 100;
-
// swfPreloader.percentTF.text = _perc + "%";
-
}
-
-
function addSWF(event:Event):void {
-
event.target.removeEventListener(Event.COMPLETE, addSWF);
-
event.target.removeEventListener(ProgressEvent.PROGRESS, preloadSWF);
-
-
_swfContent = event.target.content;
-
_swfContent.addEventListener("close", unloadSWF);
-
-
addChild(_swfContent);
-
}
-
-
function unloadSWF(event:Event):void {
-
_swfLoader.unloadAndStop();
-
-
removeChild(_swfContent);
-
_swfContent = null;
-
}
A short addition as many have asked: To dispatch the Event in the loaded SWF just use the following code within your loaded SWF:
-
var _closeEvent:Event = new Event("close", true, false);
-
closeBtn.addEventListener(MouseEvent.CLICK, dispatchUnload);
-
-
function dispatchUnload(event:MouseEvent):void {
-
dispatchEvent(_closeEvent);
-
}
Feel free to ask if any line is not clear or making sense :)



November 11th, 2009
Marvin Blase
Posted in
Does your blog have a contact page? I'm having trouble locating it but, I'd like to send you an email. I've got some recommendations for your blog you might be interested in hearing. Either way, great blog and I look forward to seeing it expand over time.
i need to load second file after the first file was completely loaded. can any one help
You are in reality a good webmaster. The site loading speed is amazing. It kind of feels that you're doing any unique trick. Also, The contents are masterwork. you've performed a excellent process on this matter!