Table of Contents

10.15 Dynamic Movie Clip Event Handlers

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):

  1. Create an empty movie clip named process.

  2. Place another empty clip, named eventClip, inside process.

  3. 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( );
    }
  4. To export process for use with the attachMovie( ) function, select it in the Library and choose Options figs/U2192.gif Linkage. Set Linkage to Export This Symbol, and assign an appropriate identifier (e.g., mouseMoveProcess).

  5. Finally, to engage the event handler, attach the process clip to the appropriate timeline using attachMovie( ).

  6. 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.


Table of Contents