addFrameScript and currentLabels

3 Comments 9 February 2011

Today I’ve been setting up a very simple project – an intro animation and then some text/links fade in. The designers are making the animation and I’m just writing some basic code for the links etc (it’s CMS driven, many languages).

The client often like to change their minds (just a regular client then) and I know that the animation may change quite a bit. To fade the content in after the animation I am using addFrameScript.

However, a problem, I know the frame that the fading should take place on could change several (hundred) times before the project goes live. I could just tell the designers where in the code to change the frame number, but I thought of a nicer solution than that.

The code was a simple combo of addFrameScript and currentLabels. I got the designer to add a label on the frame they want the fading to take place and then used this simple code:

function addLabelScript(mc:MovieClip, label:String, func:Function):void
{
  var labels:Array = mc.currentLabels;
  var i:int = labels.length;
 
  while(--i > -1)
  {
    if(FrameLabel(labels[i]).name == label)
    {
      mc.addFrameScript(FrameLabel(labels[i]).frame - 1, func);
      return;
    }
  }
 
  trace("WARNING: The label '" + label + "' does not exist in the MovieClip '" + mc.name + "'");
}

OK it’s not going to make you fall off your chair with wonder but could be useful in the future. I’ve chucked it in a utils Class so it can be used like this:

function init():void
{
  Utils.addLabelScript(animation, "animation-complete", onComplete);
}

function onComplete():void
{
  trace("hello monkey I'm finished, do your fading thing");
}

In the above example the frame label the designer adds is “animation-complete”.


Posted in ActionScript, Tip

3 Responses to addFrameScript and currentLabels

  1. 5 July 2012

    Hey, nice of you to share the snippet :]

    I just wanted to point out that the function you’ve got there won’t actually map to the correct frame, due to the indexing disparity between conventional frame functions (gotoAndPlay(), etc.) and the newer addFrameScript.

    For most purposes, the frames are numbered from 1 to [total-frames], but addFrameScript has a zero-based index going from 0 to [total-frames - 1].

    It makes sense that you might not have noticed this, since it’s only the difference of one frame and in many cases it wouldn’t matter, but say for example you were to try to add a function to the final frame in a MovieClip, your example would fail. So, a correction of the line above would be:

    mc.addFrameScript(FrameLabel(labels[i]).frame – 1, func);

    Cheers!

  2. 5 July 2012
    FlashMonkey

    Hey Adam! I didn’t know that addFrameScript has a zero based index – thanks for the tip. I’ve updated the code. Much appreciated.

  3. 5 July 2012

    Hey, no problem! It’s certainly counter-intuitive; silly Adobe. Thanks for the sharing the code.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>