|
|
||||||||
| Ultrashock Tutorials > Flash MX 2004 > ActionScript 2.0 | ||||||||
|
||||||||
|
|
ActionScript 2.0 |
|
||||||
10. Importing external class filesIn order to better organize classes and to avoid class name conflicts, the concept of packages was introduced in many languages. ActionScript 2.0 introduces the import keyword that implements this functionality. What are naming conflicts? Imagine two different developers, each offering a code library, and in each library there is a class called Utilities. If both libraries are used together, either one class file will end up overwriting the other in the same folder, or Flash will get confused as to which Utilities class it should use. Class paths come to the rescue! By incorporating a path to a class, just like files on a hard drive, classes with the same name can co-exist. To reach a particular class, use the full class path with the import statement like this: import mx.events.EventDispatcher; After a class has been imported, referring to the class can be done by only including the class name, such as EventDispatcher: class MyBroadcaster {
public function MyBroadcaster() {
EventDispatcher.initialize(this);
}
}
You could also use the full path when referencing another class (without
using the import statement), but it can get tedious when used over and over:
class MyBroadcaster {
public function MyBroadcaster() {
mx.events.EventDispatcher.initialize(this);
}
}
When creating your own classes, place them into a project folder, or a common
class library folder of your own. Do NOT put your class files inside Flash's
application folder (i.e. <Flash install folder>\<language>\First
Run\Classes\) or user folder (e.g. <drive>:\Documents and Settings\<user>\Local
Settings\Application Data\Macromedia\Flash MX 2004\<language>\Configuration\Classes\).
Doing so can create synchronization issues (there are two sets of class
files and .aso files in two different locations), leading to possible compiler
errors of missing or mismatched class files. One common approach is to create
your own class path based on the reverse of your domain name, for example:
import com.quantumwave.alliance.rebel.Commander;To import all the classes in a folder, use the asterisk * for wild-card pattern matching: import com.quantumwave.*;This one step import statement loads all the classes from the quantumwave folder but only classes that are used in the movie are compiled to the SWF (so using this will not inflate the size of your application.) |
| - discuss this tutorial - |