0
votes

Ok, so I'm a beginner at AS3 and Flash and I managed to put this code together for an animation. A Button called start_btn is supposed to start and stop a movieclip called main_mc. On the first click of the Button, the Movieclip is supposed to play (which it does), however on the second click, the movie stops in the middle of its animation (which I don't want). My question is, when you click the Button a second time, how can i get the Movieclip to finish playing its animation then stop on the last frame?

I thought about using if (main_mc.currentFrame == main_mc.totalFrames); {main_mc.stop(); but the Movieclip still does not stop on the last frame. The Movieclip itself also has a gotoAndPlay(2); command on the last frame so that the animation repeats before the Button is clicked a second time.

here is the code i have:

`start_btn.addEventListener(MouseEvent.CLICK, mainaniS);

function mainaniS(event:MouseEvent):void 
{
    main_mc.play();
    start_btn.removeEventListener(MouseEvent.CLICK, mainaniS);
    start_btn.addEventListener(MouseEvent.CLICK, mainaniSt);
    }
function mainaniSt(event:MouseEvent):void
{
    if (main_mc.currentFrame == main_mc.totalFrames);
        {main_mc.stop();}
    start_btn.removeEventListener(MouseEvent.CLICK, mainaniSt);
    start_btn.addEventListener(MouseEvent.CLICK, mainaniS);
    }`
2

2 Answers

0
votes

Try main_mc.gotoAndStop(main_mc.totalFrames).

0
votes

I was going to provide a quick and dirty solution, but decided instead to try and explain a few of the issues with your current implementation and attempt to refactor and explain and better one. Unfortunately I don't have access to Flash right now, so the code is untested.

You're adding and removing event listeners often, which is generally a bad idea. Instead, since you're using a single button to perform multiple functions it would make sense to track the button state in a separate variable. In this case, a boolean for whether or not the movieclip is currently playing.

var playing:Boolean;

Now we can combine the mainaniS and mainaniSt into one and perform a different action based on whether or not the movieclip is playing, and just keep the one eventlistener on the button. I've also taken the liberty of naming the method something more meaningful:

start_btn.addEventListener(MouseEvent.CLICK, onStartClick);

function onStartClick(event:MouseEvent):void 
{
    if(playing) {
        playing = false;
    }
    else {
        playing = true;
        main_mc.play();
    }
}

You may be wondering why we don't call main_mc.stop() in the first block: the reason is that you don't want to stop the movieclip as soon as you click the button, but after the movieclip has finished playing if the button has been clicked. Therefore, we just set playing to false to indicate that we want it to stop later.

Finally, we need to make sure the movieclip stops upon completion, but only if playing is false. To do this we add a listener to movieclip that is called every frame, and checks whether playing is false, and if it's on the last frame. Note that the last frame is actually totalFrames - 1: this is because the frame numbers start from zero rather than one (i.e. if totalFrames is 3, the frame numbers will be 0, 1, 2).

main_mc.addEventListener(Event.ENTER_FRAME, animate);

function animate(event:Event):void {
    if(!playing && main_mc.currentFrame == main_mc.totalFrames - 1) {
        main_mc.stop();
    }
}

All the refactored code together:

var playing:Boolean;
start_btn.addEventListener(MouseEvent.CLICK, onStartClick);
main_mc.addEventListener(Event.ENTER_FRAME, animate);

function onStartClick(event:MouseEvent):void 
{
    if(playing) {
        playing = false;
    }
    else {
        playing = true;
        main_mc.play();
    }
}

function animate(event:Event):void {
    if(!playing && main_mc.currentFrame == main_mc.totalFrames - 1) {
        main_mc.stop();
    }
}