flash event listener for movieclip end?

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

    I do not believe there is an event broadcast at the end of a movieclip, you could always run a script on the last frame of an animation to execute what you want. If you really want to use events, the last frame of the movieclip could execute a script that utilizes dispatchEvent() to send a custom even which can be picked up.

    I'm not sure from your post if you have used eventhandlers before, so here's a tutorial on that (I'm not good at explaining, sorry!): http://edutechwiki.unige.ch/en/ActionScript_3_event_handling_tutorial

    and for dispatchEvent(): http://www.actionscript.org/resources/articles/204/1/Using-EventDispatcher/Page1.html

    0 讨论(0)
  • 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
    
    0 讨论(0)
提交回复
热议问题