“Be inspired, enjoy your work, keep learning and never forget to play.” — gskinner

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:


5 Comments on “addFrameScript with parameters”

  1. 1 Paulo said at 12:39 pm on March 28th, 2008:

    Ah, very nice. Didn’t even think about using a Delegate since its AS3 but its so obvious now. Feels a little weird though :)

  2. 2 compelo said at 7:07 am on April 16th, 2008:

    how about doing a something like
    addFrameScript(“frame_name”,function)

    can that be done?

  3. 3 Ivan Valadares said at 10:30 am on April 16th, 2008:

    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/

  4. 4 Luke said at 11:39 pm on February 2nd, 2009:

    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

  5. 5 prabhu said at 2:25 pm on May 11th, 2009:

    Thanks a lot for your help!
    Its really good.


Leave a Reply