
Nested Loops
Thông tin tài liệu:
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ìm kiếm theo từ khóa liên quan:
máy tính mạng máy tính internet phần mềm ứng dụng lập trình SQL HTML sever web XMLTài liệu có liên quan:
-
Giáo án Tin học lớp 9 (Trọn bộ cả năm)
149 trang 295 0 0 -
Giáo trình Hệ thống mạng máy tính CCNA (Tập 4): Phần 2
102 trang 295 0 0 -
Ngân hàng câu hỏi trắc nghiệm môn mạng máy tính
99 trang 278 1 0 -
Bài giảng: Lịch sử phát triển hệ thống mạng
118 trang 278 0 0 -
47 trang 250 4 0
-
Đề cương chi tiết học phần Thiết kế và cài đặt mạng
3 trang 246 0 0 -
80 trang 238 0 0
-
6 trang 227 0 0
-
Giáo trình môn học/mô đun: Mạng máy tính (Ngành/nghề: Quản trị mạng máy tính) - Phần 1
68 trang 225 0 0 -
Giáo trình Hệ thống mạng máy tính CCNA (Tập 4): Phần 1
122 trang 222 0 0 -
122 trang 222 0 0
-
Giáo trình môn học Mạng máy tính - Nghề: Quản trị mạng máy tính - Trình độ: Cao đẳng nghề (Phần 2)
39 trang 215 0 0 -
Giáo trình căn bản về mạng máy tính -Lê Đình Danh 2
23 trang 214 0 0 -
6 trang 210 0 0
-
Bài giảng Lý thuyết mạng máy tính: Chương 3 - ThS. Lương Minh Huấn
73 trang 197 0 0 -
Đề cương chi tiết học phần: Mạng máy tính và lập trình mạng
4 trang 192 0 0 -
Chuyên đề mạng máy tính: Tìm hiểu và Cài đặt Group Policy trên windows sever 2008
18 trang 190 0 0 -
139 trang 181 0 0
-
Giáo trình Mạng máy tính (Nghề: Tin học ứng dụng - Trung cấp) - Trường Cao đẳng Cộng đồng Đồng Tháp
189 trang 174 0 0 -
Giáo trình Ứng dụng công nghiệp thông tin cơ bản: Phần 1
73 trang 172 0 0