Connecting Objects to ActionScript Classes

One transaction underlies everything you will do with ActionScript: connecting objects defined in the timeline with classes defined in external ActionScript files (those files whose names end in .as).

The procedure is simple. After you have defined your object in the Library of your main movie (.fla file), right-click (Windows) or click and hold (Mac) on the object's icon in the Library. Select Linkage from the pop-up menu. You should then see a dialog box like the one shown here.

Linkage Properties dialog

When this dialog box opens, all the entry fields will be gray, and the check boxes will be unselected. Check the box named Export for ActionScript. This will cause Export in first frame also to become checked. Leave other check boxes alone.

The bottom two of the three type-in fields become active as soon as you check Export for ActionScript. The top field remains gray, and you can ignore it, since it is merely a holdover from ActionScript 2.

There should be a value in the bottom-most field, Base Class. The value will depend on the type of object you created in the Library. In our example, we've created a Movie Clip, so Flash selects the default Base Class for this object, which is flash.display.MovieClip. A Base Class is a set of pre-defined code, built into the Flash player, that enables Movie Clips to function. You can change this Base Class, but we'll leave that option unexplored for the moment. (It is explained in later chapters of Perkins.)

Right now, the most important area of the Linkage Properties dialog is the field named Class. This is where you establish the connection between your Library object and the external ActionScript (.as) file.

By default, this field will contain the name you gave your object (or Symbol) when you created it in the Flash Library. You should change this name.

The value shown in our example, helloWorldClass, connects the mc_hello Movie Clip to a class defined in the file called helloWorldClass.as.

There is no standard naming convention for class files, but I recommend two rules: do NOT use the same name for your class that you used for the object in the Library; DO use the word class in the class file name.

The class file name is highly important, and must match exactly the name of the .as file to which it links. So if my external file is called HelloWorldClass.as, the linkage we're trying to establish here will not work -- capitalization counts, and Flash will notice that we typed a small h in the Linkage dialog, but a capital H in the class file name. (Generally speaking, all class files should be named in lower case, with capitals used only when you run words together in what's called camelCase.)

Note that you DO NOT enter the file extension in the Class field of the Linkage Properties dialog. You enter only the file name, minus extension. This is odd, confusing, and a likely source of error (thanks, Adobe), but pay attention to this detail. It can cause problems if you overlook it.

Finally, note that our example assumes the file hellowWorldClass.as is located in the same directory as the .fla file for the main movie. There are ways to link objects to class files anywhere you like, but in this course, we will always put the class file in the same directory as the main movie.


University of Baltimore Logo

Last updated: 02/03/08 17:00:58
Copyright © 2008 School of Information Arts and Technologies