User Guide
Creating movie clips with button states 173
Using on() and onClipEvent() with event handler methods
You can, in some cases, use different techniques to handle events without conflict. Using
on()
and
onClipEvent() 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
on() and onClipEvent(), 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 174.
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 173.
Broadcasting events from component instances
For any component instance, you can specify how an event is handled. Component events are
handled differently than events broadcast from native ActionScript objects.
For more information, see “Handling Component Events” in Using Components.
Creating movie clips with button states
When you attach an on() handler to a movie clip, or assign a function to one of the MovieClip
mouse event handlers for a movie clip instance, the movie clip responds to mouse events in the
same way as a button. You can also create automatic button states (Up, Over, and Down) in a
movie clip by adding the frame labels
_up, _over, and _down to the movie clip’s Timeline.
When the user moves the mouse over the movie clip or clicks it, the playhead is sent to the frame
with the appropriate frame label. To designate the hit area used by a movie clip, you use the
MovieClip.hitArea property.
To create button states in a movie clip:
1.
Select a frame in a movie clip’s Timeline to use as a button state (Up, Over, or Down).
2.
Enter a frame label in the Property inspector (_up, _over, or _down).
3.
To add additional button states, repeat steps 1–2.
4.
To make the movie clip respond to mouse events, do one of the following:
■ Attach an on() event handler to the movie clip instance, as discussed in “Using button and
movie clip event handlers” on page 171).
■ Assign a function to one of the movie clip object’s mouse event handlers (onPress,
onRelease, and so forth), as discussed in “Using event handler methods” on page 167.