flash event listener for movieclip end?

前端 未结 8 1180
执念已碎
执念已碎 2021-01-02 09:29

Could anyone advise the best way to trigger a functiont when a movieclip animation finishes? I figure an eventlistener could handle this, but not sure the best way to go abo

8条回答
  •  一向
    一向 (楼主)
    2021-01-02 10:13

    By making use of an ENTER_FRAME listener, you can tell if a MovieClip has readed the end of playback; you can then take this one step further by wrapping it up in a Wrapper class that will perform the monitoring for you:

    public class EndOfMovieClipEventDispatcher extends EventDispatcher
    {
        private var target : MovieClip;
        private var endReachedEvent : String;
    
        public function EndOfMovieClipEventDispatcher(target : MovieClip, endReachedEvent : String = "complete") {
            this.target = target;
            this.endReachedEvent = endReachedEvent;
            target.addEventListener(Event.ENTER_FRAME, onEnterFrameEvent, false, 0, true);
        }
    
        public function destroy() : void {
            target.removeEventListener(Event.ENTER_FRAME, onEnterFrameEvent);
        }
    
        private function onEnterFrameEvent(event : Event) : void
        {
            if (target.currentFrame == target.totalFrames) {
                dispatchEvent(new Event(endReachedEvent));
            }
        }
    }
    

    Usage is pretty straight forward; the call to destroy() is optional thanks to the weak event listener; but recommended if you are finished :)

    new EndOfMovieClipEventDispatcher(myMovieClip).addEventListener(Event.COMPLETE, onMovieClipCompleteEvent);
    myMovieClip.play();
    

提交回复
热议问题