338 Handling Events
You can attach onClipEvent() and on() only to movie clip instances that have been placed
on the Stage during authoring. You cannot attach
onClipEvent() or on() to movie clip
instances that are created at runtime (using the
attachMovie() method, for example). To
attach event handlers to objects created at runtime, use event handler methods or event
listeners. (See “Using event handler methods” on page 330 and “Using event listeners”
on page 332.)
For more information on button and movie clip event handlers, see the following topics:
■ “Using on and onClipEvent with event handler methods” on page 338
■ “Specifying events for on or onClipEvent methods” on page 340
■ “Attaching or assigning multiple handlers to one object” on page 341
Using on and onClipEvent with event handler
methods
You can, in some cases, use different techniques to handle events without conflict. Using the
on() and onClipEvent() methods doesn’t conflict with using event handler methods that
you define.
For example, suppose you have a button in a SWF file; the button can have an
on(press)
handler that tells the SWF file to play, and the same button can have an
onPress() method,
for which you define a function that tells an object on the Stage to rotate. When you click the
button, the SWF file plays and the object rotates. Depending on when and what kinds of
events you want to invoke, you can use the
on() and onClipEvent() methods, event
handler methods, or both techniques of event handling.
However, the scope of variables and objects in
on() and onClipEvent() handlers is different
than in event handler and event listeners. See “Event handler scope” on page 343.
You can also use
on() with movie clips to create movie clips that receive button events. For
more information, see “Creating movie clips with button states” on page 342. For
information on specifying events for
on() and onClipEvent(), see “Specifying events for on
or onClipEvent methods” on page 340.
NOTE
Attaching onClipEvent() and on() handlers is not a recommended practice. Instead, you
should put your code in frame scripts or in a class file, as demonstrated throughout this
manual. For more information, see “Using event handler methods” on page 330 and
“Attaching code to objects” on page 746.