1.13. Reusing and Organizing Code in Multiple Movies
Problem
You want to reuse code that you’ve created for one project in another Flash movie. Or you want to write your ActionScript code in an external text editor.
Solution
Place your ActionScript code in external .as files and use the
#include directive to add them to your Flash
movies:
// Adds all the code within MyActionScriptFile.as to your Flash movie.
#include "MyActionScriptFile.as"Discussion
Use the #include directive to
incorporate code from external text files
into your Flash movie during compilation from a .fla file to a .swf file. When you export a .swf file, Flash replaces the
#include directive with the contents of the
specified file. The external file must be a text file with valid
ActionScript code in it. By convention the file should be named with
the .as extension, though it is
not absolutely necessary:
#include "ASutils.as"
Notice that the #include directive is not followed
by a semicolon. Adding a semicolon causes an error.
Additionally, Flash must be able to locate the file when you export the movie. Therefore, you should place the file in a location relative to where the Flash document is saved. For example, the previous example looks for a file named ASutils.as in the same directory as the .fla document. You can also place the file in a subdirectory of the directory in which the Flash document is saved:
// Look for a file namedASutils.asin a subdirectory namedmyASFiles. #include "myASFiles/ASutils.as"
You can also ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access