开发者

Flash AS3 - Wait for MovieClip to complete

I'm writing some code that (I think) requires sequentially playing MovieClip objects. I would like to be able to leave this at a single frame and using only external source files.

The way I wish to use this is to switch between sections of my program.

remove current child

construct movieclip

add new child

wait for completion of new child and goto next step

Is this posisble?

Edit: Other than the ENTER_FRAME method. 开发者_开发百科Running a handler every frame seems wasteful when a signal surely could be sent when the clip reaches its final frame.


You could use the addFrameScript method.

http://www.kirupa.com/forum/showpost.php?p=2098268&postcount=318

MovieClip.addFrameScript(frame:int, method:Function, [frame:int, method:Function...]):void;

Something like this :

// This will be called on the last frame.
function myfunc():void{
    // Do stuff
}
// ** The frame number is zero-based.
myclip.addFrameScript(myclip.totalFrames-1, myfunc);


Look for currentframe and totalframes MovieClip properties on livedocs, use them to check on an ENTER_FRAME event wether the active movieclip is still playing or has ended.


You could dispatch a complete event when a MovieClip reaches its last frame , but I'm not sure that's a better solution , because you would need to implement the event dispatching on each MovieClip you need to play.

It seems easier and more scalable to implement a method which would work with whatever MovieClip is loaded. In this example, you would have set an allMovies variable as an Array that would contain all your MovieClips and a movieIndex integer .

 function loadMovieClip(mc:MovieClip):void
 {
     mc.addEventListener(Event.ENTER_FRAME , enterFrameListener );
     addChild( mc);
 }

 function enterFrameListener(event:Event):void
 {
      var mc:MovieClip = event.currentTarget as MovieClip;

      if( mc.currentFrame == mc.totalFrames )
      {
           mc.removeEventListener(Event.ENTER_FRAME , enterFrameListener );
           removeChild(mc);

           ++movieIndex;

           if( movieIndex < allMovies.length )
             loadMovieClip(allMovies[movieIndex] );
      }
  }


or you can dispatch some event on the needed frame like this:

// In (frame 15 of 250):
super.dispatchEvent(new Event("CUSTOM_EVENT_TYPE", true));

//In as file:
_myClip.addEventListener("CUSTOM_EVENT_TYPE", onCustomListener); 
... 
private function onCustomListener(event : Event) : void {
    event.stopImmediatePropagation(); 
    //your staff 
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜