Centering an ActionScript Object

For food I code ActionScript3 – and I have to admit, I kind of like it – not flash it self, but you can do very unwebbish things to archive excellent looks. Flash for design, xhtml for content and function – if you ask me.
Today I wanted to center an SWF in a browser – regardless of window-size. Since I did not find an adequate solution (which of course is obvious and simple) here’s mine – just for the the book:

...
// I want everything to stay exactly as I drew it
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
// register event handler
stage.addEventListener(Event.RESIZE, resizeHandler);
// I don't want to repeat the code in the initialisation, so I trigger the event manually
dispatchEvent(new Event(Event.RESIZE));
}

private function resizeHandler(e:Event):void
{
this.x = stage.stageWidth/2-this.width/2;
this.y = stage.stageHeight/2-this.height/2;
}

PS: I need a syntax highlighting plugin for wordpress.

This entry was posted in Uncategorized and tagged , . Bookmark the permalink.

Leave a Reply