Center Adobe AIR Window

I always end up digging around in past projects for this snippet, so I’m putting it here for safe keeping, and maybe it’ll help you out too.

public static function centerWindow(win:WindowedApplication):void
{
   var screenBounds:Rectangle = Screen.mainScreen.bounds;
   win.nativeWindow.x = Math.floor((screenBounds.width - win.width)/2);
   win.nativeWindow.y = Math.floor((screenBounds.height - win.height)/2);
}

The real trick here is that you have to call this on creationComplete instead of one of the other events that seems like it might work like applicationComplete. A callLater() couldn’t hurt either. ;)

This entry was posted in Flash. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

2 Comments

  1. Dan
    Posted June 4, 2009 at 9:24 am | Permalink

    Thanks for the tip, just what I was looking for! :)

  2. Posted May 26, 2011 at 4:29 am | Permalink

    I have a creationComplete init method

    tried passing this as the paramater but it doesnt work, what do I pass?

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>