Working with Classes and Objects in ActionScript 3.0
The most important knowledge about ActionScript and object-oriented programming that you can possess is knowing how to work with objects and classes. You may remember from reading Chapter 1 that everything in ActionScript is an object. A MovieClip is an instance of the MovieClip class; a Sprite is an instance of the Sprite class, and so on.
Because objects and classes are so central to working with ActionScript, nearly every chapter of this book delves into working with classes and objects. You will have gained plenty of experience with them by the time you’ve finished reading and working through the step-by-step instructions. But before you move on, you must understand the basic concepts, terminology, and definitions of classes and objects.
This chapter provides a high-level overview, beginning with the definition of classes, objects, and class hierarchy and inheritance (the “family tree” of how classes are stored and referenced). It also provides opportunities for you to practice importing classes and instantiating objects using ActionScript so that you can call their methods and set their properties. Finally, this chapter shows you how to work with external ActionScript files, which enables you to work with different ActionScript editors and to separate programming from design.
Defining Classes and Objects
A class is the blueprint for an object. It defines everything about the object: how it looks, how it acts, what it does, what data it processes, and how it is built.
An object is an instance of a class, or a copy of the class’s blueprint. Once an object of a class is instantiated, you have access to the public properties, events, and methods of that class to manipulate the object for your programming and display purposes.
Class hierarchy involves the way in which classes are categorized. Classes are external ActionScript files that define objects; they are stored and organized in a specific directory structure in the Flash CS3 program directory (Figure 4.1). To use them in your code, you have to import them.
Figure 4.1 Class package locations of class files in the Flash program directory.
To import the ActionScript class files into your code, you don’t need to know the full computer path to the files (the compiler will do that for you), but you do need to know how the classes are categorized. To import a particular class, you need to know what directory, or class package, they are stored in. For example, the MovieClip class is located in the flash.display package. The full import path to the package would thus be import flash.display.MovieClip;.
The last concept that it’s important to be familiar with is class inheritance. As a general rule, the deeper into a package a class gets, the more it inherits from the parent classes for its core functionality. As discussed in Chapter 3, the MovieClip class has inherited methods from other classes to help extend its functionality. Classes share, or inherit, methods, properties, and events from one another, so functionality has to be defined only once and shared throughout the class structure. This is not an entirely new concept for ActionScript 3, but it has been universally implemented in this version. This helps improve code performance, reduces the file size of imported classes, and makes future updates and additions to the ActionScript API easier to implement.
We’ll be seeing more of classes and subclasses throughout the rest of this book, since many classes we’ll work with are subclasses of different class packages.