Center a window on Mac

I’m planning to develop a game and am experimenting a bit. The application should be optimized for iPad or other tablets but also run on desktop (Mac, Windows).

I want to have a fixed resolution on the Mac desktop (e.g. 1920 x 1080). On larger screens (e.g. 25") the window should appear in fullscreen mode with a dark background and be centered. Yet it is always in the left upper corner.

For HTML 5 I found a way using the HTML template, but in the Mac version I can’t center the main window.

I’ve tried various options both in the project.xml and via script (window/stage / etc.). I haven’t found anything in old threads either. I would be grateful for a useful hint.

Regards, Christof

If you’re in fullscreen mode, it sounds less like centering the window, and more like centering the content that is inside the window. Because the window will take up the full screen, so it can’t really be re-positioned.

You’re probably going to need to manually adjust the position of things on the display list. I would try to ensure that everything is inside some kind of root Sprite, so that when the Sprite is moved, all content is moved with it. Then, it’s just a matter of centering the Sprite with the assumption that the size of its content will be exactly 1920x1080.

myRootSprite.x = (stage.stageWidth - 1920.0) / 2.0;
myRootSprite.y = (stage.stageHeight - 1080.0) / 2.0;

As a bonus, this should work the same with all targets, so you shouldn’t need to modify your HTML template.

In addition to centering everything when your project starts up, you may want to listen for Event.RESIZE from the stage to ensure that it all remains centered if the stage resizes.

stage.addEventListener(Event.RESIZE, event -> {
    // center all content again
    myRootSprite.x = (stage.stageWidth - 1920.0) / 2.0;
    myRootSprite.y = (stage.stageHeight - 1080.0) / 2.0;
});

Many thanks for the detailed answer.

I was hoping that there would be a solution at the Lime/project.xml level. But I am fine with it. It is actually just for this exceptional case and important for me, because I will develop the game in this configuration.

I’ll try it out this weekend.