Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Placing objects on the Stage is an essential skill when working with AS3 in Flash Professional. Sometimes, however, you will want to move an object into a different container once you have already placed it in one. For example, here I'm creating a trashcan from the library and placing it on the Stage. I'm adding it as a child object of the Stage. But what if I started creating more of these trashcans? For example, I could copy and paste this code, change the name of can, to can1 and change the position of the can to 677 pixels.
So I test the movie, and I have two cans. What if I'm going to have many cans, like a hundred cans? It might make sense to start placing them inside of a container. Let's take a look at how to do that. Above all this code, I'm going to create a new variable. I'm going to call this 'container.' I'm going to datatype it to a sprite. Now a sprite might be a new concept to you, but it's actually, fairly simple. Think of a sprite as a movie clip with only one frame. That's all there is to it.
It's a movie clip, but it takes up less memory in the Flash Player. It's also ActionScript only. So, I'm going to create a new sprite here. So container, datatype it to a sprite. Space, equals, space, new, space, Sprite, with a capital S. Some open and close parenthesis and a semicolon. So that creates a new, empty sprite, which is basically like an empty movie clip. So, now what I'll do is I will place the container on the Stage. So I'll use the addChild method.
Remember that if you forget to use the addChild method, you'll never see your object on the Stage. Creating a new instance of an object simply creates it in memory. addChild is what allows you to see the object, so you need to put it in some sort of Timeline. So, I'm going to add the container to the Stage, and then I'm going to set this container's x and y equal to 577 for x, and container.y = 493.
Notice that they are the same coordinates that I used for the original trashcan. Now I'm going to delete the coordinates for the original trashcan, and I'm going to delete the coordinates for can1. Now let's test the movie. So I can't see the sprite container, but it's in this area over here. It's invisible because it's an empty container, with nothing inside of it. I do see the trashcans at the left of the Stage. Now, if I want to put the trashcans inside of the container, all I have to do is have the container be the one that's adding the child, instead of the main Timeline.
So, right before addChild(can), I can type container.addChild(can). So I can do that in both instances. For the second can, which is can1, I'll set its x-position to 100, can1.x =100. Remember these coordinates are going to be relative to the container object. So a 0,0 position will start at the container's x and y position. So test the movie, and there are the trashcans.
The thing that's great about this is when I move the container, I can also move the trashcans. So at the bottom of my code, let's say I want to move the container 100 pixels to the left, I could type container.x -=. This is a shorthand way of subtracting a value from its current position, = 100. So, it's going to take the position of the container and shift it 100 pixels to the left. So test the movie, and you can see the container is all the way over there.
For a more dramatic effect, I'll put in 500. The trashcans are over there. So this is an easy way to stay organized because I can move all of my trashcans at the same pace, because they're in the same container. Now there is one more thing I want to look at. You can actually reparent a child object if you've already added it to the Stage. So let's take this code, container.addChild (can1), and copy and paste it a few lines down. I'm going to temporarily disable the line of code that I pasted by pressing two forward slashes at the beginning of the line.
So, it'll turn gray, and Flash will not process that code. I'm going to erase container.before addChild, and then we'll test the movie. So can1 is 100 pixels from the left edge of the Stage, and it is now a child of the main Timeline, unlike the original can that's a child of the container movie clip, which you can tell because it's at the bottom-right of the Stage. Now if I uncomment this line of code and Flash processes it, you'll see that the can goes from being a child of the main Timeline to being a child of the container.
So test the movie, and now the second can is a child of the container. Notice that we don't see it move from the one place to another because all this code is processed before the frame is drawn. So if you ever need to, you can reparent a child object to another parent by simply calling the new parent's addChild method and passing in the new child object. So to recap, the addChild method allows you to place a display object, or any other visual object, inside of a display object container.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 101622 Viewers
61 Video lessons · 88377 Viewers
71 Video lessons · 72218 Viewers
56 Video lessons · 103969 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.
Your file was successfully uploaded.