include Versus Classpath


#include Versus Classpath

External ActionScript files are not new to Flash, but they were introduced differently in Flash 2004 and are brought in at compile time.

In older versions of Flash, if you wanted to include an external AS file, you would use a pound sign (#) followed by the keyword include, and the path to the AS file as a string literal like this:

 #include "myFile.as" 

Notice that this line of code is not followed by a semicolon like most lines in ActionScript. An include statement cannot be followed by a semicolon or errors may occur. Also, do not put code directly below an include statement or errors might occur. Put at least one line of space between include statements and other code.

That was the old way of including external ActionScript in Flash files and it will still work if you want to use it. However, the new way is much simpler and it is called classpath.

The classpath is a path or group of paths to where external ActionScript files can be found. And to include information from within one of the files, you simply call the constructor of the object class and Flash will look for a reference to that object in the classpath.

Here is an example of an external AS file:

 class MyClass{     //class definition } 

And provided that this external AS file is within the classpath, the ActionScript to use it in Flash would be as follows:

 var sampleClass:MyClass = new MyClass(); 

When this action is called, it will look through the classpath to see if there is a class named MyClass in it (which incidentally will be a file named MyClass.as). You can also keep external AS files in the same directory as the Flash file that is trying to use them, and if the class being referred to in Flash cannot be found in the defined paths of the classpath, it will then look in its own directory, which is also controlled by the classpath.

NOTE

External ActionScript files are compiled directly into the SWF file when the SWF file is created, so if an external AS file is updated, the Flash file using it must be recompiled before the changes will take effect.


You can set the classpath in your preferences by going to Edit>Preferences and choosing the ActionScript category (see Figure 18.2). Then choose the ActionScript 2.0 Settings button and the ActionScript Settings dialog box pops up as shown in Figure 18.3. Here you can add classpaths, remove them, or edit them. You can also sort the order in which they are checked. Notice that it has two directories by default. The first one listed is the ".", which represents the same directory as the Flash file you are working in. The second classpath is where Flash's built-in classes are keptin the Classes directory of the user's configuration directory.

Figure 18.2. ActionScript preferences for all Flash documents.


Figure 18.3. ActionScript Settings dialog box where you can control your classpaths.


At the top of the ActionScript settings box, there is a field labeled Export Frame for Classes, and it has the number 0 as its value, which means that before the first frame of ActionScript is reached, it will compile the external code that is necessary for that file. But the field is grayed out and cannot be adjusted. This is because there is another spot where you can adjust your classpath settings, in the Publish Settings dialog box, in the Flash section, as shown in Figure 18.4. If you have the ActionScript version set to ActionScript 2.0 (the default), you can click the Settings button, and an ActionScript settings dialog box appears as in Figure 18.5. The difference is that there are no default classpaths, and you can adjust the export frame. This ActionScript setting is for the current file only and will not be kept from file to file (unless you save it as part of your publishing profile). And even though you can adjust the export frame, it will not let you go lower than the number zero.

Figure 18.4. Publish settings for the Flash tab.


Figure 18.5. ActionScript settings for the publish settings.


Now that you have seen where to create external ActionScript, and how to use it, let's start creating these files.




Macromedia Flash Professional 8 Unleashed
Macromedia Flash Professional 8 Unleashed
ISBN: 0672327619
EAN: 2147483647
Year: 2005
Pages: 319

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