flash event listener for movieclip end?

前端 未结 8 1203
执念已碎
执念已碎 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:30

    You can create a custom MovieClip class that dispatches an event when the movie clip object is on the last frame. You can then make the custom MovieClip class the base class of your movie clip animation:

    CustomMovieClip.as:

    package display 
    {
        import events.TimelineEvent;
        import flash.display.MovieClip;
        import flash.events.Event;
    
        public class CustomMovieClip extends MovieClip
        {
            private var _isLastFrame:Boolean;
    
            public function get isLastFrame():Boolean { return _isLastFrame }
    
            public function CustomMovieClip()
            {
                init();
    
            }// end function
    
            private function init():void
            {
                addEventListener(Event.ENTER_FRAME, onEnterFrame);
    
            }// end function
    
            private function onEnterFrame(e:Event):void
            {
                if (!_isLastFrame)
                {
                    if (currentFrame == totalFrames) 
                    {
                        dispatchEvent(new TimelineEvent(TimelineEvent.LAST_FRAME));
                        _isLastFrame = true;
    
                    }// end if
    
                }
                else 
                {
                    if (currentFrame != totalFrames) _isLastFrame = false;
    
                }// end else
    
            }// end function
    
        }// end class
    
    }// end package
    

    TimelineEvent.as:

    package events
    {   
        import flash.events.Event;
    
        public class TimelineEvent extends Event 
        {
            public static var LAST_FRAME:String = "lastFrame";
    
            public function TimelineEvent(type:String, bubbles:Boolean=false, cancelable:Boolean=false) 
            { 
                super(type, bubbles, cancelable);
    
            }// end function
    
            public override function clone():Event 
            { 
                return new TimelineEvent(type, bubbles, cancelable);
    
            }// end function
    
        }// end class
    
    }// end package
    

    Main.as(document class):

    package 
    {
        import display.CustomMovieClip;
        import events.TimelineEvent;
        import flash.display.Sprite;
        import flash.events.Event;
    
        public class Main extends Sprite 
        {
            public function Main():void 
            {
                if (stage) init();
                else addEventListener(Event.ADDED_TO_STAGE, init);
    
            }/// end function
    
            private function init(e:Event = null):void 
            {
                removeEventListener(Event.ADDED_TO_STAGE, init);
    
                var customMovieClip:CustomMovieClip = new CustomMovieClip();
                customMovieClip.addEventListener(TimelineEvent.LAST_FRAME, onCustomMovieClipLastFrame);
                customMovieClip.play();
    
            }// end function
    
            private function onCustomMovieClipLastFrame(e:TimelineEvent):void
            {
                var customMovieClip:CustomMovieClip = CustomMovieClip(e.target);
                customMovieClip.removeEventListener(TimelineEvent.LAST_FRAME, onCustomMovieClipLastFrame);
    
                trace(customMovieClip.isLastFrame); // output: true
    
            }// end function
    
        }// end class
    
    }// end package
    

提交回复
热议问题