158
Syntax and Language Fundamentals
5.
Select Frame 1 of the Timeline, and then type the following ActionScript in the
Actions panel:
var i:Number;
for (i = 0; i < 5; i++) {
this.attachMovie("libraryLinkageClassName", "clip" + i + "_mc", i,
{_x:(i * 100)});
}
6.
Select Control > Test Movie to test the code in Flash Player.
Notice how five movie clips duplicate across the top of the Stage. This ActionScript
duplicates the movie clip symbol in the library and repositions the clips on the Stage at
x
coordinates of 0, 100, 200, 300 and 400 pixels. The loop executes five times, with the
variable
i
assigned a value of 0 through 4. On the last iteration of the loop, the value of
i
increments to 4 and the second expression (
i < 5
) is no longer true, which causes the
loop to exit.
Remember to include a space following each expression in a
for
statement. For more
information, see the
for statement
in the
ActionScript 2.0 Language Reference
.
Using for..in loops
Use the
for..in
statement to loop through (or
iterate
through) the children of a movie clip,
properties of an object, or elements of an array. Children, referenced previously, include other
movie clips, functions, objects, and variables. Common uses of the
for..in
loop include
looping over instances on a timeline or looping over the key/value pairs within an object.
Looping over objects can be an effective way to debug applications because it lets you see what
data returns from web services or external documents such as text or XML files.
For example, you can use a
for...in
loop to iterate through the properties of a generic object
(object properties are not kept in any particular order, so properties appear in an
unpredictable order):
var myObj:Object = {x:20, y:30};
for (var i:String in myObj) {
trace(i + ": " + myObj[i]);
}
This code outputs the following in the Output panel:
x: 20
y: 30
You can also iterate through the elements of an array:
var myArray:Array = ["one", "two", "three"];
for (var i:String in myArray) {
trace(myArray[i]);
}
Содержание FLASH 8-LEARNING ACTIONSCRIPT 2.0 IN FLASH
Страница 1: ...Learning ActionScript 2 0 in Flash...
Страница 8: ...8 Contents...
Страница 18: ...18 Introduction...
Страница 30: ...30 What s New in Flash 8 ActionScript...
Страница 66: ...66 Writing and Editing ActionScript 2 0...
Страница 328: ...328 Interfaces...
Страница 350: ...350 Handling Events...
Страница 590: ...590 Creating Interaction with ActionScript...
Страница 710: ...710 Understanding Security...
Страница 730: ...730 Debugging Applications...
Страница 780: ...780 Deprecated Flash 4 operators...
Страница 830: ...830 Index...