Understanding the ActionScript Language > About built-in objects > Using a built-in object

 

Using a built-in object

Some built-in Flash objects are top-level objects; you can use the methods and properties of a top-level object without creating a new instance of the object. For example, to use the methods and properties of the top-level Math object, you use the name of the built-in object followed by the method or property, as in the following:

area = Math.PI * radius * radius;

Other built-in objects, like the Date object, require you to create a new instance of the object to use its methods and properties. You use the new operator with a constructor function to create an object. (A constructor function is a function that creates a new instance of an object.) The ActionScript built-in objects are prewritten constructor functions. When you create a new instance of a built-in object, all the properties and methods of that object are copied into the instance. This is similar to dragging a movie clip from the library to the Stage. For example, the following statement creates a new Date object called currentDate and then calls the getMinutes method:

currentDate = new Date();
currentMinute = currentDate.getMinutes();

In the following code, the object c is created from the constructor Color:

c = new Color(this);

Each object that requires a constructor function has a corresponding new element in its folder in the Actions panel—for example, new Color, new Date, new String, and so on.

You can also use the object initializer operator ({}) to create an object of the generic type Object.

 
To create an object with the new operator in normal mode:

1

Choose Window > Actions to open the Actions panel if it isn't already open.

2

In the Actions toolbox (at the left of the panel), click the Actions folder to open it, then open the Variables folder.

3

Double-click the set variable action.

4

Enter an identifier in the Variable box; this is the name of the new object.

5

Click in the Value box to place the insertion point. Then browse in the Actions toolbox to the object you want to create, and double-click new Date, new Color, and so on.

6

Select the Expression option next to the Value box.

If you don't select the Expression option, the entire value will be a string literal.

 
To use the object initializer operator ({}) in normal mode:

1

Choose Window > Actions to open the Actions panel if it isn't already open.

2

In the Actions toolbox, click the Actions folder to open it. Click the Variables folder to open it.

3

Double-click the set variable action.

4

Enter an identifier in the Variable box; this is the name of the new object.

5

Select the Expression option next to the Value box.

6

In the Value box, enter the property name and value pairs separated by a colon inside the object initializer operator ({}).

For example, in this statement the property names are radius and area and their values are 5 and the value of an expression:

myRadius = 5;
myCircle = {radius: myRadius, area:(Math.PI * myRadius * myRadius)};

The parentheses cause the expression inside them to evaluate. The returned value is assigned to the variable area.

You can also nest array and object initializers, as in this statement:

newObject = {name: "John Smith", projects: ["Flash", "Dreamweaver"]};

For more information on the Actions panel, see Writing Scripts with ActionScript. For detailed information on each object, see its entry in the ActionScript Dictionary.