Storing Code in External Files


Flash 8 is easily extensible, as there are various areas in which you can place code. This can be the cause of major problems — because there are many available options, you need to make sure the code is correctly placed so your movie is manageable and easy to use. In Flash 8, you have the option to store your code in an external .as file.

The #include Command

Once you have an .as file to use in a project, it is easy to make the ActionScript within the file available to your movie. The #include command is used to load any code from an external file and put it in the Flash movie file (.swf) published or tested by Flash 8. If you edit the .as file included in your movie, the changes will not automatically become part of an already-published Flash movie file (.swf). The #include command is only executed at the time you publish or test the .swf file.

This simple example shows you how to include an external ActionScript file in a movie.

On the CD-ROM 

You can find externalActions.as in the ch32 folder on this book's CD-ROM. Make a copy of this file to a folder on your hard drive. externalActions.as contains a trace() action that will appear in the Output panel once you have entered Test Mode.

  1. Create a new Flash document by choosing File New.

  2. Save the Flash document to the same location on your hard drive where you copied the externalActions.as file.

  3. Select the first frame of Layer 1 and open the Actions panel (F9). In the Script pane, type the following code:

     #include "externalActions.as" 

    Caution 

    Do not include a semicolon after the #include directive, such as #include "externalActions.as";. Doing so results in a compiler error, preventing the file from being included in the Flash movie.

  4. Test the movie by choosing Control Test Movie. In the Output panel, the words "Traced from externalActions.as" should appear. This is the result of the trace() action in the externalActions.as file.

Now that you know how to use the #include directive, you can start to store larger amounts of code within .as files. One of the primary benefits of using .as files is that you can easily share the same ActionScript code from one Flash document to another.

Cross-Reference 

Read Chapter 30, "Applying HTML and Text Field Formatting," to see more Flash documents that use the #include directive.

Caution 

Don't confuse the #include directive with the import keyword in ActionScript 2.0 code. You can think of the #include directive as a copy and paste of the code from an ActionScript file (.as) into the current script shown in the Actions panel. The import keyword, however, simply loads an ActionScript 2.0 class into the ActionScript compiler so that it can check for errors within your code. For example, if you use the #include directive in several places throughout your Flash document's actions, then the referenced document is inserted into those places when you publish or test the .swf file. With an imported class file, however, the code is only stored once within the Flash movie.

Import Script Command

If you prefer not to use the #include command to incorporate the code in your Flash movie file (.swf) at run time, you can simply import the file directly into Flash 8 using the Actions panel.

On the CD-ROM 

Continue to use the externalActions.as file for this exercise.

  1. Open Flash 8 and create a new Flash document.

  2. Save this file to the same location as the externalActions.as file.

  3. Select the first frame of Layer 1 and open the Actions panel (F9).

  4. Select Import Script from the options menu of the Actions panel.

  5. Browse to the folder where you saved externalActions.as, highlight it, and click OK. Flash 8 will import the file and put it in your movie.

Caution 

When you import ActionScript from a file using this method, the contents of the Script pane will be overwritten and replaced with the contents from the ActionScript file.

Tip 

Flash 8 does not parse the file and check for incorrect syntax when importing. You should check the syntax of the code once it has imported to make sure it is correct. To do this, choose Check Syntax from the options menu of the Actions panel.

Export Script Command

Just as you can import the contents of an external file, you can export the contents of the Script pane to an .as file. This will enable you to take advantage of the editing capabilities in the Actions panel while still leveraging the usefulness of external ActionScript files.

  1. Open a Flash document that contains some ActionScript code. You can use the debugging_code.fla document from the exercise earlier in this chapter.

  2. Select a frame containing ActionScript code, and open the Actions panel.

  3. Select Export Script from the options menu of the Actions panel.

  4. Save the new .as file to a preferred location on your system.

Team Environments

When working with a team of developers on one project, it can get quite difficult to manage the development of an application. Many "newbie" applications are built within one .fla file, which poses a problem because only one person can work on the file at any given time. It can become very confusing and file versioning errors may occur.

A great way to reduce dependency on other team members is to break the .fla file up as much as possible. This may include breaking a project into several Flash document files (.fla), which are then combined using the loadMovie() method command at run time. Another effective method is to use external code files for storing data. For each block of code performing a separate task, or each custom object you are using in your application, you can store the ActionScript in an external file. This makes it easy for many people to work simultaneously on one application.

Cross-Reference 

For more information on Flash project management, see Chapter 3, "Planning Flash Projects." This chapter discusses the Project panel featured in Flash Professional 8.

ActionScript Libraries and Classes

With Flash 8, you can build ActionScript routines (and even your own custom classes) suitable for re-use across a variety of situations. For instance, you may have a block of code that you use to initiate a connection to a server-side script, or a Flash Communication Server application. Storing such code in external code files is an easy way to include them in your next project without much effort.

An ActionScript library is a set of code used for a specific purpose. It can be any block of ActionScript, from simple variable declarations to more advanced ActionScript objects. This sort of code is well suited for being stored in an external text file, and easily included in many projects.

Cross-Reference 

It's beyond the scope of this chapter to discuss the use of custom class files. Refer to Chapter 31, "Creating a Game in Flash," for an example of a Flash document that uses ActionScript 2.0 classes.




Macromedia Flash 8 Bible
Macromedia Flash8 Bible
ISBN: 0471746762
EAN: 2147483647
Year: 2006
Pages: 395

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