Centralizing Code

Probably the single most important feature of functions is their reusability—we can create reusable segments of code that can be executed from anywhere in a movie. Because functions can be executed remotely, they can be stored in a central location, easing code maintenance and upgrades.

Suppose, for example, that we have three buttons in three different movie clips, all with the same purpose. Instead of triplicating our code, we can put one copy in a function on the main timeline and invoke it from each button as needed. This saves time, reduces the potential for errors, and makes it easy to update the behavior of all three buttons at once. Because the code is centralized, testing is more reliable and troubleshooting much easier. If one button works and another doesn’t, chances are that the problem is in the second button’s function invocation and not in your centralized behavior.

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.