# Stage Center Problem

**URL:** https://forum.kirupa.com/t/stage-center-problem/184595
**Category:** flash
**Created:** [April 6, 2006, 4:49am UTC](https://forum.kirupa.com/t/stage-center-problem/184595 "2006-04-06T04:49:19Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![blacksrv](https://avatars.discourse-cdn.com/v4/letter/b/bbce88/32.png) [@blacksrv](https://forum.kirupa.com/u/blacksrv)
#### Post date: [April 6, 2006, 4:49am UTC](https://forum.kirupa.com/t/stage-center-problem/184595/1 "2006-04-06T04:49:19Z")

</div>

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.

```auto
 
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 += (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();
};
Stage.addListener(stageListener);
stageListener.onResize();

```

Thanks in advance.

---

<div class="post-metadata">

### Author: ![devonair](https://avatars.discourse-cdn.com/v4/letter/d/3be4f8/32.png) [@devonair](https://forum.kirupa.com/u/devonair)
#### Post date: [April 6, 2006, 7:22am UTC](https://forum.kirupa.com/t/stage-center-problem/184595/2 "2006-04-06T07:22:33Z")

</div>

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;  
Stage.addListener(stageListener);  
//  
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]

---

<div class="post-metadata">

### Author: ![blacksrv](https://avatars.discourse-cdn.com/v4/letter/b/bbce88/32.png) [@blacksrv](https://forum.kirupa.com/u/blacksrv)
#### Post date: [April 6, 2006, 5:41pm UTC](https://forum.kirupa.com/t/stage-center-problem/184595/3 "2006-04-06T17:41:19Z")

</div>

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

this is the code, it’s working now

```auto
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();
};
Stage.addListener(stageListener);
stageListener.onResize();

```
