Copying Clip Event Handlers
A quick point that has
major ramifications: movie clip event handlers are duplicated when a
movie clip is duplicated via the duplicateMovieClip(
)
function. Suppose, for example, we
have a movie clip on stage called square, which
has a load event handler defined:
onClipEvent (load) {
trace("movie loaded");
}What happens when we duplicate square to create
square2?
square.duplicateMovieClip("square2", 0);Because the load handler is copied to
square2 when we duplicate
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 handlers, we can create slick
recursive functions with very powerful
results. For a demonstration that only scratches the surface of
what’s possible, refer to Example 10.2.
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access