User Guide

Using ActionScript in Flex applications 51
Referring to external files
The
source attribute of the <mx:Script> tag and the #include directive refer to files in
different ways.
The following are the valid paths to external files that are referenced in an
<mx:Script> tag’s
source attribute:
Absolute URLs, such as http://www.macromedia.com or file:///C|/site_flashteam/foo.gif.
Site-relative URLs, such as /scripts/myscript.as. A URL that begins with a slash is resolved
relative to the context root of the application. The default application root is /flex_app_root.
Relative URLs, such as ../myscript.as. A relative URL that does not start with a slash is resolved
relative to the file that uses it. If the tag
<mx:Script source="../IncludedFile.as"> is
included in “mysite/myfiles/myapp.mxml”, then the system searches for
mysite/IncludedFile.as”.
For an ActionScript
#include directive, you can only reference relative URLs. You can specify file
locations relative to the context root (start the location with a slash) or relative to the document
root (omit the leading slash), as the following example shows:
#include "/myfiles/functions.as" // Relative to the context root. Resolves to
// /flex_app_root/myfiles/functions.as
#include "functions.as" // Relative to the current document root (in this case,
// in the same directory as the current document).
Flex searches the ActionScript classpath for imported classes and packages. Flex does not search the
ActionScript classpath for files that are included using the
#include directive or the source
attribute of the
<mx:Script> tag.
Importing classes and packages
If you create many utility classes or include multiple ActionScript files to get at commonly used
functions, you might want to store them in a set of classes in their own package. You can import
ActionScript classes and packages using the
import statement. By doing this, you do not have to
explicitly enter the fully qualified class names when accessing classes within ActionScript.
The following example imports the MyClass class in the MyPackage.Util package:
<mx:Script>
import MyPackage.Util.MyClass;
...
</mx:Script>
In your ActionScript code, instead of referring to the class with its fully qualified package name
(MyPackage.Util.MyClass), you can refer to it as MyClass, as the following example shows:
import MyPackage.Util.MyClass;
function createClass() {
tObj = new MyClass;
}
Note: All statements other than import and variable declarations must be inside functions in an
<mx:Script> tag.