Dynamic Movie Clip Event Handlers
Early in this
chapter,
we learned about two kinds of events in
Flash—those that are attached to movie clips and buttons and
those that are attached to other data objects such as
XML
and XMLSocket
. To
create event handlers for data objects, we assign the handler
function name as a property of the object. Recall the syntax to add a
function dynamically:
myXMLDoc.onLoad = function ( ) { trace("all done loading!"); };
Dynamic function assignment lets us change the behavior of the handler during movie playback. All we have to do is reassign the handler property:
myXMLDoc.onLoad = function ( ) { gotoAndPlay("displayData"); };
Or we can even disable the handler altogether:
myXMLDoc.onLoad = function ( ) { return; };
Unfortunately,
handlers of movie clip and button
events are not nearly so flexible; they cannot be changed or removed
during movie playback. Furthermore, movie clip event handlers cannot
be attached to the main movie timeline of any movie! It’s
impossible to directly create an event handler for a movie’s
_root
clip.
In order to work around these limitations, we can—in the case
of the enterFrame
and the user-input events—use
empty movie clips to simulate dynamic event-handler removal and
alteration. Empty movie clips even let us simulate
_root
-level events. We’ve already seen the
technique in Chapter 8, where we learned how to
create an event loop as follows:
Create an empty movie clip named
process
.Place another empty clip called
eventClip ...
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.