Monday, 12 April 2010

How to load an external SWF

This is how you can load an external SWF, using the progress event to make some sort of preloader.



import flash.net.URLRequest;
import flash.display.Loader;
import flash.events.Event;
import flash.events.ProgressEvent;


function startLoad() {
   var mLoader:Loader = new Loader();
   var mRequest:URLRequest = new URLRequest(“MouseActions.swf”);
   mLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,onCompleteHandler);
   mLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS,onProgressHandler);
   mLoader.load(mRequest);
}


function onCompleteHandler(loadEvent:Event) {
   addChild(loadEvent.currentTarget.content);
}


function onProgressHandler(mProgress:ProgressEvent) {
   var percent:Number = mProgress.bytesLoaded/mProgress.bytesTotal;
   trace(percent);
}

startLoad();

2 comments:

  1. where is fla source file.. i would like to test with ur fla source file. Can u upload in this website this script source file?

    thanks.

    Mohammad Raihan Mazumder
    Jakarta, Indonesia

    ReplyDelete
  2. @Raihan... First of all thanks for stopping by. I think there's no sense of publishing a source file, since this is the source code, just copy and past to an empty keyframe in the timeline. If you're talking about this code in a class, in pretty much the same thing, just give it the class structure and place the methods private or public... as far as I can see you knew how to do it already, in your website. The difference is that this gives you the percentage of loading in case you want a preloader. Cheers

    ReplyDelete