Search code examples
flashactionscriptmovieclip

how to ristrict a movie clip to play only once and move to another movie clip using class?


I ve been working on an app in which on the main screen there is a movie clip ball_1 which repeats itself, as soon as any button is pressed another movie clip ball_2 begins and the previous movie clip ball_1 disappears. I want ball_2 to play only once,disappear, and the movie clip ball_1 to return back to the main screen. I am using class based scripting.

Current Code:

BTN_1.addEventListener(MouseEvent.CLICK,playClip_1); 

function playClip_1(e:MouseEvent):void {
    ball_2.visible = true; 
    ball_2.gotoAndPlay(2); 
    ball_1.visible = false; 
}

Solution

  • Ok, I think there is just barely enough information to answer this now.

    First, you'll want two function, one to show ball_1, one to show ball_2

    function playClip_1(e:MouseEvent = null):void {
        ball_2.visible = true; 
        ball_2.gotoAndPlay(2); 
        ball_1.visible = false; 
        ball_1.stop(); //no sense having it keep playing when not visible
    }
    
    //function to call when ball_2 finishes it's timeline
    function clip1Complete(e:Event = null):void {
        ball_2.visible = false;  
        ball_1.visible = true; 
        ball_1.play();
    }
    

    Now, you need a way for ball_2 to call the clip1Complete function when it gets to the end of it's timeline.

    The best way, would be to use an Event, in the same code context as above, put this:

    ball_2.addEventListener(Event.COMPLETE, clip1Complete);
    

    Then, on the very last frame of the ball_2 timeline, put this:

    stop;
    dispatchEvent(new Event(Event.COMPLETE));
    

    Alternatively, you could forgo the event listener and call the function directly on the last frame of ball_2 like so:

    stop();
    MovieClip(parent).clip1Complete();