Center The Movie Clips

Let's center each of those MovieClips on the stage right after we attach them. Remember we have named each instance using "main"+n. You can reference the _x property of each clip using _root["main"+n]._x but you will have to repeatedly type that same string _root["main"+n] every time you set a new property (like _y or _rotation or _alpha)

Instead, we will turn that MovieClip reference into a simple variable called line (again, I made up that variable name. It has nothing to do with any built-in functions). Then we can just use line every time we want to modify a property.

Variables can hold many different types of data.

  1. Select the attachMovie action first - we want the new actions to run right after this script
  2. Click Statements > Variables > var
  3. Type line = this["main"+n] in the Variables box
  4. Select MovieClip from the Type drop down list

Let's get these MovieClips in the center of the stage. You can figure this out yourself by checking the width of your stage and dividing by 2 to set the _x position, then checking the height and dividing by 2 to set the _y position. But if you change the dimensions of your movie later, you are going to have to redo these actions! No way!

Let's use Stage.width and Stage.height to have Flash lookup the width and height for you.

To center your MovieClips horizontally on the stage you use line. _x = Stage.width/2;
To center your MovieClips vertically you use line. _y = Stage.height/2;

Start by setting the _x position of line:

Repeat set variable to center line vertically:

Ok, test your project. All the clips should be stacked right on top of each other in the center of the stage: