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.

Get ActionScript: The Definitive Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.