=================================
=================================
=================================
출처: http://www.republicofcode.com/tutorials/flash/as3loops/
Basic Loop Usage
If it wasn't for loops you would have to repeat a statement multiple times if you want to have an action executed more than once. For example, say that you want to create ten MovieClips and add them to the display list. The long way for doing this would have been to type the command ten times:
addChild(new MovieClip());
addChild(new MovieClip());
addChild(new MovieClip());
addChild(new MovieClip());
addChild(new MovieClip());
addChild(new MovieClip());
addChild(new MovieClip());
addChild(new MovieClip());
addChild(new MovieClip());
However, you can do the same exact thing using a loop without having to type the same code multiple times:
addChild(new MovieClip());
}
This second code generates the same exact result, but in a much more compact way. Using a loop also makes it less likely for you to have typing errors because you type the code once and then it is automatically repeated.
There are five different loop types, we are going to explain how to create each of them in turn.
The "For" Loop
The for loop is most widely used loop format because of its very compact form and because it is the easiest one to understand and use. You can use use this loop to execute a number of code statements multiple times using this format:
statements;
}
Each for loop must have a counter (also called an iterator). This counter can be any number variable. You can define this counter when you create the loop:
statements;
}
Or alternatively you can define the counter before hand and just reset it when you want to use it in the loop:
for (i=0; condition, action){
statements;
}
The second element in the loop is a condition. The loop will only run while that condition is satisfied. For example, if you want your loop to run ten times you will need to state that i must not be greater than ten:
statements;
}
Finally, you have to specify the action by which i will increase, decrease, or multiply. In most simple cases you would have this value increase by one. This command is executed after each loop cycle.
statements;
}
Your loop has now been defined and you can put any code inside it to have play repeatedly.
new MovieClip();
}
It is very common to make use of the i counter inside the loop. As the value of i will increase by 1, it is usually helpful to have each of your objects have a unique name by the value of i in its name. For example, if we wanted to have our MovieClips have their names in a series the series of mc1, mc2, mc3, etc. We would do that by using i as part of the name this way:
var my_mc = new MovieClip();
my_mc.name = "mc"+i;
addChild(my_mc);
}
The code above will create ten MovieClips, name them in the series of mc1, mc2, mc3, etc, and add each of them to the display list.
That is basically everything you need to know to start using the for loop. You might want to check our other tutorials to see how it used in actual projects.
The "While" Loop
The while loop has the same exact function as the for loop, but it is written in a different format. Instead of having all the information necessary to run the loop in a single place, the while loop only requires the condition to be provided to the loop itself and leaves the rest for the programmed to supply in the surrounding code.
statements;
}
In order to use this loop you must create your own counter and then create your own action to invalidate the condition later. The code which we have created earlier can be ported in this loop format this way:
while (i < 10){
new MovieClip();
i++;
}
You should note that it is much easier to create an infinite loop by mistake using a while conditional as you might forget to provide the action required to increase the value of the counter. You should always use the for loop for your basic usage instead of attempting to use the while loop if your program does not require it specifically.
The "Do While" Loop
A do while loop is very similar to a while loop, the only difference is that a do while will execute the statements once before attempting to check if the condition is satisfied or not. This means that whether or not your condition was satisfied you will always have the statements executed at least once. The general format of this loop is as follows:
statements;
} while (condition);
Again, usage of this loop is required in advanced programs and will not be used in simple beginner applications. The example we had before could be ported into this loop this way:
do {
new MovieClip();
i++;
} while (i < 10);
For Each In Loop
This loop and the next one are not regular loops and do not serve the same function as those illustrated earlier. The purpose of the for each in loop is not to repeat any general set of code, but to examine an object to retrieve the value of all properties associated with that object. It is written in the generalized format shown below:
trace(prop);
}
For example, assume that you have a generic object that has two properties called myName and myAge.
myObject.myName = "John Doe";
myObject.myAge = 30;
You can use the for each in loop to retrieve the values of these properties easily:
myObject.myName = "John Doe";
myObject.myAge = 30;
for each (var prop in myObject){
trace(prop);
}
This example might not make a lot of sense because we know the names of our properties and we could have retrieved them manually, but this loop can be helpful when you are working with an object for which you do not know the properties.
For In Loop
The for in loop has a very similar function to the for in each loop, while the previous loop is used to retrieve the values of all the properties of an object, the for in loop is used to retrieve the names of the properties. It is written in the following generalized format:
trace(prop);
}
If we use the same example above with this loop we will retrieve the names of the properties as opposed to their values:
myObject.myName = "John Doe";
myObject.myAge = 30;
for (var prop in myObject){
trace(prop);
}
As previously stated, the for each in and for in loop are advanced loops that will be rarely used in beginner and intermediate projects, do not worry about them if you do not fully understand how they work.
Using Variables in Loops
An important thing to learn about loops is that defining a variable inside one makes that variable local, meaning that the variable and its value will disappear after each loop iteration. If a variable was defined inside a loop, you will get an error if you try to retrieve that variable from outside the loop because that variable does not exist outside it.
var k:Number = i;
}
trace(k);
If you want to manipulate a variable that you would like to retrieve and access after the loop, you will have to define this variable outside the loop and then simply update its value from within the loop:
for (var i:Number = 0; i<10; i++){
k = i;
}
trace(k);
However, you should remember that the value of k, or any variable you set directly through a loop will equal that value at the end of the loop and it will not refer to any value previously set. If you are going to create a number of objects from inside a loop, you will need to use advanced referencing techniques or arrays to be able to refer to these individual objects later on.
This concludes our tutorial, I hope that you learned something new from it. Feel free to post any questions or comments you have at the Republic of Code Forum.
- End of Tutorial
=================================
=================================
=================================
'ADOBE > ActionScript' 카테고리의 다른 글
Java에서 쓰는 데이터형(map, array)는 AS3.0에서 어떻게 쓰이나? (0) | 2011.11.14 |
---|---|
플래시 ActionScript3.0 Array를 사용한 HashMap (0) | 2011.11.14 |
플래쉬 소수점 처리 (0) | 2011.11.14 |
[AS] 플래시 삼성스마트 TV 앱 올리기 에뮬레이터 OR TV (스마트허브에 올리기) (0) | 2011.11.11 |
플래시 삼성 스마트TV 앱에서의 메모리 최적화 (0) | 2011.11.10 |