14.2 Sharing Classes Without Sharing Source Files

 <  Day Day Up  >  

Earlier we learned that it's easiest to share a class library simply by distributing class files (that is, .as files). However, you may not want to share a class library in that way for two reasons:

  • You don't want to distribute your source files because they're proprietary.

  • You want your code to compile faster. (That is, your core classes don't change much, so you don't want them recompiled every time you export a .swf file ”you're happy using precompiled versions of the classes.)

Both of these concerns are valid. However, given the work involved in distributing a class library without distributing its source files, you should consider the following factors before you take the no-source plunge:

  • No code in a .swf file is safe. Several Flash decompilers that can strip source code from a .swf file are available, but not obfuscators that can absolutely , 100% prevent source code theft. At best, efforts to protect source code in a .swf are a deterrent only, merely costing the would-be thief extra work.

  • The time saved by using precompiled libraries may be consumed by the extra time required to maintain a no-source code library. Your mileage may vary. (Maybe you should upgrade your computer instead and make all compiling faster?)

The remainder of this chapter explains how to distribute a class library without distributing its source files. Hold on to your hat. The ride's about to get a little bumpy.

14.2.1 Runtime Versus Compile Time

Once you've determined that you must distribute a no-source class library, you need to decide whether the library should be incorporated at compile time or loaded at runtime. A class library loaded at compile time is known as a static class library (contrast this with a dynamic class library , which, as we learned earlier, is a class library loaded at runtime). In Flash MX 2004, a static class library takes the form of a component .swc file, which is added to a .fla file at authoring time. As we saw earlier, a dynamic class library takes the form of a .swf file, such as spidercore_runtime.swf . Dynamic class libraries are loaded into the Flash Player via loadMovie( ) . The pros and cons in Table 14-1 should help you decide which kind of library you should use in your application.

Table 14-1. Comparing static class libraries with dynamic class libraries

Library type

Pros

Cons

Dynamic library

  • No need to recompile .swfs that incorporate the class library when the class library .swf changes (you need to recompile the dynamic library only once, not recompile the .swf s that include it)

  • Reduces overall file size when used across multiple .swf files

  • Loaded only if necessary, incurring download cost when actually used or not at all if not used

  • Harder to create, maintain, use, and distribute

  • Easier to steal

Static library

  • Easier to create, maintain, use, and distribute

  • Harder to steal

  • Need to recompile all .swf files that use the library if library changes

  • Adds to file size of every .swf

  • Completely included in every .swf file that incorporates the library, whether or not all the classes in the library are referenced at runtime


Whether you prefer to use a static class library or a dynamic class library, if you want to ship a class library without also shipping the source code, you'll need to build a static class library to start with. In the case of shipping a dynamic class library without also shipping source code, you must provide the static class library for the sake of compile-time type checking. Hence, we'll learn how to create a static class library first.

14.2.2 Creating a No-Source Compile-time Class Library

A compile-time class library (a.k.a. a static class library) takes the form of a Flash MX 2004 component, which includes the following:

  • The compiled bytecode for the classes in the library

  • An intrinsic class definition file for each class in the library (this provides type checking information to the compiler)

  • An optional icon .png file for use in the Flash MX 2004 Components panel

In Flash MX 2004, components are fundamentally visual entities. They are subclasses of the MovieClip class and intended primarily as reusable GUI widgets. The type of class library we want to build, by contrast, is not visual at all. It is simply a collection of code that can be added to an application. Hence, shipping a nonvisual class library as a component is, in part, an exercise of fitting a square peg into a round hole. That is, some aspects of the component-creation process are obviously not related to the basic task of shipping class libraries. However, these aspects are required due to the architecture of components in Flash MX 2004. What matters in the end is that it all works.

To learn how to create a compile-time class library as a component, we'll return to our fictitious web shop, Spider Services. This time, suppose we want to ship our TextAnimation class in a compile-time class library, which we'll again call spidercore . Here are the general tasks we have to complete:

  1. Create the classes in the class library (in our case, just the TextAnimation class).

  2. Create the component icon (in our case, a little spider) as a .png file.

  3. Create the component class, called ClassLoader .

  4. Create the component.

  5. Distribute the component to other developers, providing instructions on how to use it.

Now let's look at each task individually. The source files discussed in the next several sections can be downloaded at http:// moock .org/eas2/examples.

Note that some steps in the following sections are repeated from the earlier section "Loading Classes at Runtime." If you have completed the procedures in that section already, you can skip some of the steps in the following sections.

14.2.2.1 Create the classes in the class library

The first step in creating a class library is to create the classes themselves . Refer to the earlier section "Create the classes in the class library," which I shan't repeat here.

14.2.2.2 Create the component icon

We're going to give our component an icon, not only for cosmetic appearances in the Flash MX 2004 Components panel but also so that we have an image to put in our component symbol. As we'll soon see, the component symbol should not be empty, otherwise instances of it will not show up on stage at authoring time. Note that for components created simply to contain a class library, such as ours, it's appropriate to use the same icon for the Components panel and for the content of the component itself. The similarity helps draw a connection between the component in the Library of a .fla file and an instance of the component placed on the Stage.

To create the component icon, follow these steps:

  1. Create the following working directory for the component source files: c:\data\spiderservices\spidercore . (You will have done this step already if you completed the procedures earlier under "Loading Classes at Runtime.")

  2. Create an 18 x 18-pixel image and save it in .png format using, say, Adobe Photoshop, Macromedia Fireworks, or your favorite image editor. Our image is a little illustration of a spider, as seen in Figure 14-1.

  3. Save the image as c:\data\spiderservices\spidercore\spidercore.png .

Figure 14-1. Our component icon
figs/as2e_1401.gif

14.2.2.3 Create the component class

We must now create the class that will be associated with our component. Because our component's only purpose is to load other classes, our component class simply lists the classes we want to include in our class library. The component class is called ClassLoader because loading classes is its only purpose. Here's the source code for the ClassLoader class, which is stored in a file named ClassLoader.as in the directory c:\data\actionscript\com\spiderservices :

 [IconFile("spidercore.png")] class com.spiderservices.ClassLoader extends MovieClip {   public function setSize ( ) {     _width = 18;     _height = 18;   }   public function doNothing ( ):Void {     // Trick the compiler into including     // the   TextAnimation   class in the component.     com.spiderservices.effects.TextAnimation;   } } 

The ClassLoader class has four key features:

  • It uses a component metadata tag to specify the name of the image icon, spidercore.png , to use in the Flash MX 2004 Components panel. (A component metadata tag tells the compiler something about a component, and you need not concern yourself with the technical details for the purposes of this discussion. For more information, see Using Components Creating Components > Component Metadata in the online Help.)

     [IconFile("spidercore.png)] 

  • It extends MovieClip . This is a necessary evil required by the Flash MX 2004 component architecture.

     class com.spiderservices.ClassLoader extends MovieClip { 

  • It implements a setSize( ) method, indicating how an instance of the component should be sized on the Stage. This is another necessary evil required by the Flash MX 2004 component architecture.

     public function setSize ( ) {   _width = 18;   _height = 18; } 

  • In a dummy method, doNothing( ) , ClassLoader lists all the classes that should be included in our class library. Listing the classes forces the compiler to include them when generating the component. The compiler finds and includes all dependent classes automatically (for example, if the TextAnimation class uses a Tween class, then the Tween class will be included in the spidercore component automatically).

 public function doNothing ( ):Void {   com.spiderservices.effects.TextAnimation; } 

The preceding four features are, in truth, quite bizarre ”you likely wouldn't expect the ClassLoader class to be required at all! At least, once the general structure is in place, it rarely needs to be altered (so you can forget how strange creating the class loader was after it's done).

14.2.2.4 Create the component

Now that the ClassLoader class is ready, we can create our spidercore component, which is what will contain the class library we'll distribute to the world.

The spidercore component is a movie clip symbol with a so-called component definition that transforms it from a plain old movie clip into a component. The movie clip symbol resides in a .fla file ( spidercore.fla ), which we'll create specifically for the purpose of producing the spidercore component.

To create spidercore.fla , follow these steps:

  1. Create a new .fla file in the following location: c:\data\spiderservices\spidercore\spidercore.fla . (Note that the spidercore.png must be in the same folder as the spidercore.fla file in order for the spidercore component icon to be included with the component.)

  2. Add c:\data\actionscript to the global classpath using Edit Preferences ActionScript Language ActionScript 2.0 Settings. (Again, you might have done this step already.)

To create the spidercore component, follow these steps:

  1. In the spidercore.fla file, choose Insert New Symbol (Ctrl-F8) to create a new movie clip symbol.

  2. Name the new symbol spidercore .

  3. In the spidercore.fla Library, right-click (Windows) or Cmd-click (Mac) the spidercore symbol and select the Linkage option.

  4. Under Linkage, be sure the Export for ActionScript and Export in First Frame options are checked.

  5. For the Linkage Identifier, enter spidercore .

  6. For AS 2.0 Class, enter com.spiderservices.ClassLoader .

  7. Click OK to accept the Linkage settings specified in Steps 4-6.

  8. In the spidercore.fla 's Library, right-click (Windows) or Cmd-click (Mac) the spidercore symbol and select Component Definition from the contextual menu.

  9. In the Component Definition dialog box, for AS 2.0 Class, enter com.spiderservices.ClassLoader .

  10. Under Options, check Display in Components Panel.

  11. For Tool Tip Text, enter SpiderCore Class Library .

  12. Click OK to accept the Component Definition settings specified in Steps 9-11.

Now we must place some arbitrary content in the spidercore symbol so that instances of it can appear on the Stage at authoring time. If we skip this important step, instances on the Stage will completely disappear when deselected! To add content to the spidercore symbol, follow these steps:

  1. To edit the spidercore symbol, double-click it in the spidercore.fla document's Library.

  2. Select File Import Import to Stage (Ctrl-R in Windows or Cmd-R on Mac).

  3. Choose the spidercore image file: c:\data\spiderservices\spidercore\spidercore.png .

  4. Ensure that the image appears in the spidercore symbol, with its top-left corner aligned to the symbol's registration point, which is indicated by a small crosshair in the center of the symbol.

  5. Save the spidercore.fla file.

Our component is now complete. All that's left to do is export its .swc file, which is the compiled component that we'll distribute to the world. Follow these steps:

  1. In the spidercore.fla 's Library, right-click (Windows) or Cmd-click (Macintosh) on the spidercore symbol and select Export SWC File from the contextual menu.

  2. Save the .swc file as c:\data\spiderservices\spidercore\spidercore.swc .

The exported spidercore.swc file is our finished class library component. We can now give the component to other developers to add to their Flash MX 2004 Components panel, for inclusion in any .fla file.

14.2.2.5 Distribute the component to developers

Anyone who wishes to use the class library must obtain a copy of spidercore.swc and install it in Flash MX 2004. Here's the set of instructions we include in our class library documentation to help users install the spidercore component:

  1. Open the following folder, substituting your operating-system user account name for USER and your Flash language code for LANGUAGE_CODE, such as "en" for English:


    On Windows

    c:\Documents and Settings\ USER /Local Settings\Application Data/Macromedia\Flash MX 2004\ LANGUAGE_CODE \Configuration\Components\

    Note that the Local Settings folder is a hidden folder that must be revealed explicitly in Windows File Explorer using Tools Folder Options View Advanced Settings Files and Folders Hidden Files and Folders Show Hidden Files and Folders.


    On Mac

    HD:/Users/ USER /Library/Application Support/Macromedia/Flash MX 2004/ LANGUAGE_CODE /Configuration/Components/

  2. In the Components folder, create a new folder named Spider Services .

  3. Copy spiderservices.swc to Components/Spider Services .

  4. If you are currently running the Flash authoring tool, click the Components panel's pop-up Options menu (top right of the panel) and select Reload.

    The spidercore component should now appear in the Components panel.

14.2.2.6 Try it out

Let's try using our spidercore component by adding it to a movie. We'll pretend we're creating a web site for the Mega Bridal Depot, and we want to use the text animation effect in our spidercore class library. Follow these steps:

  1. Install the spidercore component in the Components panel as described in the preceding section.

  2. Create the following working directory for the Mega Bridal Depot project: c:\data\spiderservices\megabridaldepot .

  3. Create a new .fla file named mbd_home.fla in the following location: c:\data\spiderservices\megabridaldepot\mbd_home.fla .

  4. Drag an instance of the spidercore component from the Components panel to the Stage of mbd_home.fla .

  5. Delete the instance you just dragged to the Stage. (The component is required in the .fla file's Library only, not on stage.)

  6. Be sure never to change the Linkage settings for the component. The settings should always be set to Export for ActionScript and Export in First Frame. To change the frame on which the classes in the component are exported, use File Publish Settings ActionScript Version Settings Export Frame for Classes.

  7. On frame 1 of Layer 1 in mbd_home.fla , add the following code:

     import com.spiderservices.effects.TextAnimation; var textani:TextAnimation = new TextAnimation( ); 

  8. Export a test .swf file from mbd_home.fla using Control Test Movie (Ctrl-Enter on Windows or Cmd-Enter on Mac).

If you followed the preceding steps correctly, the Output panel should display:

 Imagine a text effect with great majesty. 

14.2.3 Creating a No-Source Runtime Class Library

Earlier in this chapter, under "Loading Classes at Runtime," we learned that a runtime-loaded class library can save overall file size when the same classes are used across multiple .swf files. However, the implementation we studied earlier for runtime class libraries required users of the library to have access to the library's source code.

By combining a no-source compile-time class library with a runtime class library, we can distribute a runtime class library without distributing any class source code. Here's the technique:

  1. Create a class library .swf file, someLib_runtime.swf , as described earlier under "Loading Classes at Runtime."

  2. Create a class library component, someLib , as described under "Creating a No-Source Compile-time Class Library."

  3. Create a .fla file, someApp.fla , that loads someLib_runtime.swf , as described under "Loading Classes at Runtime."

  4. Add the someLib component to someApp.fla , as described under "Distribute the component to developers."

  5. Create an exclusion XML file, someApp_exclude.xml , for someApp.fla , as described earlier under "Loading Classes at Runtime."

  6. Select File Export Export movie to export someApp.swf from someApp.fla .

Because of the someApp_exclude.xml file, the movie someApp.swf will not contain the classes included in the someLib component. Instead, someApp.swf will load the classes from someLib_runtime.swf .

Look, Mom! No source!

 <  Day Day Up  >  


Essential ActionScript 2.0
Essential ActionScript 2.0
ISBN: 0596006527
EAN: 2147483647
Year: 2004
Pages: 177
Authors: Colin Moock

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