Dynamically browsing MovieClip frame labels

Frame labels are great for keeping track of graphical states on a MovieClip timeline, but let’s face it: their usefulness was limited in AS2 due to the fact that you had to know that they existed before you could reference them. Any AS2 developer can probably think of a time in their past when they built a large state machine based on hard-coded frame labels that MovieClips would need to implement. Inevitably it didn’t matter how solid your code was in those cases; your app could–and would–still break unless you remembered to include ever silly “main”, “secondary”, and “my-cool-screen” frame label defined within code.

But there’s good news to be had here: all that has been addressed in AS3. In fact, the frame label model is unbelievably cool now! The first and most basic new feature is the MovieClip.currentLabel property. That gives us the frame label of the current timeline frame. While that may seem pretty straight forward, it’s a huge step up from AS2 where the current frame label was not available at run-time (…which always seemed like a pretty big oversight to me).

However, I think the best new feature is the MovieClip.currentLabels property, which gives an array of ALL frame labels on a timeline, along with their corresponding frame numbers. AWESOME. That means that we can quickly and easily set up a scenario that will let us browse all of a MovieClip’s labeled timeline frames at run-time. Here’s an example:

import flash.display.FrameLabel;

// _displayClip = MovieClip with timeline to access.
// _select = Flash UI ComboBox component instance.

_displayClip.stop();
_select.removeAll();
_select.addEventListener(Event.CHANGE, this._onSelectLabel);

// get and populate all of displayClip's frame labels in the select menu
for each (var j:FrameLabel in _displayClip.currentLabels)
{
    // add list items with FrameLabel object properties: name, frame.
    _select.addItem({label:j.name, data:j.frame});
}

function _onSelectLabel(evt:Event):void
{
    // update displayClip when a new frame is selected
    _displayClip.gotoAndStop(_select.selectedItem.data);
}

If you set that scenario up in a new Flash document and give the _displayClip a few frame labels to browse through, then you’ll be able to select and display the various frames in your MovieClip using the combobox (…just be sure to put different graphics on each labeled timeline frame so that you can see a change in appearance when you switch between frames!).

So, what can you do with this? I’ve used it in the new Lassie Shepherd editor so that a developer can select frame labels to display within externally loaded SWF media. It works like a charm. This would also be a good means to build a really simple navigation scheme: you’d set up and label different content frames along a timeline, load that movie into a shell, and then you could populate navigation options within the shell based on the frame labels. The beauty here is that if you ever added new content frames to the loaded media, they’d plug right into the shell movie that rendered the navigation.

Advertisements

No comments yet

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: