# Stage Center Problem

I’m using createEmptyMovieClip command to create an mc and then drawing a box inside, the problem when I’m trying to center at the Stage, the MC is at 0,0 and is not centered, I was wondering if I can center the mc, calculating the size of the box, to be at 0,0 on the Stage.

``````
Stage.scaleMode = "noScale";
Stage.align = "LT";
stageListener = new Object();
MovieClip.prototype.centerIt = function() {
mShape.onEnterFrame = function() {
var StageX:Number = Math.round(Stage.width/2);
var StageY:Number = Math.round(Stage.height/2);
var speed:Number = 5;
mShape._x += (StageX-mShape._x)/speed;
mShape._y += (StageY-mShape._y)/speed;
if (Math.abs(StageX-mShape._x)<1 && Math.abs(StageY-mShape._y)<1) {
mShape._x = StageX;
mShape._y = stageY;
delete mShape.onEnterFrame;
}
};
};
stageListener.onResize = function() {
mShape.centerIt();
};
stageListener.onResize();

``````

hey, blacksrv,

Drawn movie clips will always be set with a registration point of (0,0). One way to “move” that point is to draw “around” (0,0) (i.e. moveTo and lineTo negative coordinates half the dimensions of the box).

Here’s a quick example:
[AS]Stage.scaleMode = “noScale”;
Stage.align = “TL”;
//
var stageListener:Object = new Object();
var boxID:Number = 0;
var easeSpeed:Number = 5;
//
// create a blue rectangle
var myBox = newBox(200, 150, 0x0000FF);
//
// Stage stuff
function stageResize():Void {
slide(myBox, Math.round(Stage.width/2), Math.round(Stage.height/2));
}
stageListener.onResize = stageResize;
//
stageResize();
//
//
// easing function
function slide(mc:MovieClip, x:Number, y:Number):Void {
mc.onEnterFrame = function() {
mc._x += (x-mc._x)/easeSpeed;
mc._y += (y-mc._y)/easeSpeed;
if (Math.abs(x-mc._x)<1 && Math.abs(y-mc._y)<1) {
mc._x = x;
mc._y = y;
delete mc.onEnterFrame;
}
};
}
//
// draws a rectangular movie clip centered around (0,0)
function newBox(w:Number, h:Number, color:Number):MovieClip {
var box:MovieClip = this.createEmptyMovieClip(“b”+boxID++, this.getNextHighestDepth());
with (box) {
beginFill(color, 100);
moveTo(-(w/2), -(h/2));
lineTo((w/2), -(h/2));
lineTo((w/2), (h/2));
lineTo(-(w/2), (h/2));
lineTo(-(w/2), -(h/2));
endFill();
}
return box;
}[/AS]

thanks for your reply, but I’m to lazy to write all the drawings again :sleep:

this is the code, it’s working now

``````Stage.showMenu = false;
Stage.scaleMode = "noScale";
Stage.align = "LT";
stageListener = new Object();
MovieClip.prototype.centerIt = function() {
mShape.onEnterFrame = function() {
var StageX:Number = Math.round(Stage.width/2);
var StageY:Number = Math.round(Stage.height/2);
var speed:Number = 5;
mShape._x =mShape._x + (StageX - mShape._x - mShape._width / 2)/speed;
mShape._y =mShape._y + (StageY - mShape._y - mShape._height / 2)/speed;
if (Math.abs(StageX-mShape._x)<1 && Math.abs(StageY-mShape._y)<1) {
mShape._x = StageX;
mShape._y = stageY;
delete mShape.onEnterFrame;
}
};
};
stageListener.onResize = function() {
mShape.centerIt();
};