User Guide

Using button and movie clip event handlers 171
To create a simple focus manager with event listeners:
1.
Using the Text tool, create a text field on the Stage.
2.
Select the text field and, in the Property inspector, select Input from the Text Type pop-up
menu, and select the Show Border Around Text option.
3.
Create another input text field below the first one.
Make sure the Show Border Around Text option is not selected for this text field. You can
continue to create input text fields.
4.
Select Frame 1 in the Timeline and open the Actions panel (Window > Development
Panels > Actions).
5.
To create an object that listens for focus notification from the Selection class, enter the following
code in the Actions panel:
// Creates listener object, focusListener
var focusListener:Object = new Object();
// Defines function for listener object
focusListener.onSetFocus = function(oldFocus_txt:TextField,
newFocus_txt:TextField) {
oldFocus_txt.border = false;
newFocus_txt.border = true;
}
This code creates a new (generic) ActionScript object named focusListener. This object
defines an
onSetFocus property for itself and assigns a function to the property. The function
takes two parameters: a reference to the text field that does not have focus and one to the text
field that has focus. The function sets the
border property of the text field that does not have
focus to
false, and sets the border property of the text field that has focus to true.
6.
To register the focusListener object to receive events from the Selection object, add the
following code to the Actions panel:
// Registers focusListener with broadcaster
Selection.addListener(focusListener);
7.
Test the application (Control > Test Movie), click in the first text field, and press the Tab key
to switch focus between fields.
Using button and movie clip event handlers
You can attach event handlers directly to a button or movie clip instance on the Stage by using the
onClipEvent() and on() handlers. The onClipEvent() handler broadcasts movie clip events,
and the
on() handler handles button events.
To attach an event handler to a button or movie clip instance, click the button or movie clip
instance on the Stage to bring it in focus, and then enter code in the Actions Panel. The title of
the Actions panel reflects that code will be attached to the button or movie clip: Actions Panel -
Button or Actions Panel - Movie Clip. For guidelines about using code that’s attached to button
or movie clip instances, see Chapter 3, “Organizing ActionScript in a document,” on page 82.