Join Todd Perkins for an in-depth discussion in this video Creating loops, part of Flash CS4 Professional: Object-Oriented Programming .
Now we are going to look at creating of loops using ActionScript 3.0. Let's go to the first keyframe of the Actions layer and open up Actions panel. There are a few different types of loops in ActionScript, and you can find out about all of them using Flash Help. But the most common one you are going to use is a for loop. To create a for loop, type for, and some parentheses and some curly braces and the code inside the curly braces is what's going to be repeated based on what you type in the parenthesis. So in the parenthesis we are going to create a variable called i. This is going to be our loop's iterator. So var i and the data type is typically going to be a positive integer. So uint, it's all lowercase, and by using this data type when I'm going to use only positive whole numbers, I'm optimizing my application.
I am just going to set that equal to 0. I'll type a semicolon. So that's the first part of the loop is you clear your iterator. You don't actually have to create it in this part. I can just put i if I already had a variable called i, and then I can also set it equal to 0 in here if I wanted to. So I have that and then we declare how long our loop is going to run for, how many times the loop is going to run. It's going to run until this condition is false. So as long as i is less than 10. So that's the second part. Type the semicolon and then a space and then we define what's going to happen if the code inside the loop executes and this condition is not true. That conditions being i<10.
We are going to increment i by 1, so type i++ and then we will write the code in the loop. So I'm just going to do a trace statement. trace(); and I'm going to trace the value of i. So I'm going to test the movie. Command+Return on the Mac, Ctrl+Enter on the PC and in the Output window I have 0 through 9. So if I close the Actions panel you could see that a little bit better. I'll scroll to the Output window. So 0, 1, 2, 3, 4, 5, 6, 7, 8, 9. Nice! I return to my Actions panel. And really that's all there is to it. You can create a for loop like this. There is also a while loop and there is a do while loop.
For more information about the while and do while loops, you can look them up in Flash Help. But to create a for loop, use the word for, and in the parentheses you need to declare three things separated by semicolons. First the iterator, which in this case is i. It's a positive integer equal to 0. Second is the condition of the loop as long as i<10 the loop will run, and third what happens if the condition is not met once the block of code runs, and what we are going to do here is increment i by 1.
So this is the typical loop. Really the only thing that usually changes is the number 10 here, and that just changes the number of time that the loop will run. You put all the looping code inside of curly braces and here we are just tracing the value of i.
- Loading external text, images, and XML files
- Creating essential properties for a View class
- Defining a Model class
- Building and implementing interfaces and designs
- Debugging projects
- Building applications with Controller-class buttons
Skill Level Intermediate
1. ActionScript 3.0 Essentials
2. Advanced ActionScript Techniques
3. Object-Oriented Programming Fundamentals
4. The Model View Controller Design Pattern
5. Building a Model Class
6. Building a View Class
7. Building a Controller Class
8. Improving and Debugging the Application
9. Moving Forward
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.Cancel
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.