9/7/2011 9:33:28 AM
Title:
DispatchEvent not working
I have parent movieclip. It loads a child movieclip. If one tries to play just the child movieclip alone(without loading in parent movieclip), it is supposed to stop at frame 2, but if someone tries to load it from parent movieclip, it is supposed to play animations located on frame 3. When loading is complete, the child movieclip dispatches a custom event "showPL" and the parent movieclip is supposed to play animations on frame 3 by using code: MovieClip(myloader.content).gotoAndStop(3);. The problem I am facing is that it stops at frame 2, it does not play animations on frame 3. Thanks in advance, any help would be welcomed.
CODE ON PARENT MOVIECLIP
var myloader:Loader = new Loader();
part8_btn.addEventListener(MouseEvent.CLICK, loadF8);
function loadF8(e:MouseEvent):void{
SoundMixer.stopAll();
myloader.load(new URLRequest("part8.swf"));
stage.addChild(myloader);
myloader.contentLoaderInfo.addEventListener(Event.COMPLETE, swfLoaded);
function swfLoaded(event:Event):void {
myloader.content.addEventListener("showPL", showPL_handler);
}
function showPL_handler(event:Event):void {
MovieClip(myloader.content).gotoAndStop(3);
}
}
myloader.contentLoaderInfo.addEventListener(Event.COMPLETE, movieLoaded);
function movieLoaded(event:Event):void {
myloader.x=0;
myloader.y=0;
myloader.scaleX=1;
myloader.scaleY=1;
stage.addChild(myloader);
}
//CODE ON CHILD MOVIECLIP
addEventListener(Event.ENTER_FRAME, loaderF);
function loaderF(e:Event):void{
var toLoad:Number = loaderInfo.bytesTotal;
var loaded:Number = loaderInfo.bytesLoaded;
var total:Number = loaded/toLoad;
if(loaded == toLoad){
removeEventListener(Event.ENTER_FRAME, loaderF);
dispatchEvent(new Event("showPL"));
gotoAndStop(2);
}else{
preloader_mc.preloaderFill_mc.scaleX = total;
preloader_mc.percent_txt.text = Math.floor(total*100) + "%";
preloader_mc.ofBytes_txt.text = loaded + "bytes";
preloader_mc.totalBytes_txt.text = toLoad + "bytes";
}
}
9/9/2011 10:46:51 PM
I had faced a similar issue long time back. The solution I did was to add a small time delay to move it to frame3. It seems that the when we try to move playhead to some frame, the movie is not initialized properly, its just loaded. So it does not behave in a proper way.
I called the "gotoAndStop(3)" after 500 ms time delay in complete event. So it seems to be enough time to allow loaded swf to initialize. I hope it works for you, let me know