Emmy
Emmy

Reputation: 13

How do I make a looping moviescript finish before stopping it?

I'm animating a leaking object where water drops are falling down. I want the leaking to stop as soon as I click on a different object. When I use stop(); it just stops and the water drops are stuck in the air. Of course I don't want this, how do I fix it?

Bloempoteetkamer.addEventListener(MouseEvent.CLICK, rotplanteetkamer);

Bloempoteetkamer.buttonMode=true;
Bloempoteetkamer.mouseChildren=false;

function rotplanteetkamer(evt:MouseEvent){
    Planteetkamer.play()
    Druppel.stop()
    Bloempoteetkamer.removeEventListener(MouseEvent.CLICK, rotplanteetkamer);
    Bloempoteetkamer.buttonMode=false;
    stop();
}

Bloempoteetkamer is the object that you have to click to stop the leaking. Druppel is the waterdroplets that are falling down. Planteetkamer is a different moviescript that plays when I click on Bloempoteetkamer.

Upvotes: 1

Views: 64

Answers (2)

VC.One
VC.One

Reputation: 15936

You likely want to use EnterFrame option to check, during playback of the MClip (Druppel), if it has actually reached the final frame and only then should the MClip stop().

Try like this :

Bloempoteetkamer.addEventListener(MouseEvent.CLICK, rotplanteetkamer);

Bloempoteetkamer.buttonMode=true;
Bloempoteetkamer.mouseChildren=false;

function rotplanteetkamer(evt:MouseEvent){
    Planteetkamer.play();
    //Druppel.stop();
    Druppel.addEventListener(Event.ENTER_FRAME, check_if_LastFrame);
    Bloempoteetkamer.removeEventListener(MouseEvent.CLICK, rotplanteetkamer);
    Bloempoteetkamer.buttonMode=false;
    stop();
}


function check_if_LastFrame(target : Event) : void
{
    if (target.currentFrame == target.totalFrames) 
    {
        Druppel.stop();
        Druppel.removeEventListener(Event.ENTER_FRAME, check_ifLastFrame);
    }
}

Upvotes: 3

BadFeelingAboutThis
BadFeelingAboutThis

Reputation: 14406

Another more compact option is to use AddFrameScript. The addFrameScript method injects code onto a specific frame of a MovieClip as if you had done so using the tools in Adobe Animate.

Here is an example that creates a reusable function to stop a movie clip when it next reaches it's last frame:

function stopClipAfterLastFrame(clip:MovieClip):void {
    //addFrameScript to the last frame that calls an inline function
    clip.addFrameScript(clip.totalFrames-1, function(){
        clip.stop(); //stop the clip
        clip.addFrameScript(clip.totalFrames-1, null); //remove the frame script so it doesn't stop again the next time it plays
    });
}

So, if you called stopClipAfterLastFrame(Druppel) that would stop it once it reaches the last frame.

keep in mind addFrameScript is an undocumented feature - which means it may on some future flash player/AIR release not work anymore - though that's very unlikely at this point. It's also very important to remove your frame scripts (by passing null as the function) to prevent memory leaks.

Upvotes: 1

Related Questions