I recently built a simple class called FrameScriptManager that allows you to set actions on any frame referenced by number or label at runtime from within a bound class. This allows developers to associate code with specific points in timeline animations or transitions, without having to modify the FLA. Because you can reference frames by label, the developer also does not have to worry about designers changing the length of the animation, as long as the label remains in the appropriate location.
This class relies on the undocumented addFrameScript method in AS3, which should be fine as Flash CS3 uses it to insert frame actions, but you should be aware that there is a dependency on an undocumented method prior to using it.
I also built an experimental version of the class that adds additional features, like reverting the frame script and getting or calling the original script. This would have been handy to allow developers to add their own frame actions, but still call the original timeline actions. Unfortunately, because the frameN methods (autogenerated by FlashCS3 for frame actions) are placed in the internal space, these features can only be used on movieclips in the same package as the FrameScriptManager class. As such, I wouldn’t recommend using this version, but I have included it for reference.
Here’s a simple example of using FrameScriptManager:
var fsm:FrameScriptManager = new FrameScriptManager(myMC); trace("lbl1 is on frame: "+fsm.getFrameNumber("lbl1"); fsm.setFrameScript("lbl1",myMethod); |
Note that you should never call setFrameScript from within a frame script â it causes the frame script to re-execute which results in an infinite loop. The safest approach is to set all your frame scripts in your constructor. It is possible to set scripts based on user interaction and other events, but you must keep the above issues in mind.
You can download FrameScriptManager, a demo file, and the experimental version by clicking here.
Thanks for sharing!
COOL , we (development) work a lot with animators/ designers, so this is very handy indeed ..
thnx !
To bad yo cant add frames to the timeline with actionscript on the fly….
Thanks for the post Grant 😉
Nice one!
Is there any way to add parameters to the method call?
Hi Grant,
I’ve found an error in the code of the experimental version.
Line 71 is:
var tpkg:String = getQualifiedClassName(this);
but should actually be:
var tpkg:String = getQualifiedClassName(target);
The error doesn’t break anything, but causes the wrong error message sometimes.
Cheers,
Jonathan Hopcroft