- The Flash DOM
- The ActionScript Classes
- Extending the ActionScript Classes
- Summary
Extending the ActionScript Classes
When you're familiar with all the standard objects that ActionScript has to offer, it's possible to use the principles of object-oriented programming to put them to good use.
Creating Your Own Objects
The simplest of all ActionScript classes is named, unremarkably, Object. The Object class provides no methods and no properties, so you're free to add whatever you want.
For example, let's say that you want to create a new object called Jack that will have a number of properties. One way to go about this is to instantiate (copy) Object and then simply assign new properties afterward.
Jack = new Object(); Jack.Age = 24; Jack.Height = 178; Jack.Address = "58 Linemeer Road";
Another way of creating custom objects is to use a constructor function. As the name implies, constructor functions are used to "construct" new objects in the course of executing a few programming instructions. Here's an example:
function Person(Age, Height, Address) { this.Age = Age; this.Height = Height; this.Address = Address; } Jack = new Person(24, 178, "58 Linemeer Road");
Rather than starting from scratch by instantiating the Object class and then adding person-related properties one by one, a Person "recipe" is created in the form of the constructor function.
After the Jack object is created, its properties can be either used as is or applied to perform calculations. For instance, an equation could draw on Jack's Age property to determine his year of birth:
CurrentYear = 2001; YearOfBirth = CurrentYear Jack.Age;
In this case YearOfBirth comes out equal to 2001 24, or 1977.
But objects aren't just collections of propertiesthey can also do things. An object method is most commonly just a regular function that has been associated with that object. Here's an example, this time adding a new method to Jack:
function Talk() { trace("Hello there!"); } Jack.Talk = Talk;
Using association, the Jack object now has a method that can be invoked, like so:
Jack.Talk();
Executing this instruction results in the words "Hello there!" being printed in Flash's Output window during movie testing. Methods can be used to do anything that a regular function can, including performing a set of instructions and, if needed, returning a given value.
NOTE
Notice that when a function is being associated with an object, no parentheses are used, as in Jack.Talk = Talk;. Parentheses should follow the name of a function or method only when it is being invoked or run, as in Jack.Talk();.
Building on Existing Objects
Sometimes creating new objects from scratch using Object or constructor functions is the best solution. But other times extending one of ActionScript's existing objects is useful also.
Remember that when an object is instantiated from a class, it inherits all the methods of that class. After the object is created, it's possible to associate new methods and properties with it, to complement or replace the ones that it already has.
Jack, for instance, inherited the properties Age, Height, and Address when created using the Person constructor. But the method Talk needed to be added separately in the example because Person didn't include it.
In the case of the Person constructor function, it's easy to add the Talk method before creating Jack, to make sure that Jack inherits Talk:
function Talk() { trace("Hello there!"); } function Person(Age, Height, Address) { this.Age = Age; this.Height = Height; this.Address = Address; this.Talk = Talk; <- Talk is added to the constructor }
That's fine for objects that you created the recipes for yourself, but what about the built-in ActionScript objects, such as Sound and Array, the ones that have already been created and are now fixed?
The good news is that ActionScript supports what is known as the prototype property, which allows for new methods and properties to be added to existing classes. Those new additions then apply to any objects created using that class.
Allow me to demonstrate using the Person example. This time, instead of adding Talk to the constructor function, the prototype property is used to add Talk after the fact:
function Talk() { trace("Hello there!"); } function Person(Age, Height, Address) { this.Age = Age; this.Height = Height; this.Address = Address; } Jack = new Person(24, 178, "58 Linemeer Road"); Person.prototype.Talk = Talk; <- Talk is added to the Person class
Amazingly, even though Jack has already been created, it now possesses the Talk method, thanks to the prototype property of Person. All other subsequent objects created using the Person constructor will also inherit the Talk method.
So, imagine that you want to extend the standard MovieClip object so that all movie clips in your Flash project can move themselves to the top-left corner of the stage. By defining a new method and then adding it to the MovieClip prototype property, all movie clips now include that method:
function JumpHome() { this._x = 0; this._y = 0; } movieClip.prototype.JumpHome = JumpHome;
NOTE
Notice how the this keyword is used in the JumpHome function. The function works without naming a specific movie clip because this automatically points to whatever movie clip is contextually accurate at the time. this is a powerful tool when creating reusable functions.
Now let's say that you have a movie clip named CarGraphic, You can make it jump to the top-left corner with this command:
CarGraphic.JumpHome();
Although the JumpHome method did not previously exist, ActionScript will look up the prototype chain until it finds a match. In this case, the inheritance goes something like CarGraphic <- MovieClip <- Object.
If JumpHome was added to the prototype properties of both MovieClip and Object, the MovieClip version would take precedence because it appears first.