How to align a movie clip to the centre of the stage in AS3

Written by sue smith Google
  • Share
  • Tweet
  • Share
  • Pin
  • Email
How to align a movie clip to the centre of the stage in AS3
With ActionScript 3.0, you can create dynamic animated effects. (Comstock/Comstock/Getty Images)

In ActionScript 3.0, you can manipulate Flash movie objects. If you have a Flash document with a movie clip inside it, it requires a minimal amount of ActionScript 3.0 code to position the object on the stage. The ActionScript 3.0 language stage class provides the data you need to calculate the point at the centre of the visible stage. You can use this point to move your movie clip to the stage centre, with a little adjustment required depending on how your clip symbol is structured.

Skill level:
Moderately Easy

Other People Are Reading


  1. 1

    Open Flash and create a movie clip. You can work with an existing Flash document and movie clip if you have one available. Otherwise, create a new document in Flash and new symbol. Draw a shape on the stage, select it and press "F8" or select "Modify" and "Convert to Symbol." Enter a name of your choice and select "Movie Clip" as the type. You can set the registration point if you wish, but by default it will be at the top left corner of your shape. Click "OK" to create your new object.

  2. 2

    Make your clip accessible to ActionScript. Select the movie clip on the stage. In the Properties panel, give it a name, for example "myClip_mc" with the standard "_mc" suffix. Using the "_mc" suffix can help when working in Flash or other coding environments in which you can be prompted with relevant code hints.

  3. 3

    Create ActionScript code for your movie. Add a layer in your file for ActionScript code if you do not already have one. Click the "Insert Layer" button in the timeline and alter the layer name to "actions" or another name of your choice. Open the layer for coding by selecting it and opening the Actions panel. You can type your code in the editor that appears.

  4. 4

    Move your movie clip. Enter the following code to refer to your clip's X position property, altering it if you chose a different name: myClip_mc._x

    Now assign it a position representing the centre of the stage, by referring to the Stage ActionScript class: myClip_mc._x=Stage.width/2;

    By dividing the stage width by two, you calculate the central point on the X axis. This would work for a movie clip with a central registration point. If you used the default top-left registration point, you need to extend the code as follows, taking the symbol width into account: myClip_mc._x=Stage.width/2-myClip_mc._width/2;

    Do the same for the Y property: myClip_mc._y=Stage.height/2-myClip_mc._height/2;

    This time you use the Stage height property. Test your movie. The movie clip should appear in the centre of the visible stage area.

Tips and warnings

  • Using the Stage object is more effective than "hard-coding" the dimensions as the stage area may change in size.
  • If you calculate the dimensions of Flash movie objects lots of times in a single script your movie may become inefficient.

Don't Miss

  • All types
  • Articles
  • Slideshows
  • Videos
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the site, you consent to the use of cookies. For more information, please see our Cookie policy.