
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
240
|
Chapter 10: Events and Event Handling
Suppose, for example, we have a movie clip on stage named square, which has an
onClipEvent(load) handler defined:
onClipEvent (load) {
trace("movie loaded");
}
What happens when we duplicate square to create square2?
square.duplicateMovieClip("square2", 0);
Answer: Because the onClipEvent(load) handler is copied to square2 when we dupli-
cate square, the birth of square2 causes its load handler to execute, which displays
“movie loaded” in the Output window. By using this automatic retention of han-
dlers, we can create slick recursive functions with very powerful results. For a dem-
onstration, refer to MovieClip.onLoad( ) in the Language Reference.
In contrast, suppose we have a movie clip named
circle and we assign it an
onEnterFrame( ) event handler property as follows:
circle.onEnterFrame = function ( ) {
trace(this._name);
}
What happens when we duplicate circle to create circle2?
circle.duplicateMovieClip("circle2", 1);
Answer: Only circle’s name appears in the Output window. The onEnterFrame( )
event handler property is not copied to
circle2,socircle2’s name does not appear
in the Output window. To force every duplicate of the
circle clip to define an
onEnterFrame( ) event handler property, we can set the property on a frame of
circle’s timeline: ...