Writing Scripts with ActionScript > Using predefined objects

Using predefined objects

You can use Flash's predefined objects to access certain kinds of information. Most predefined objects have methods (functions assigned to an object) that you can call to return a value or perform an action. For example, the Date object returns information from the system clock and the Sound object lets you control sound elements in your movie.

Some predefined objects have properties whose values you can read. For example, the Key object has constant values that represent keys on the keyboard. Each object has its own characteristics and abilities that can be used in your movie.

The following are Flash's predefined objects:

Array
Boolean
Color
Date
Key
Math
MovieClip
Number
Object
Selection
Sound
String
XML
XMLSocket

Movie clip instances are represented as objects in ActionScript. You can call predefined movie clip methods just as you would call the methods of any other ActionScript object.

For detailed information on each object, see its entry in ActionScript dictionary: Overview.


 
Creating an object

There are two ways to create an object: the new operator and the object initializer operator ({}). You can use the new operator to create an object from a predefined object class, or from a custom defined object class. You can use the object initializer operator ({}) to create an object of generic type Object.

To use the new operator to create an object, you need to use it with a constructor function. (A constructor function is simply a function whose sole purpose is to create a certain type of object.) ActionScript's predefined objects are essentially prewritten constructor functions. The new object instantiates, or creates, a copy of the object and assigns it all the properties and methods of that object. This is similar to dragging a movie clip from the Library to the Stage in a movie. For example, the following statements instantiate a Date object:

currentDate = new Date();

You can access the methods of some predefined objects without instantiating them. For example, the following statement calls the Math object method random:

Math.random();

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

To create an object with the new operator in Normal Mode:

1 Choose setVariable
2 Enter an identifier in the Name field.
3 Enter new Object, new Color, and so on in the Value field. Enter any arguments required by the constructor function in parentheses.
4 Check the Expression box of the Value field.
If you don't check the Expression box, the entire value will be a string literal.

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

c = new Color(this);

Note: An object name is a variable with the object data type assigned to it.

To access a method in Normal Mode:

1 Select the evaluate action.
2 Enter the name of the object in the Expression field.
3 Enter a property of the object in the Expression field.

To use the object initializer operator ({}) in Normal Mode:

1 Select the setVariable action.
2 Enter name in the Variable field; this is the name of the new object.
3 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:

myCircle = {radius: 5, area:(pi * radius * radius)};

The parentheses cause the expression to evaluate. The returned value is the value of the variable area.

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

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

For detailed information on each object, see its entry in ActionScript dictionary: Overview.


 
Accessing object properties

Use the dot (.) operator to access the value of properties in an object. The name of the object goes on the left side of the dot, and the name of the property goes on the right side. For example, in the following statement, myObject is the object and name is the property:

myObject.name

To assign a value to a property in Normal Mode, use the setVariable action:

myObject.name = "Allen";

To change the value of a property, assign a new value as shown here:

myObject.name = "Homer";

You can also use the array access operator ([]) to access the properties of an object. See Dot and array access operators.


 
Calling object methods

You can call an object's method by using the dot operator followed by the method. For example, the following example calls the setVolume method of the Sound object:

s = new Sound(this);
s.setVolume(50);

To call the method of a predefined object in Normal Mode, use the evaluate action.


 
Using the MovieClip object

You can use the methods of the predefined MovieClip object to control movie clip symbol instances on the Stage. The following example tells the instance dateCounter to play:

dateCounter.play();

For detailed information on the MovieClip object, see its entry in ActionScript dictionary: Overview.


 
Using the Array object

The Array object is a commonly used predefined ActionScript object that stores its data in numbered properties instead of named properties. An array element's name is called an index. This is useful for storing and retrieving certain types of information such as lists of students or a sequence of moves in a game.

You can assign elements of the Array object just as you would the property of any object:

move[1] = "a2a4";
move[2] = "h7h5";
move[3] = "b1c3";
...
move[100] = "e3e4";

To access the second element of the array, use the expression move[2].

The Array object has a predefined length property that is the value of the number of elements in the array. When an element of the Array object is assigned and the element's index is a positive integer such that index >= length, length is automatically updated to index + 1.