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();