Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Sometimes you may want to perform an action on each child object in a display object container. Using arrays and for loops, it's a simple process. Let's take these trashcans on Stage here. Notice that they're in a movie clip called container. If I double-click container, I can enter its Timeline. Notice that none of these trashcans have an instance name, but I still want to align them to the zero position, vertically. Let's take a look at how that is possible.
This is a trick that's going to work, whether or not objects have an instance name. Let's select the first keyframe of the actions layer, and open up the Actions panel. I'll start by creating a for Loop. Remember, it's for, and then parentheses, and curly braces. Inside of the for Loop, type var space i, colon uint; - remember that's a positive integer, equals zero, semicolon, space, i, less than, and space.
We'll stop there for just a second. Now here, we want to run a loop for each child inside of the container object. That way it will work whether or not we add or subtract objects inside of the container. So to get the number of children inside the container, we type container.numChildren with a capital C, semicolon and the space, and then i++. The great thing about the numChildren property and using numbers in a loop is that you can actually access each child in a display object container by a number.
Within each display object container, there is layering. Each time a new child is added, it's added in front of the highest child before it. So we have sort of a stack going on. It's just like the layers in Flash. So each time you add a child, it goes on top of the one before it. So, to access the children, type getChildAt. So getChild, with capital C, At with capital A, and then parentheses and then just pass in i in the parentheses.
Since we're looking for children inside of the container, we're going to need to add a container before getChildAt. So container.getChildAt(i);. So this is how we reference all of the children inside the container, through their index numbers. Now after the close parenthesis, you can type any property or method you want, using dot syntax. So dot, y, space, equals, space, zero. So even though these objects don't have instance names, they still exist, and you can still reference them through ActionScript.
Let's see how it works. Test the movie, and all the trashcans or aligned. You'll notice that if I move the container over, double-click it, and Option+Drag or Alt+Drag to create more trashcans, the code will not break. I'll test the movie again, and all the trashcans are aligned. So using the numChildren property inside of a for Loop, and the getChildAt method, you can reference any object, even if it doesn't have an instance name.
Get unlimited access to all courses for just $25/month.Become a member
61 Video lessons · 100136 Viewers
56 Video lessons · 113162 Viewers
71 Video lessons · 82028 Viewers
131 Video lessons · 39359 Viewers
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.