Example: Using custom class files in Flash
277
To specify package paths, use dot (
.
) notation to separate package directory names. Package
paths are hierarchical; that is, each dot represents a nested directory. For example, suppose you
create a class named ClassName that resides in a
com/macromedia/docs/learnAs2
package
in your classpath. To create an instance of that class, you could specify the fully qualified
class name.
You can also use the fully qualified class name to type your variables, as shown in the
following example:
var myInstance:com.macromedia.docs.learnAs2.ClassName = new
com.macromedia.docs.learnAs2.ClassName();
You can use the
import
statement to import packages into a script, which lets you use a class’s
abbreviated name rather than its fully qualified name. You can also use the wildcard character
(
*
) to import all the classes in a package. If you use the wildcard character, you don’t need to
use the fully qualified class name each time you use the class.
For example, suppose that in a script you imported the above class using the
import
statement, as shown in the following example:
import com.macromedia.docs.learnAs2.util.UserClass;
Later, in the same script, you could reference that class by its abbreviated name, as shown in
the following example:
var myUser:UserClass = new UserClass();
You can use the wildcard character (
*
) to import all the classes in a given package. Suppose
you have a package named
com.macromedia.utils
that contains two ActionScript class files,
ClassA.as and ClassB.as. In another script, you could import both classes in that package
using the wildcard character, as shown in the following code:
import com.macromedia.utils.*;
The following example shows that you can then reference either of the classes directly in the
same script:
var myA:ClassA = new ClassA();
var myB:ClassB = new ClassB();
The
import
statement applies only to the current script (frame or object) in which it’s called.
If an imported class is not used in a script, the class is not included in the resulting SWF file’s
bytecode, and the class isn’t available to any SWF files that the FLA file containing the
import
statement might load.
NO
T
E
The following exercise is part of
“Example: Using custom class files in Flash”
on page 276
which continues the examples
“Example: Writing custom classes”
. If you
need ClassA and ClassB, you can download the class files from
www.helpexamples.com/flash/learnas/classes/
.
Summary of Contents for FLASH 8-LEARNING ACTIONSCRIPT 2.0 IN FLASH
Page 1: ...Learning ActionScript 2 0 in Flash...
Page 8: ...8 Contents...
Page 18: ...18 Introduction...
Page 30: ...30 What s New in Flash 8 ActionScript...
Page 66: ...66 Writing and Editing ActionScript 2 0...
Page 328: ...328 Interfaces...
Page 350: ...350 Handling Events...
Page 590: ...590 Creating Interaction with ActionScript...
Page 710: ...710 Understanding Security...
Page 730: ...730 Debugging Applications...
Page 780: ...780 Deprecated Flash 4 operators...
Page 830: ...830 Index...