1

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.

Sergey4
  • 13
  • 2

1 Answers1

2

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

BadFeelingAboutThis
  • 14,445
  • 2
  • 33
  • 40
  • BTW... maybe you can show me another (simple) solution that has the same effect ? – Sergey4 Sep 25 '18 at 18:22
  • @Sergey4 The simplest (and the most Flash-y) way to achieve what you want is just to put the script into the 51-th frame on timeline. – Organis Sep 25 '18 at 18:28
  • @Sergey4 - The main other way to do it through code at runtime, is using an enterframe handler which watches every frame to see when you hit frame 51, it's a much uglier solution – BadFeelingAboutThis Sep 25 '18 at 21:46