Joys of dispatchEvent()
On a recent project I had to load two swfs in two seperate movieclips at the same time. For some reason, when I had both Loaders going, only one Event.COMPLETE would fire and I just couldn’t figure out why. I would disable one or the other and it would fire off consistently every time when it’s by itself.
I found that the only way to get around it was to check if the swf has completely loaded in the ProgressEvent listener and manually set off the Event.COMPLETE listener.
private var mcLoader1:Loader = new Loader();
private function loadSwf():void{
var mcRequest:URLRequest = new URLRequest(“URL”);
mcLoader1.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, loadProgress);
mcLoader1.contentLoaderInfo.addEventListener(Event.COMPLETE, loadComplete);
mcLoader1.load(mcRequest);
}
private function loadProgress(evt:ProgressEvent):void {
var percentLoaded:uint = Math.round((evt.bytesLoaded / evt.bytesTotal) * 100);
if (percentLoaded == 100) {
mcLoader1.dispatchEvent(new Event(Event.COMPLETE));
}
}
private function loadComplete(evt:Event):void {
//Do something with mcLoader1
}
You have to be careful when doing this because in the event that the Event.COMPLETE fires as intended, the listener associated it with will get called twice. I have to warn you that this is more of a hack than a fix, but will do you good if you have a similar problem on a tight timeline.
-Car