Ultrashock Tutorials > Flash MX 2004 > ActionScript 2.0  
 
by Dave Yang, quantumwave.com - swfoo.com
Download Source Files 
 
ActionScript 2.0
 

01. Introduction
02. A little bit of OOP in ActionScript 1.0 
03. What's new in ActionScript 2.0?

04. New keywords and features for OOP
05. Scope and "this"
06. Private or protected?
07. Dynamic vs Static classes

08 Inheritance.
09. Overriding, Overwriting or Overloading?

10. Importing external class files
11. Interface
12. What is missing in ActionScript 2.0?
13. Conclusion

- discuss this tutorial -

10. Importing external class files

In 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 -
 
©2003 Ultrashock.com - All rights reserved