Skip to main content
Known Participant
February 11, 2010
Answered

stageWidth/2 & stageHeight/2 Not centering?

  • February 11, 2010
  • 2 replies
  • 1171 views

I'm trying to center a preloader into the center of the stage..


It's putting it anywhere BUT the center!

if (stage!=null) {      init(); } addEventListener(Event.ADDED_TO_STAGE, init); function init(event:Event = null):void {      stage.scaleMode=StageScaleMode.NO_SCALE;      stage.align=StageAlign.TOP_LEFT;      stage.addEventListener(Event.RESIZE, resizeHandler);                 setBackground(); } function setBackground() {      //var reg2 = stage.stageWidth / 2;      //var reg1 = stage.stageHeight / 2;      //bg_mc.x = reg2;      bg_mc.width=stage.stageWidth;      bg_mc.height=stage.stageHeight;      bg_mc.scaleX <= bg_mc.scaleY ? (bg_mc.scaleX = bg_mc.scaleY) : (bg_mc.scaleY = bg_mc.scaleX);      preloader.x=(stage.stageWidth-preloader.width)/2;      preloader.y=(stage.stageHeight-preloader.height)/2; } function resizeHandler(event:Event):void {      setBackground(); }

This topic has been closed for replies.
Correct answer kglad

then use:






if (stage!=null) {
     init();
}

addEventListener(Event.ADDED_TO_STAGE, init);

function init(event:Event = null):void {
     stage.scaleMode=StageScaleMode.NO_SCALE;
     stage.align=StageAlign.TOP_LEFT;
     stage.addEventListener(Event.RESIZE, resizeHandler);
          
     setBackground();
}

function setBackground() {
     //var reg2 = stage.stageWidth / 2;
     //var reg1 = stage.stageHeight / 2;
     //bg_mc.x = reg2;

     bg_mc.width=stage.stageWidth;
     bg_mc.height=stage.stageHeight;
     bg_mc.scaleX <= bg_mc.scaleY ? (bg_mc.scaleX = bg_mc.scaleY) : (bg_mc.scaleY = bg_mc.scaleX);


     preloader.x= stage.stageWidth/2;
     preloader.y= stage.stageHeight/2;
}

function resizeHandler(event:Event):void {
     setBackground();
}

2 replies

kglad
Community Expert
kgladCommunity ExpertCorrect answer
Community Expert
February 11, 2010

then use:






if (stage!=null) {
     init();
}

addEventListener(Event.ADDED_TO_STAGE, init);

function init(event:Event = null):void {
     stage.scaleMode=StageScaleMode.NO_SCALE;
     stage.align=StageAlign.TOP_LEFT;
     stage.addEventListener(Event.RESIZE, resizeHandler);
          
     setBackground();
}

function setBackground() {
     //var reg2 = stage.stageWidth / 2;
     //var reg1 = stage.stageHeight / 2;
     //bg_mc.x = reg2;

     bg_mc.width=stage.stageWidth;
     bg_mc.height=stage.stageHeight;
     bg_mc.scaleX <= bg_mc.scaleY ? (bg_mc.scaleX = bg_mc.scaleY) : (bg_mc.scaleY = bg_mc.scaleX);


     preloader.x= stage.stageWidth/2;
     preloader.y= stage.stageHeight/2;
}

function resizeHandler(event:Event):void {
     setBackground();
}

Known Participant
February 12, 2010

Works perfect! Thanks kglad!

kglad
Community Expert
Community Expert
February 12, 2010

you're welcome.

(sorry about all those nestings in the above message.  this forum wasn't working per usual when i was creating that message.)

kglad
Community Expert
Community Expert
February 11, 2010

where's your preloader's registration point?

Known Participant
February 11, 2010

It's in the center of the preloader.