Sending a
movie clip to a new frame is a simple affair. Calling either
gotoAndPlay()
or
gotoAndStop()
will
jump the movie clip to the frame number specified as a parameter.
Alternatively, you can pass a string that matches the name of a
frame label. Any frame on the timeline can be assigned a label.
To do this, select a frame on the timeline and then enter a name
in the Frame Label field on the Property inspector.
The advantages of using frame labels instead of numbers are particularly
evident when creating a complex movie clip. When the number of frames,
layers, and tweens in an animation becomes large, consider labeling
important frames with explanatory descriptions that represent shifts
in the behavior of the movie clip (for example, “off,” “walking,”
or “running”). This improves code readability and also provides
flexibility, since ActionScript calls that go to a labeled frame
are pointers to a single reference—the label—rather than a specific
frame number. If later on you decide to move a particular segment
of the animation to a different frame, you will not need to change
your ActionScript code as long as you keep the same label for the
frames in the new location.
To represent frame labels in code, ActionScript 3.0 includes
the FrameLabel class. Each instance of this class represents a single
frame label, and has a
name
property representing
the name of the frame label as specified in the Property inspector,
and a
frame
property representing the frame number
of the frame where the label is placed on the timeline.
In order to get access to the FrameLabel instances associated
with a movie clip instance, the MovieClip class includes two properties
that directly return FrameLabel objects. The
currentLabels
property
returns an array that consists of all FrameLabel objects across
the entire timeline of a movie clip. The
currentLabel
property
returns a string containing the name of the frame label encountered
most recently along the timeline.
Suppose you were creating a movie clip named
robot
and
had labeled the various states of its animation. You could set up
a condition that checks the
currentLabel
property
to access the current state of
robot
, as in the following
code:
if (robot.currentLabel == "walking")
{
// do something
}
Flash Player 11.3 and AIR 3.3 added the
frameLabel
event
to the FrameLabel class. You can assign an event handler to the
FrameLabel instance that represents a frame label. The event is
dispatched when the playhead enters the frame.
The following example creates a FrameLabel instance for the second
frame label in the Array of frame labels for the MovieClip. It then
registers an event handler for the
frameLabel
event:
var myFrameLabel:FrameLabel = robot.currentLabels[1];
myFrameLabel.addEventListener(Event.FRAME_LABEL, onFrameLabel);
function onFrameLabel(e:Event):void {
//do something
}