User Guide
198 Chapter 7: Using the Built-In Classes
For more information, see the following topics:
• “Creating a new object” on page 198
• “Accessing object properties” on page 198
• “Calling object methods” on page 198
• “About class (static) members” on page 199
Creating a new object
To create an instance of an ActionScript class, use the
new operator to invoke the class’s
constructor function. The constructor function always has the same name as the class, and returns
an instance of the class, which you typically assign to a variable.
For example, the following code creates a new Sound object:
var song:Sound= new Sound();
In some cases, you don’t need to create an instance of a class to use its properties and methods. For
more information, see “About class (static) members” on page 199.
Accessing object properties
Use the dot (
.) operator to access the value of a property in an object. Put the name of the object
on the left side of the dot, and put the name of the property on the right side. For example, in the
following statement,
myObject is the object and name is the property:
myObject.name
The following code creates a new Array object and then shows its length property:
var my_array:Array = new Array("apples", "oranges", "bananas");
trace(my_array.length); // length is 3
You can also use the array access operator ([]) to access the properties of an object, such as using
the array access operator for debugging purposes. The following code loops over an object to
display each of its properties:
for (i in results) {
trace("the value of ["+i+"] is: "+ results[i]);
}
For more information, see “Dot and array access operators” on page 56.
Calling object methods
You call an object’s method by using the dot (
.) operator followed by the method. For example,
the following code creates a new Sound object and calls its
setVolume() method:
var mySound:Sound = new Sound(this);
mySound.setVolume(50);