Tutorial: Moderizing the config.lua question

Hi Lori,

When I work with “letterbox” scale (and I almost always do since it’s my personal preference), I find that setting two variables early on in “main.lua” is extremely valuable for positioning objects that should be “anchored to one side”. These variables are really simple calculations, honestly, and they will be dynamically calculated no matter what screen or device you’re testing on (assuming it’s “letterbox” scale setting).

Basically, they tell you the width and height of the letterbox bars (empty space) on any device:

[lua]

local letterboxWidth = (display.actualContentWidth-display.contentWidth)/2

local letterboxHeight = (display.actualContentHeight-display.contentHeight)/2

[/lua]

Using these, you can simply add or subtract the value from the position of any object which needs to be pushed up against one side of the screen. For the left, you’d subtract “letterboxWidth”, or for the right side, add “letterboxWidth”. Same concept with “letterboxHeight” and the top/bottom of the screen.

If you need to use these in multiple scenes of a Composer-based app, I suggest that you set them to Composer variables that will be known throughout the app, using the “.setVariable()” and “.getVariable()” APIs:

https://docs.coronalabs.com/api/library/composer/setVariable.html

https://docs.coronalabs.com/api/library/composer/getVariable.html

Hope this helps,

Brent

Great!  Thanks for the help.  I’ll give it a try. :slight_smile:

To anchor to the top or left, simply use the number of pixels.   Object.x = 50

Then on every device, it will be 50 pixels from the left edge.  Object.y = 50

will keep it 50 pixels from the top edge.  Object.y = display.contentHeight - 50

will keep it 50 pixels from the bottom edge.

Rob

Awesome!   Thanks for all the help.

Lori

I’m not sure why this is happening.  But I am using some overlay pop ups.  I’m  centering them on the screen and I was trying to use the same method of using display.contentCenterX and display.contentCenterY in placing the object on the overlay (buttons, headings etc).  It appears to work fine on the iPhones but the centering is off on the iPad’s.   Since everything is working from the center I am perplexed - it seems like it should work the same.

Any thoughts or ideas would be appreciated.

Thanks,

Lori