332
Handling Events
Event handlers for runtime objects
You can also assign functions to event handlers for
objects you create at runtime. For example, the following code creates a new movie clip
instance (
newclip_mc
) and then assigns a function to the clip’s
onPress
event handler:
this.attachMovie("symbolID", "newclip_mc", 10);
newclip_mc.onPress = function () {
trace("You pressed me");
}
For more information, see
“Creating movie clips at runtime” on page 360
.
Overriding event handler methods
By creating a class that extends an ActionScript class,
you can override event handler methods with the functions that you write. You can define an
event handler in a new subclass that you can then reuse for various objects by linking any
symbol in the library of the extended class to the new subclass. The following code overrides
the MovieClip class’s
onPress
event handler with a function that decreases the transparency
of the movie clip:
// FadeAlpha class -- sets transparency when you click the movie clip.
class FadeAlpha extends MovieClip {
function onPress() {
this._alpha -= 10;
}
}
For specific instructions on extending an ActionScript class and linking to a symbol in the
library, see the examples in
“Assigning a class to symbols in Flash” on page 279
. For
information on writing and working with custom classes, see
Chapter 7, “Classes.”
Using event listeners
Event listeners let an object, called a
listener object
, receive events broadcast by another object,
called a
broadcaster object
. The broadcaster object registers the listener object to receive events
generated by the broadcaster. For example, you can register a movie clip object to receive
onResize
notifications from the Stage, or a button instance could receive
onChanged
notifications from a text field object. You can register multiple listener objects to receive
events from a single broadcaster, and you can register a single listener object to receive events
from multiple broadcasters.
Содержание FLASH 8-LEARNING ACTIONSCRIPT 2.0 IN FLASH
Страница 1: ...Learning ActionScript 2 0 in Flash...
Страница 8: ...8 Contents...
Страница 18: ...18 Introduction...
Страница 30: ...30 What s New in Flash 8 ActionScript...
Страница 66: ...66 Writing and Editing ActionScript 2 0...
Страница 328: ...328 Interfaces...
Страница 350: ...350 Handling Events...
Страница 590: ...590 Creating Interaction with ActionScript...
Страница 710: ...710 Understanding Security...
Страница 730: ...730 Debugging Applications...
Страница 780: ...780 Deprecated Flash 4 operators...
Страница 830: ...830 Index...