addFrameScript with parameters
Posted: September 20th, 2007 | Author: Ivan Valadares | Filed under: Actionscript3, Code | 5 Comments »There is a little __undocumented__ function that can be very useful sometimes called addFrameScript. It allows you to specify a function that is called when the playhead of the Movieclip timeline enters the specified frame number. This can be a great improvement if you don’t want to write code directly on the timeline or if you don’t want to add an EnterFrame listener to the movieClip.
MovieClip.addFrameScript(frame_number:uint,function_to_call:Function)
Example:
Note: The frame number are zero based (0 to totalframes-1)
This function works well, but it’s impossible to pass arguments to the caller function. After a little research we found Ian Thomas delegate class.
Using delegate class is possible to call a function with arguments in situations that normally you couldn’t. So for addFrameScript we did:
Ah, very nice. Didn’t even think about using a Delegate since its AS3 but its so obvious now. Feels a little weird though
how about doing a something like
addFrameScript(“frame_name”,function)
can that be done?
i think what you want, is something like this :
for (var i:int=0;i < mc.currentLabels.length;i++)
{
if (mc.currentLabels[i].name==”frame_name”)
{
mc.addFrameScript(mc.currentLabels[i].frame-1,function);
}
}
Where mc is your movieclip and frame_name is your frame name, for more details check my previous post on frame labels:
http://www.dreaminginflash.com/2007/09/26/11/
Do you know if there is a way to append the frame script to the return function in the Delegate class?
something like:
package
{
public class Delegate
{
public static function create(handler:Function,…args):Function
{
return function(…innerArgs):void
{
handler.apply(this,innerArgs.concat(args));
frame1()
}
}
}
}
but frame1() would change depending on what frame you added the function to
Thanks a lot for your help!
Its really good.