center object based on browser size

Center MovieClip based on browser size

Monday, October 19th, 2009 | AS3 Gadgets | No Comments

Put the code below on the first frame of your flash movie. Add a movieclip to stage and give it the instance name “myMovie”. This AS3 code will dynamically center your movieclip based on browser size.

?View Code ACTIONSCRIPT
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
 
function resizeHandler(e:Event):void
{
  myMovie.x = (myMovie.stage.stageWidth / 2) - (myMovie.width / 2);
  myMovie.y = (myMovie.stage.stageHeight / 2) - (myMovie.height / 2);
}
 
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.addEventListener(Event.RESIZE, resizeHandler);
 
stage.dispatchEvent(new Event(Event.RESIZE));
//the end

Tags: , , ,