rss twitter

The finer Art of Loading (#1): Simple load & unload

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:

Actionscript:
  1. var _swfLoader:Loader;
  2. var _swfContent:MovieClip;
  3.  
  4. loadSWF("01.swf");
  5.  
  6. function loadSWF(path:String):void {
  7.    var _req:URLRequest = new URLRequest();
  8.    _req.url = path;
  9.  
  10.    _swfLoader = new Loader();
  11.    setupListeners(_swfLoader.contentLoaderInfo);
  12.  
  13.    _swfLoader.load(_req);
  14. }
  15.  
  16. function setupListeners(dispatcher:IEventDispatcher):void {
  17.    dispatcher.addEventListener(Event.COMPLETE, addSWF);
  18.    dispatcher.addEventListener(ProgressEvent.PROGRESS, preloadSWF);
  19. }
  20.  
  21. function preloadSWF(event:ProgressEvent):void {
  22.    var _perc:int = (event.bytesLoaded / event.bytesTotal) * 100;
  23.    // swfPreloader.percentTF.text = _perc + "%";
  24. }
  25.  
  26. function addSWF(event:Event):void {
  27.    event.target.removeEventListener(Event.COMPLETE, addSWF);
  28.    event.target.removeEventListener(ProgressEvent.PROGRESS, preloadSWF);
  29.  
  30.    _swfContent = event.target.content;
  31.    _swfContent.addEventListener("close", unloadSWF);
  32.  
  33.    addChild(_swfContent);
  34. }
  35.  
  36. function unloadSWF(event:Event):void {
  37.    _swfLoader.unloadAndStop();
  38.  
  39. removeChild(_swfContent);
  40.    _swfContent = null;
  41. }

A short addition as many have asked: To dispatch the Event in the loaded SWF just use the following code within your loaded SWF:

Actionscript:
  1. var _closeEvent:Event = new Event("close", true, false);
  2. closeBtn.addEventListener(MouseEvent.CLICK, dispatchUnload);
  3.  
  4. function dispatchUnload(event:MouseEvent):void {
  5.    dispatchEvent(_closeEvent);
  6. }

Feel free to ask if any line is not clear or making sense :)

53 Responses to “The finer Art of Loading (#1): Simple load & unload”

  1. Ernie Rapin says:

    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.

  2. karthik says:

    i need to load second file after the first file was completely loaded. can any one help

  3. Mac Adobe says:

    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!

Leave a Reply

Powered by WordPress | Free T-Mobile Phones for Sale | Thanks to Palm Pre Blog, Video Game Music and Get Six Pack Abs