16.4 Adding Scripts to Movie Clips

ActionScript for Flash MX: The Definitive Guide, 2nd Edition
By Colin Moock
Chapter 16.  ActionScript Authoring Environment

We've already seen that we can attach code to the frames of a movie clip's timeline. It is also possible to attach code to a movie clip instance itself. To do so, we select the instance on stage and then add our code to the Script pane of the Actions panel. As with buttons, all code attached to movie clip objects must be contained within event handlers. Event handlers tell the interpreter when to execute the movie clip code. For example, the following code sets the variable x to 10 when a movie clip loads:

onClipEvent (load) {   var x = 10; }

Movie clip event handlers can react to mouse and keyboard activity, data loading, frames rendering, and the birth and death of movie clips. For complete coverage of movie clips and event handlers, see MovieClip in the Language Reference.

With the introduction of the callback-function event model in Flash MX, attaching code directly to movie clip instances is rarely necessary. Event handlers should be applied to movie clip instances either from a central code repository on the main timeline or, if the handler applies to all instances of the symbol, from within the movie clip symbol itself. Event handlers can also be specified for an entire movie clip symbol with a MovieClip subclass (see Chapter 14). For very specific exceptions, consult the MovieClip event handler entries in the ActionScript Language Reference.

     



    ActionScript for Flash MX. The Definitive Guide
    ActionScript for Flash MX: The Definitive Guide, Second Edition
    ISBN: 059600396X
    EAN: 2147483647
    Year: 2002
    Pages: 780
    Authors: Colin Moock

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net