Learning ActionScript 2.0 in Adobe Flash |
|
|
|
| Syntax and Language Fundamentals > About operators > 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: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.
The Output panel displays 5.
myClip["spam"] = 10;
The Output panel displays 10.
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.
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.
|
|
|
|