actionscript-3flashadobecs3

Adobe Flash CS3 - "do something" only when playhead hit a defined keyframe


I would like to know how to create in Adobe Flash CS3 a button that will execute a function (like gotoAndPlay(51) but ONLY when playhead hit a defined keyframe from timeline. Here is a drawing that does explain better what I want to do

So after I click the button, it will wait until the playhead hit the closest defined keyframe, only then it will execute my desired function.

Any help is appreciated.


Solution

  • There are a few ways to accomplish this. The most succinct way is to use an undocumented* feature called addFrameScript

    What you can do with that method, is place code on a specific frame at runtime.

    So, you could do something like:

    //listen for the click on your button
    myButton.addEventListener(MouseEvent.CLICK, buttonClickHandler);
    
    //which frame should the gotoAndPlay run on?
    var targetFrame:int = 19; //this is actually frame 20, since frames are 0 based in script
    
    //this function will run on the above frame
    function goto51(){
        //goto frame 51
        gotoAndPlay(51);
        //remove the frame script
        addFrameScript(targetFrame, null);
    }
    
    function btnClickHandler(e:Event) {
        //use addFrameSCript to run the function goto51 on the target frame
        addFrameScript(targetFrame, goto51);
    }
    

    *-one should point out that using undocumented features of a language comes with risk of the feature being taken out on a future version, however given the current life cycle stage of AS3, this is very unlikely