- Defining Classes and Objects
- Importing a Class
- Instantiating Objects
- Working with External Code
Importing a Class
You must import classes in ActionScript before you can construct objects and utilize methods, properties, and events of a class. Classes are imported at the beginning of the code, so unqualified class names can be used throughout the code. If a class is not imported, all references to a class need to be qualified. An example of using a qualified class name for constructing a MovieClip object would be as follows:
var mc:MovieClip = new flash.display.MovieClip();
The import directive for a class allows the use of unqualified (or shortened) class names in the code without indicating the package each and every time, thus saving lots of typing.
However, because the approach of this book is tailored to users who are new to ActionScript, all of the ActionScript code that you will construct is timeline based, which means you don’t have to import many of the classes that we use (Flash will automatically do this for us).
If you were to take a class-based approach to ActionScript, or if you were a developer using Flex, then you would need to import all of the classes used in your code.
So that you can become aware of what a more advanced ActionScript developer would do, and to help you see the big picture, I will include import statements whenever possible in the timeline, even though you don’t need to construct them.
If you do need to import a class, you would use the import statement followed by the class package path and the subclass that you wish to import.
To import classes:
To import the DisplayObject, Graphics, and Shape subclasses of the flash.display class package, add the following ActionScript:
import flash.display.DisplayObject; import flash.display.Graphics; import flash.display.Shape;
These classes are required to construct a Shape object, draw the shape on the stage, fill and stroke it with methods of the Graphics class, and add it to the display list on the stage using methods of the DisplayObject class.