Center MovieClip

Well that was easy, but perhaps you're thinking: So what! Why couldn't I just drag the stupid bee over there and been done with this project?

Let's get a tiny sample of what can happen when you start animating with ActionScript. Let's say the task is to center the bee on the stage horizontally and vertically. You can probably do that (look up the width and height of the stage then divide by two). But what happens if you need to change the dimensions of the stage? You're going to have to move your MovieClip again!

Well, you won't need to move anything again when you're doing things correct with ActionScript. Let's look at a built-in property that ActionScript can look up for us: Stage.width

Stage.width does what it says: displays the width of the stage. If you want to find the horizontal center of your stage, you just have Flash do a little math for you: Stage.width/2 It's that easy! And of course, if you decide to change the width of your movie, Flash will continue to do the math for you!

Let's try to center the bee movieClip horizontally on the stage:

Change the Value of bee._x from 200 to Stage.width/2

Test your work, then get the bee centered vertically! You can probably figure this out on your own, especially if you know that the y property of a movieClip is set using _y and the height of your stage is reference using Stage.height:

OK, test the final project, then play around with the height and width of the stage. Make certain your movieClip is always (automatically) centered and BEE happy: