Earlier in this chapter, we saw that Flash 5-style on( ) and onClipEvent( ) handlers cannot be changed or removed during movie playback. Furthermore, onClipEvent( ) event handlers cannot be attached to the main movie timeline (the _root) of any movie.
In order to work around these limitations, we can—in the case of onClipEvent(enterFrame) and the movie clip mouse and key events—use empty movie clips to simulate dynamic event-handler removal and alteration. Empty movie clips even let us simulate onClipEvent( ) handlers for the main timeline. Follow these steps to see how it works (note that the following technique applies to Flash 5 only, because event handler properties in Flash MX can be removed directly via delete):
Create an empty movie clip named process.
Place another empty clip, named eventClip, inside process.
On eventClip, attach the desired event handler. The code in the eventClip's handler should target the process clip's host timeline, like this:
onClipEvent (mouseMove) { this._parent._parent.doSomeFunction( ); }
To export process for use with the attachMovie( ) function, select it in the Library and choose Options Linkage. Set Linkage to Export This Symbol, and assign an appropriate identifier (e.g., mouseMoveProcess).
Finally, to engage the event handler, attach the process clip to the appropriate timeline using attachMovie( ).
To disengage the handler, remove the process clip using removeMovieClip( ).
To see this technique in action, download "Event Loop, Controllable" from the online Code Depot.