Search code examples
actionscript-3flash

Playing a sequence of given frames using ActionScript3 Flash


I Am beginner with Flash and ActionScript 3 I have 8 lip code for a character that i did create in different frames, so i want to play my animation frame by frame but with a different order to form a phrase that my character will say I did try it on my own but i did not successed:

    stop();

var tableau = new Array(); 
tableau[0]=2;
tableau[1]=4;
tableau[2]=1;
tableau[3]=7;
tableau[4]=8;
tableau[5]=1;
tableau[6]=7;

for(var i =0;i<tableau.length;i++){
    trace(tableau[i]==this.currentFrame);
    if(tableau[i]==this.currentFrame){
        gotoAndPlay(tableau[i]);
        trace(this.currentFrame);
    }
}

Solution

  • It's pretty much simple. What you need is to subscribe to the special event that fires once per frame and move the playhead once per frame according to the plan.

    stop();
    
    var Frames:Array;
    
    // This will prevent things from overlapping
    // if one of the frames on the list is the
    // current one and playhead will hit here
    // once again (and try to execute code).
    if (Frames == null)
    {
        Frames = [2,4,1,7,8,1,7];
        addEventListener(Event.ENTER_FRAME, onFrame);
    }
    
    function onFrame(e:Event):void
    {
        // Get the next frame index and remove it from the list.
        var aFrame:int = Frames.shift();
    
        // If there are no more frames to show,
        // unsubscribe from the event.
        if (Frames.length < 1)
        {
            removeEventListener(Event.ENTER_FRAME, onFrame);
        }
    
        gotoAndStop(aFrame);
    }