Table of Contents

16.7 Externalizing ActionScript Code

ActionScript code can be saved in external text files (which use the .as extension, by convention) or .swf files and imported into a Flash document. By maintaining code in external files, we facilitate the use of standard code libraries across many projects. We can import external code into Flash using Import From File, the #include directive, or a shared library.

16.7.1 Import From File (Author-Time Import)

While editing a .fla file, we can bring code into the Action panel's Script pane using Import From File, found under the pop-up Options menu button in the top-right corner of the Actions panel (see Figure 16-1). Import From File is a one-time operation that copies the contents of the external file into the Actions panel, replacing any script currently there. Code imported via Import From File is not persistently linked to the .fla file in any way. To append or insert script text instead of replacing it, you must manually copy and paste the text from an external text-editing application.

16.7.2 #include (Compile-Time Import)

When we export ( compile) a .swf file from a .fla file, we can incorporate code from an external text file using the #include directive. For information on using #include, see the Language Reference.

16.7.3 Shared Library (Runtime Import)

To import code from an external source while a movie is actually playing, we must create a shared library .swf file with a movie clip containing the code to import. Runtime import offers the most flexible approach to sharing code across movies, because it does not require recompilation of the movies that import the shared code; when the shared library .swf file is updated, the movies that link to it reflect the update automatically.

The following procedures describe how to share a simple test function from a movie called codeLibrary.swf to a movie called myMovie.swf. We'll create the codeLibrary.swf movie first:

  1. Create a new Flash document.

  2. Create a new movie clip symbol named sharedFunctions.

  3. On frame 1 of the sharedFunctions clip, add the following code:

    function test () {
      trace("The shared function, test, was called.");
    }
  4. Select the sharedFunctions clip in the Library.

  5. From the Library panel, select Linkage from the pop-up Options menu.

  6. Select Export for Runtime Sharing.

  7. In the Identifier box, type sharedFunctions.

  8. In the URL box, type codeLibrary.swf.

  9. Click OK.

  10. Save the document as codeLibrary.fla.

  11. Use File figs/U2192.gif Export Movie to create codeLibrary.swf from codeLibrary.fla.

  12. Close the codeLibrary.swf and codeLibrary.fla files.

Now, we'll create the myMovie.swf file, which will execute the code imported from codeLibrary.swf:

  1. Create a new Flash document.

  2. Save the new document as myMovie.fla in the same folder as codeLibrary.fla.

  3. Rename Layer 1 to sharedCode.

  4. Select File figs/U2192.gif Open As Library, and choose codeLibrary.fla. The codeLibrary.fla Library appears.

  5. From the codeLibrary.fla Library, drag an instance of the sharedFunctions clip onto the Stage at frame 1 of myMovie.fla.

  6. Name the instance sharedFunctions.

  7. On the main timeline of myMovie.fla, create a new layer called scripts.

  8. Add a keyframe to the scripts layer at frame 2.

  9. Add a new frame to the sharedCode layer.

  10. On frame 2 of the scripts layer, attach the following code:

    stop();
    sharedFunctions.test();
  11. Export myMovie.swf.

  12. The following text appears in the Output window: "The shared function, test, was called."

Note that a shared library is a linked asset, much like a .gif image is linked to a .html file. Therefore, shared library .swf files must always be uploaded correctly with the files that use them. To change the location of a link from an imported symbol to a shared library, follow these steps:

  1. Select the symbol in the Library.

  2. From the Library panel's pop-up Options menu, choose Linkage.

  3. Under URL, set the new location.


Table of Contents