Danh mục tài liệu

Nested Loops

Số trang: 10      Loại file: pdf      Dung lượng: 21.37 KB      Lượt xem: 7      Lượt tải: 0    
Xem trước 2 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

Các vòng lặp lồng nhau Loops cung cấp một cách tuyệt vời của tự động hoá một bộ kịch bản nhiệm vụ. Tuy nhiên, các vòng lặp có thể thực hiện nhiều hơn việc thực hiện lặp đi lặp lại của một tập hợp các hành động. Một vòng lặp lồng nhau, có nghĩa là, một vòng lặp đặt bên trong một vòng lặp, có thể hữu ích cho việc tạo một chuỗi looping rằng thực hiện một tập các hành động, thay đổi một chút, thực hiện những hành động tương tự một lần nữa, thay đổi một chút,...
Nội dung trích xuất từ tài liệu:
Nested Loops < Day Day Up >Nested LoopsLoops provide a great way of automating a set of scripting tasks. However, loops canaccomplish more than the repetitive execution of a set of actions. A nested loop—that is,a loop placed inside another loop—can be useful for creating a looping sequence thatexecutes a set of actions, changes a bit, executes those same actions again, changes a bit,and so on. Heres an example of a nested loop:var i:Number = 0;while (++i you understand this concept, think about writing a letter. A letter represents a nested-loopprocess in which you start on line 1 and write perhaps 100 characters, drop to line to 2and write 100 characters, and so on. If your letter is 25 lines long, a script to do the workmight look something like this:var i:Number = 0;while (++i 7.8. var xSpacing:Number = 160;9.10. var ySpacing:Number = 120;11.12. var xStart:Number = 190;13.14. var yStart:Number = 30;15.16. }17. The preceding exercise set up the duplicated list_btn button instances to call this function and pass it a parameter value (pictureID) when clicked. When its finished, this function will copy one of the three pictureID movie clip instances at the top of the stage four times, forming a two-by-two grid, and then send each duplicated instance to a unique frame to display an image. The first line in this function creates a variable called picToDuplicate. This variables value—which is based on the pictureID value of 1, 2, or 3 that was passed to the function when it was called—is then set to picture1_mc, picture2_mc, or picture3_mc, which happen to be the names of the instances containing pictures on the stage. Well use this value later in this function definition to identify which instance to duplicate. The xSpacing variable represents the amount of space to allot between the left sides of the two movie clip instances found on the same horizontal row. The ySpacing variable indicates the space between two movie clips in the same column. The values of these spacing variables are arbitrary and will depend largely on the amount of space you like between movie clips. The next two variables, xStart and yStart, represent the starting position of the first duplicated movie clip in relation to the stage. Any subsequent movie clip duplicates are positioned relative to this point.3. Add this script at the bottom of the itemClicked() function definition:4.5. var v:Number = 0;6.7. var i:Number = -1;8.9. while (++i < 2) {10.11. var j:Number = -1;12.13. while (++j < 2) {14.15. ++v;16.17. var name:String = pictures + v;18.19. _root[picToDuplicate].duplicateMovieClip(name, v);20.21. _root[name]._x = xStart + i * xSpacing;22.23. _root[name]._y = yStart + j * ySpacing;24.25. _root[name].gotoAndStop(v);26.27. }28.29. }30.This loop contains a nested loop—the portion of the function that actually createsthe two-by-two grid of movie clip instances. A single loop would create a singlecolumn of movie clip instances. In contrast, a nested loop creates two instances ina column, alters the script slightly to move the column coordinates, and createsanother two instances in a column (which well explain in a moment). Lets look atthe logic that allows it to work.The outer loop, beginning at line 3 of the script, increments i by 1 (++i), setting aninitial value of 0. The condition of this outer loop says, As long as i is less than 2,execute the actions below. Because 0 < 2, the actions within the loop areexecuted. The first action sets the value of j to -1. Then a nested (inner) loopappears that uses the value of j. First, j is incremented by 1 (++j) and a condition isset that says, As long as j is less than 2, continue looping through the actions thatfollow.The script continues to do nothing but execute the actions in this inner loop untilthat condition becomes false. During the first iteration of this inner loop, the valueof v is incremented by 1 (++v), giving it a value of 1. This variables value is usedseveral times in the lines of script that follow. Using ActionScript that should befamiliar to you by now, the appropriate pictureID movie clip instance is duplicatedand positioned. During the second iteration of this inner loop, the value of j isincremented by 1 (as shown in the loops conditional statement), giving it a valueof 1, which is still less than 2, so the actions within that loop are executed again.This inner loop cannot perform a third iteration because j would be incrementedagain (++j), making it equal to 2 (a condition that exits the inner loop). As a result,the script revisits the outer loop. At that point, i (used by the outer loop) isincremented by 1 (++i), giving it a value of 1, which is still less than 2, so theactions in the outer loop are executed again. As a result, the value of j is reset to -1, and the actions in the inner loop are executed two more times.The concept just described can be tricky; review the logic until you understand itthoroughly.To achieve the effect of creating a two-by-two grid of movie clips (the properspacing), you use this script:_root[name]._x = xStart + i * xSpacing;_root[name]._y = yStart + j * ySpacing;The first line uses the current value of i to set the horizontal spacing. The secondline uses the current value of j to set the vertical spacing when a movie clip isduplicated. Youve already learned that with each outer loop iteration the innerloop performs two iterations. While i has a value of 0, the value of j is set to both 0and 1 during execution of the inner loop, i is incremented to a value of 1, and thevalue of j is set to both 0 and 1. Because we know the values of xStart, xSpacing,yStart, and ySpacing, as well as how the values of i and j are incremented in thelooping process, we ...

Tài liệu được xem nhiều:

Tài liệu có liên quan: