actionscript-3flash-cs5.5

How to make a nested mc play without stopping or looping AS3 CS5


I'm having a problem getting a nested mc to play without looping.

On the main stage I have a mc with instance name smash1_mc.

Within that mc I have another mc with instance name box1_mc. It has a simple tween with a stop action on frame 1. I've placed the label implode on frame 2 of box1_mc.

What I want to have happen is for implode to play when the smash1_mc playhead reaches frame 30; so I've placed a keyframe on frame 30 in the actions layer of smash1_mc. In the actions panel of that keyframe I have tried a few different things.

//When I use the code below, the it only plays the first frame of the "implode" //label instead of the full timeline of box1_mc

addEventListener(Event.ENTER_FRAME, boxSmash);

function boxSmash(event:Event):void
{   
    box1_mc.gotoAndPlay("implode");
}

//When I use 'play();' instead of gotoAndPlay("implode"); like this…

addEventListener(Event.ENTER_FRAME, boxSmash);

function boxSmash(event:Event):void
{
    box1_mc.play();
}

box1_mc plays completely but it loops, despite having a stop(); action on the last frame. I don’t really care which mechanism I use, I just want the frame event to trigger one complete play of box1_mc without box1_mc looping independently thereafter. It seems like something simple. But I don't know what's wrong.

I can send the whole .fla file if this is confusing. I'm actually working on a bigger thing for a training project but I made this simple .fla to demonstrate the problem I'm having with the parent timeline controlling the child timeline.


Solution

  • This is really a simple logic. Whatever code you put in an enterframe will constantly run at each frame, so if you ask a mc to go to frame 2 then that's what that mc will do constantly: going to frame 2. Doesn't matter if that mc has 1000 frames to play, you tell it to go to frame 2 and that's what it does. Get it?

    So first case: box1_mc.gotoAndPlay("implode");

    Correctly at each frame your mc gotoandplay the label "implode". That might not be what you want but that's what you ask the code to do.

    Second case: box1_mc.play();

    Correctly the mc does what you ask which is: no matter what as, long as the enterframe runs you keep on playing. And that's what the mc does.

    In both cases the mc play correctly the way you ask it to. Now that you know that as long as the enterframe runs the code will repeat and the mc will do what the code says you should be able to figure out a way to stop that?

    EDIT: to answer your comment. It depends on whether or not you really need an enterframe. In the example you show you don't but that might be just an example. If you don't need an enterframe then don't use it and just call directly your mc and make it play. If you do need an enterframe then you can either stop it:

    addEventListener(Event.ENTER_FRAME, boxSmash);
    
    function boxSmash(event:Event):void
    {   
        box1_mc.gotoAndPlay("implode");
        removeEventListener(Event.ENTER_FRAME, boxSmash);
    }
    

    or you can check whether the mc is already playing

    function boxSmash(event:Event):void
    {   
        if(!box1_mc.isPlaying)
        {
            box1_mc.gotoAndPlay("implode");
        }
    }
    

    or you can create your own check:

    var mymcisplaying:Boolean;
    function boxSmash(event:Event):void
    {   
        if(!mymcisplaying)
        {
            box1_mc.gotoAndPlay("implode");
            mymcisplaying = true;
        }
    }
    

    and there's even hundreds of different way to do this so here's just a few.