Viewers: in countries Watching now:
In ActionScript 3.0 in Flash Professional CS5 Essential Training, Todd Perkins shows Flash designers how to incorporate ActionScript code into their projects and create interactive presentations and applications. The course includes a review of ActionScript language basics and the object-oriented programming (OOP) methodology, a tour of those Flash Professional CS5 features designed for developers, such as code hinting and the Code Snippets panel, and instructions on interacting with objects in the Library and placing code on the Timeline. Exercise files are included with the course.
Sometimes you will want to run the same block of code many times, with little variation. For example, let's say you have a slideshow with a hundred images, and you need to load and place all of the thumbnails. It would be very tedious to copy and paste the loading and placing code 100 times. So to save time, and to be more organized, you can use a loop and write the code only once, and have it repeat itself a hundred times. What we're going to do here is use a loop to place the trashcans on the stage.
I'm going to align the trashcans to the Y position of the second trashcan. If you don't have access to the exercise files, just create nine instances of any movie clip and name them can1_mc, can2 _mc, et cetera, all the way through can9_mc. Let's go to the first keyframe of the actions layer and open the Actions panel. Here I've added all these cans to a trashCans array.
Note that these values are not in quotes, because they are referring to instances on the stage. Now let's take a look at writing a loop to modify the position of each of these objects. Type the word 'for' some parentheses and some curly braces. Notice the parentheses and curly braces are becoming common. We use them for functions, for the if statements, switch/case statements, and for this for loop here. So inside of the parentheses, type var space i, lowercase, colon, uint, u-i-n-t.
This is another data type that starts with a lowercase letter. Set that = 0. By the way, uint is a positive integer, so a whole number, 0 and above. Semicolon after that 0; and i, less than. This is going to run nine times, once for each of the trashcans, a semicolon, and then i++.
Let's walk through what this line of code does. These three different sections of the loop specify different things. The first part is called the iterator variable. This is going to be a variable that we'll use inside of the loop. So we declare the variable, just like any other variable. We call it i for iterator, but it could be anything you want. Declare the datatype as a positive integer and set the value = 0.
That's a starting value for the loop. You can set it to any value you want, but most of the time it's going to be zero. The next part refers to how many times the loop will run. The loop will run as long as i < 9. Finally, you explain what will happen after each time the loop runs. i++ means to increment I by 1 or add 1 to it each time the loop runs. So just to see the loop in action, type 'trace' inside of the loop and in the parentheses for the trace statement, just type i. Make sure not to put it in quotes.
So if you test the movie and take a look in the Output window, you'll see the numbers 0 through 8. Let's go back to the code, and that corresponds to an index of the array, or at least we can tell Flash to make that correspond to an index of the array. So what we'll do is erase the trace statement and place the trashcans. To do that, we'll reference the trashcans through the array.
So type trashCans, index i, which means you put i inside of square brackets, so that's referring to a trashcan each time, and then type .y. That refers to the vertical position of the object. And we'll set that = can2_mc.y; so we're aligning each object to the Y position of can2_mc.
So let's test the movie and see the code in action. Now, all the trashcans are aligned. Remember, if you look at the stage, they're not aligned. So you can use a loop to do that. And the great thing about it is it doesn't matter if we have 9 trashcans or 10,000 trashcans; modifying this code is as simple as modifying that number. Finally, if you really want to optimize this code and have it update based on the size of the array, you can change the 9 value to be the length of the array, or how many items are in the array. So change that to trashCans.length.
Again, that refers to the number of items that are inside of the array, which right now is nine. That way if you ever change the array, you won't have to update the for loop. To test the movie again, show that it's working. Now, if I were to click and drag and erase, let's say almost all of the cans inside of the array, so I only have three cans now, if I test the movie, then only the first three cans will be aligned, so I don't have to update the loop. So this is a pretty a simple example, but you can see how a loop can save you so much time.
Using a for loop, you can perform the same block of code on every object in an array and save loads of time building your applications.
Find answers to the most frequently asked questions about ActionScript 3.0 in Flash Professional CS5 Essential Training .
Here are the FAQs that matched your search "" :
Sorry, there are no matches for your search "" —to search again, type in another word or phrase and click search.
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.