Using dot and array access operators

You can use the dot operator (.) and the array access operator ([]) to access built-in or custom ActionScript properties. You use dot operators to target certain indexes in an object. For example, if you have an object that contains some user information, you can specify a certain key name in the array access operator to retrieve a user's name, as demonstrated in the following ActionScript:

var someUser:Object = {name:"Hal", id:2001};
trace("User's name is: " + someUser["name"]); // User's name is: Hal
trace("User's id is: " + someUser["id"]); // User's id is: 2001

For example, the following ActionScript uses the dot operator to set certain properties within objects:

myTextField.border = true;
year.month.day = 9;
myTextField.text = "My text";

The dot operator and the array access operator are very similar. The dot operator takes an identifier as its property, but the array access operator evaluates the contents to a name and then accesses the value of that named property. The array access operator lets you dynamically set and retrieve instance names and variables.

The array access operator is useful if you don't know exactly what keys are in an object. When this occurs, you can use a for..in loop to iterate through an object or movie clip and display its contents.

To use dot and array access operators:

  1. In a new Flash document, create a movie clip on the main Timeline.
  2. Select the movie clip and open the Property inspector.
  3. Type in an instance name of myClip.
  4. Add the following ActionScript to Frame 1 of the Timeline:
    myClip.spam = 5;
    trace(myClip.spam); // 5
    

    If you want to set a value in the myClip instance on the current timeline you can use the dot or array access operators, as demonstrated in this ActionScript. If you write an expression inside the array access operator, it evaluates that expression first and uses the result as the variable name.

  5. Select Control > Test Movie to test the document.

    The Output panel displays 5.

  6. Return to the authoring environment, and replace the first line of ActionScript with the following:
    myClip["spam"] = 10;
    
  7. Select Control > Test Movie to test the document.

    The Output panel displays 10.

  8. Return to the authoring environment, and double-click the myClip instance.
  9. Add four new instances inside the myClip instance.
  10. Use the Property inspector to add the following instance names to each of the four new instances: nestedClip1, nestedClip2, nestedClip3, nestedClip4.
  11. Add the following code to Frame 1 of the main Timeline:
    var i:Number;
    for (i = 1; i <= 4; i++) {
        myClip["nestedClip" + i]._visible = false;
    }
    

    This ActionScript toggles the visibility of each of the nested movie clips.

  12. Select Control > Test Movie to test the ActionScript you just added.

    Now the four nested instances are invisible. You're using the array access operator to iterate over each nested movie clip in the myClip instance and set its visible property dynamically. You save time, because you don't have to specifically target each instance.

You can also use the array access operator on the left side of an assignment, which lets you set instance, variable, and object names dynamically:

myNum[i] = 10;

In ActionScript 2.0, you can use the bracket operator to access properties on an object that are created dynamically, in case the class definition for that object is not given the dynamic attribute. You can also create multidimensional arrays using this operator. For more information on creating multidimensional arrays using array access operators, see Creating multidimensional arrays.