is supported in part by

July 27, 2007

Chapter 31, Pages 1-3, Essential ActionScript 3.0

Here are the first 3 pages of Chapter 31 of Essential ActionScript 3.0. This is the final chapter preview.

31. Distributing a Class Library

This chapter discusses three specific techniques for sharing a group of classes (a class library) among multiple projects and multiple developers. By far the easiest way to share classes is to simply distribute the source code. We’ll cover this easiest case first, before we learn how to share classes without distributing source code, as you might want to do when selling a professional class library.
The term “class library” is programmer jargon for an arbitrary group of classes distributed to a team or to the world at large. Don’t confuse it with a .fla file’s Library or the Flash Library panel. Those terms are unique to the Flash authoring environment and not part of the current discussion.

In ActionScript, a class library can be distributed to other developers simply as a bunch of source .as files, in a .swf file, or in a .swc file. We’ll cover all three approaches in this chapter. Note, however, that ActionScript offers a wide range of options for distributing class libraries; this chapter covers three specific canonical situations, but is not exhaustive. For more information on distributing class libraries, see the following Adobe documentation:

* Programming ActionScript 3.0 > Flash Player APIs > Client System Environment > Using the ApplicationDomain class (
* Building and Deploying Flex 2 Applications > Building Flex Applications®Using Runtime Shared Libraries (

The example files discussed in this chapter are available at

Sharing Class Source Files

Let’s start with the simplest way to distribute a class library: sharing class source files.

Suppose you work in a small web shop called Beaver Code, whose web site is You’ve made a class—com.beavercode.effects.TextAnimation—that creates various text effects. You want to use the TextAnimation class on two sites you’re working on, Barky’s Pet Supplies and Mega Bridal Depot. Rather than place a copy of the class file (that is, in each project folder, you store the class file centrally and merely refer to it from each project. For example, on Windows, you store in the following location:


To make the TextAnimation class accessible to both projects, you add the directory c:\data\actionscript to each project’s classpath (the classpath is discussed in Chapter 7).

By the same logic, if there were several members on your team, you might think it would be handy to store your classes on a central server so everyone would be able to use them by adding the server folder to their project’s classpath. For example, you might want to store all shared classes on a server called codecentral, as follows:


But working directly off the server is highly perilous and not recommended.
If you store your classes on a central server and allow developers to modify them directly, the developers are liable to overwrite one another’s changes. Furthermore, if the clock of the server and the clock of a programmer’s personal computer are not in perfect sync, then the latest version of the class might not be included in the program at compile time. To avoid these problems, you should always use version control software to manage your class files when working on a team. Two popular (and free!) options are CVS (see and Subversion (

On large projects, you might also want to automate the .swf export process using a build tool such as Apache Ant (
For information on using Ant with Flex Builder 2, see Using Flex Builder 2 > Programming Flex Applications > Building Projects > Advanced build options > Customizing builds with Apache Ant (

To automate .swf export in the Flash authoring tool, you’d have to execute a command-line JSFL script to tell Flash to create the .swf for each .fla file in your project. Complete coverage of command-line compilation with Flash is outside the scope of this book, but here’s a quick sample that gives the general flavor of it on Windows:

// Code in exportPetSupplies.jsfl:
// ===============================
// Open the .fla file.
var doc = fl.openDocument("file:///c|/data/projects/pet/petsupplies.fla");
// Export the .swf file.
doc.exportSWF("file:///c|/data/projects/pet/petsupplies.swf", true);
// Quit the Flash authoring tool (optional).

// Command issued on command line from /pet/ directory:
// ====================================================
"[Flash install_folder]\flash.exe" exportPetSupplies.jsfl

For the preceding example command to work, Flash must not be running. After the command is issued, the compiled petsupplies.swf movie appears in the directory c:\data\projects\pet.

Posted by moock at July 27, 2007 10:39 PM