10.14 Code Reusability

ActionScript for Flash MX: The Definitive Guide, 2nd Edition
By Colin Moock
Chapter 10.  Events and Event Handling

When using event handlers and listeners, don't forget the code-centralization principles discussed in Chapter 9. Avoid unnecessary duplication and intermingling of code across movie elements. If you find yourself entering the same code in more than one event handler's body, you should try defining it in a single function and pointing each event handler to that function. Try generalizing your code, pulling it off the object and placing it in a code repository somewhere in your movie; global code used throughout a movie should be attached to the _global object.

In almost all cases, it's a poor idea to hide statements inside an on( ) or onClipEvent( ) handler. Remember that encapsulating your code in a function makes your code reusable and easy to find. This is particularly true of buttons I rarely place any code on a button in Flash MX, and I rarely use more than a single function-invocation statement in a Flash 5 on( ) handler. For movie clips, you'll need to employ keener judgment, as placing code directly on clips can often be a healthy part of a clean, self-contained code architecture. Experiment with different approaches until you find the right balance for your needs and skill level. Regardless, it always pays to be mindful of redundancy and reusability issues.

     



    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