Understanding the ActionScript Language > About scripting in ActionScript > About object-oriented scripting

 

About object-oriented scripting

In object-oriented scripting, information is organized into groups called classes. You can create multiple instances of a class, called objects, to use in your scripts. You can create your own classes and use the built-in ActionScript classes; the built-in classes are located in the Objects folder of the Actions panel.

When you create a class, you define all the properties (characteristics) and methods (behaviors) of each object it creates, just as real-world objects are defined. For example, a person could be said to have properties such as gender, height, and hair color and methods such as talk, walk, and throw. In this example, Person would be a class, and each individual person would be an object, or an instance of that class.

Objects in ActionScript can be pure containers for data, or they can be graphically represented on the Stage as movie clips, buttons, or text fields. All movie clips are instances of the built-in class MovieClip, and all buttons are instances of the built-in class Button. Each movie clip instance contains all the properties (for example, _height, _rotation, _totalframes) and all the methods (for example, gotoAndPlay, loadMovie, startDrag) of the MovieClip class.

To define a class, you create a special function called a constructor function. (Built-in classes have built-in constructor functions.) For example, if you want information about a bicycle rider in your movie, you could create a constructor function, Biker, with the properties time and distance and the method getSpeed, which tells you how fast the biker is traveling:

function Biker(t, d) {
	this.time = t;
	this.distance = d;
	this.getSpeed = function() {return this.time / this.distance;};	
}

In this example, you create a function that needs two pieces of information, or parameters, to do its job: t and d. When you call the function to create new instances of the object, you pass it the parameters. The following code creates instances of the object Biker called emma and hamish.

emma = new Biker(30, 5);
hamish = new Biker(40, 5);

In object-oriented scripting, classes can receive properties and methods from each other according to a specific order; this is called inheritance. You can use inheritance to extend or redefine the properties and methods of a class. A class that inherits from another class is called a subclass. A class that passes properties and methods to another class is called a superclass. A class can be both a subclass and a superclass.

For more information, see Using a built-in object and Creating inheritance.