views:

38

answers:

2

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. Is there a way to do this without downloading additional classes?

Cheers

+1  A: 

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.

//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') );
}
PatrickS
A: 

Subscribe to the stage's Event.ENTER_FRAME event and check the currentFrame property of your created movie clip.

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);
   }
}
Richard Szalay