简体   繁体   English

当外部SWF到达第X帧时,如何卸载?

[英]When External SWF has reached Frame X, how do I unload it?

Here's my swf loading code: 这是我的瑞士法郎加载代码:

function loadBall(e:MouseEvent):void{
var mLoader:Loader = new Loader();
var mRequest:URLRequest = new URLRequest("ball.swf");
mLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onCompleteHandler);
mLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgressHandler);
mLoader.load(mRequest);
}

function onCompleteHandler(loadEvent:Event){
    currentMovie = MovieClip(loadEvent.currentTarget.content) 
    addChild(currentMovie);
    trace(loadEvent);
}
function onProgressHandler(mProgress:ProgressEvent){
var percent:Number = mProgress.bytesLoaded/mProgress.bytesTotal;
trace(percent);
}

I wish to detect if ball.swf has reached frame 244 and then unload it. 我希望检测ball.swf是否已到达第244帧,然后将其卸载。 Is there a way to do this without downloading additional classes? 有没有一种方法可以下载其他类?

In frame 244 of the ball movie clip, you could dispatch an event to inform the MainTimeline that frame 244 has been reached, then you will need to delete all references to the ball mc and let garbage collection handle it from there. 在球形影片剪辑的第244帧中,您可以调度一个事件以通知MainTimeline已经到达了244帧,然后您将需要删除对mc的所有引用,并从此处进行垃圾回收。

//in the ball movie clip, on frame 244

this.dispatchEvent( new Event("End of Movie") );

//in the main timeline , after loading the swf

function onCompleteHandler(event:Event):void
{
   //keep the ball movie clip as a local variable
   var ball:MovieClip = event.target.loader.content as MovieClip;
   ball.name = "ball";
   ball.addEventListener( "End of Movie" , remove , false , 0 , true );
   addChild( ball);
}

function remove(event:Event):void
{ 
   event.target.removeEventListener( 'End of Movie' , remove );

   //now you can retrieve the ball mc by its name and remove it from the stage
   this.removeChild( this.getChildByName('ball') );
}

Subscribe to the stage's Event.ENTER_FRAME event and check the currentFrame property of your created movie clip. 订阅舞台的Event.ENTER_FRAME事件,并检查创建的影片剪辑的currentFrame属性。

private static final BALL_END_FRAME : int = 244;

private var _ball : MovieClip;

function onCompleteHandler(event:Event):void
{
   _ball = event.target.loader.content as MovieClip;
   addChild(_ball);

   stage.addEventListener(Event.ENTER_FRAME, onEnterFrameHandler);
}

function onEnterFrameHandler(event:Event):void
{
   if (_ball.currentFrame == BALL_END_FRAME)
   {
      removeChild(_ball);
      stage.removeEventListener(Event.ENTER_FRAME, onEnterFrameHandler);
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM