![]() ![]() ![]() | |
![]() | |
![]() | |
![]() |
Note: If you have never used ActionScript to write object-oriented scripts and don't need to target Flash Player 5, you should not use the information in this section, because writing scripts using ActionScript 1 is deprecated; instead, see Creating Classes with ActionScript 2.0 for information on using ActionScript 2.0.
ActionScript is an object-oriented programming language. Object-oriented programming uses objects, or data structures, to group together properties and methods that control the object's behavior or appearance. Objects let you organize and reuse code. After you define an object, you can refer to the object by name without having to redefine it each time you use it.
A class is a generic category of objects. A class defines a series of objects that have common properties and can be controlled in the same ways. Properties are attributes that define an object, such as its size, position, color, transparency, and so on. Properties are defined for a class, and values for the properties are set for individual objects in the class. Methods are functions that can set or retrieve properties of an object. For example, you can define a method to calculate the size of an object. Like properties, methods are defined for an object class, and then invoked for individual objects in the class.
ActionScript includes several built-in classes, including the MovieClip class and others. You can also create classes to define categories of objects for your applications.
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 application, 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.
An object is a complex data type containing zero or more properties and methods. Each property, like a variable, has a name and a value. Properties are attached to the object and contain values that can be changed and retrieved. These values can be of any data type: String, Number, Boolean, Object, MovieClip, or undefined. The following properties are of various data types:
customer.name = "Jane Doe"; customer.age = 30; customer.member = true; customer.account.currentRecord = 000609; customer.mcInstanceName._visible = true;
The property of an object can also be an object. In line 4 of the previous example, account
is a property of the object customer
and currentRecord
is a property of the object account
. The data type of the currentRecord
property is Number.
![]() | |
![]() | |
![]() | |
![]() ![]() ![]() |