AS3 Quickie – Frame Label Events

With Flash Player 11.3 and above, the ability to listen for a frame label event is now included in the runtimes. For example; if you have a MovieClip symbol with a certain set of animation within it, and you have a number of frame labels defined upon the symbol’s timeline… you can now listen for these particular frames through their individual label events and then respond to them in some way.

[kml_flashembed publishmethod=”static” fversion=”11.3.0″ movie=”/wp-content/uploads/2012/11/FrameLabelEvents.swf” width=”550″ height=”400″ targetclass=”flashmovie” play=”true” loop=”true” menu=”false” quality=”high” wmode=”direct” allowfullscreen=”true” allowscriptaccess=”always” allownetworking=”all”]
Requires Flash Player 11.3 or above!

To get this working only takes a few steps in your ActionScript code. For each frame label you wish to respond to, you must create a new flash.display.FrameLabel object and add an event listener to it. In order to utilize Event.FRAME_LABEL you must target Flash Player 11.3 or above.

You’ll (of course) need to define some frame labels across the timeline. Here, we’ve established 4 frame labels within a MovieClip symbol:

In ActionScript, we must perform the necessary imports:

import flash.events.Event;
import flash.display.FrameLabel;

We then need to assign an event listener to each frame label we wish to respond to. In order to accomplish this, we read all of the symbol frame labels into an array in order to dynamically create flash.display.FrameLabel objects which have event listeners attached to them, listening specifically for Event.FRAME_LABEL:

labelsArray = angyFace.currentLabels;
for(var i:uint=0; i<labelsArray.length; i++){
	var frameLabel:FrameLabel = labelsArray[i];
	frameLabel.addEventListener(Event.FRAME_LABEL, onFrameLabel);
}

And follow through with a method to handle the event, itself:

private function onFrameLabel(e:Event):void {
	//do something
}

Of course, depending upon our requirements, we may want to register each frame label object specifically in order to manage it appropriately. For simplicity – this example project has no cleanup routine associated with it.

The full code is below. Note that the FLA contains upon the Stage a TextField with the instance ID of statusField and a MovieClip symbol instace called angryFace which contains the 4 frame labels we are registering:

package  {
	import flash.display.MovieClip;
	import flash.display.FrameLabel;
	import flash.text.TextField;
	import flash.events.Event;
	
	public class Main extends MovieClip {
		public var statusField:TextField;
		public var angyFace:MovieClip;
		
		private var labelsArray:Array;
		
		public function Main() {
			this.addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(e:Event):void {
			labelsArray = angyFace.currentLabels;
			for(var i:uint=0; i<labelsArray.length; i++){
				var frameLabel:FrameLabel = labelsArray[i];
				frameLabel.addEventListener(Event.FRAME_LABEL, onFrameLabel);
			}
		}
		
		private function onFrameLabel(e:Event):void {
			statusField.appendText("\n" + e.target.frame + ": " + e.target.name);
		}
		
	}
	
}

Want the full package? Download below:
Packaged code and Flash Professional CS6 project

2 thoughts on “AS3 Quickie – Frame Label Events”

  1. Pingback: AS3 Quickie – Frame Label Events | In Flagrante Delicto! « eaflash

  2. For a player target lower than Flash Player 11.3 you could use the TimeLineController of the AS3 Temple Library:
    http://templelibrary.googlecode.com/svn/trunk/doc/temple/ui/animation/TimelineController.html


    var controller:TimelineController = new TimelineController(this, true);
    controller.addEventListener(TimelineControllerEvent.REACH_FRAME, onFrameLabel);

    private function onFrameLabel(e:TimelineControllerEvent):void
    {
    statusField.appendText("\n" + e.frame + ": " + e.label);
    }

Leave a Comment

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