EasyManua.ls Logo

MACROMEDIA FLASH 8-LEARNING ACTIONSCRIPT 2.0 IN FLASH - Using Event Listeners

MACROMEDIA FLASH 8-LEARNING ACTIONSCRIPT 2.0 IN FLASH
830 pages
Print Icon
To Next Page IconTo Next Page
To Next Page IconTo Next Page
To Previous Page IconTo Previous Page
To Previous Page IconTo Previous Page
Loading...
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 clips 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 classs
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.

Table of Contents

Related product manuals