Download (direct link):
As mentioned in the introduction to this chapter, an object is a programming construct that has intrinsic qualities and characteristics. This is really the same idea as an object you can see in the so-called real world. For example, a book is an object. Books have intrinsic qualities — each book has a title, an author, a publisher, a page count, and so on. In programming terms, these qualities or characteristics are called properties.
In addition to having properties that are descriptive, objects can also perform actions. Your computer is an object. It is capable of performing actions such as turning on and off, opening and closing applications, and so on. Cars can accelerate, birds can fly, and so on. These are all examples of actions that objects can perform. When programmers talk about the actions that ActionScript objects can perform, we call them methods.
¦ ¦ ¦ ¦ In This Chapter
Understanding objects and classes
Using properties and methods
Working with objects as associative arrays
Using packages to organize classes
Using interfaces to define rules for classes ¦¦¦¦
Understanding Object Blueprints
Objects can be categorized according to the blueprint from which they were created. This is true of nonprogramming objects as well. You can categorize all cars as car objects. Clearly not all cars are exactly the same. Even two cars that rolled off the same line one after the other are going to have some differences, and cars of different make, model, and year are going to be quite different. Nonetheless, all
144 Part II ¦ Laying the ActionScript Foundation
cars can be said to have similar characteristics such as having tires, engines, steering wheels, and so on. Likewise, all cars (okay, well, all running cars) are capable of the action of accelerating, braking, and the like. It is possible to say, therefore, that cars all derive from the same fundamental blueprint.
In this same way, objects in ActionScript can be categorized based on the type of blueprint from which they have been derived. In programming terms, the blueprint is called a class.
The class defines all the basic properties and methods for any objects that are derived from it. For example, a class with which you may already be familiar, the MovieClip class, defines properties such as _x and _y as well as methods such as play(). These properties and methods are defined in the class and then each MovieClip object inherits them.
If you look in the Actions toolbox you should find a folder named Built-In Classes. In that folder are several subfolders organizing all the built-in classes in ActionScript. Using ActionScript you can create objects from the built-in classes as well as from custom classes that you (or someone else) have created. First, let’s take a look at the built-in classes. Then, later on in the chapter, you learn how you can create your own custom classes.
Creating an Object
Before you tell Flash to create an object, all you have is the blueprint — the class. You need to specifically tell Flash that you want it to create an instance of that class. An instance of a class is synonymous with an object that is derived from the class. In most cases, you create an instance of a class by invoking the class’s constructor. The constructor is a special function that shares the name of the class and creates a new instance. The constructor should be invoked as part of a new statement, and the returned value (the new instance) can be assigned to a variable.
var varName -.datatype = new ClassName();
The datatype you should use to declare the variable should match the name of the class from which you are instantiating the object.
The constructor function, just like any other function, might not accept parameters. This depends entirely on how the constructor has been defined. In Chapters 9 through 27 you’ll get a chance to review the constructors for all the built-in classes, and so you’ll know what, if any, parameters their constructors expect. Also, when you use code hinting in the Actions panel, you can quickly see what parameters a given constructor may expect.
To begin with, let’s take a look at the most basic kind of object there is — an Object object. The Object class is the most fundamental class in all of ActionScript, so it is a good place to start. Here is how you can create a new Object i nstance using the constructor.
var oFirstObj:Object = new Object();
That wasn’t too difficult now, was it? You can now use the instanceof operator (see Chapter 5) to verify that oFirstObj i s, in fact, an instance of the Object class.
trace(oFirstObj instanceof Object); // Displays: true
Now that you’ve seen how to create a basic Object object, how about creating a String object? When you create a String object using the constructor, you typically will want to pass the constructor a string literal (a quoted string) as a parameter.
var sTitle:String = new String("ActionScript Bible");
Again, you can test to verify that the new object is actually an instance of the String class.