My approach is to use a standard 16:9 for my game’s main canvas. This allows me to get a consistent visual output for the core gameplay.
What I do is to use some simple math to calculate the difference on x and y axis between my hardcoded 16:9 canvas and the actual screen dimensions. I use the difference to add to the coordinates of UI elements that are along the edges of the screen so that they can be pushed outward depending on the device without any special code being required.
Of course, for background images, i use a safely large size which will cover all aspect ratios.
For computing the difference in my coordinate bounds and those of the actual device, I have written a crude but effective script which has global variables that I can summon anywhere in the code to get the x and y difference values.
I’ll add that script here once I’m back on my pc. Hope it can help you or anyone else
UPDATE:
So, after checking, I use letterbox scaling which I find is easiest to maintain consistency in gameplay but this really depends on the kind of game-- you may feel quite comfortable lettering Solar2d scale things dynamically but that just doesn’t work for us. This is how I set things up in config.lua:
content =
{
width = 750,
height = 1334,
fps = 60,
shaderPrecision = "lowp",
scale = "letterbox",
},
I have also attached the screenCompensation script. It should automatically compute X and Y differences relative to your fixed letterbox and will also account for whether your app is set to run in portrait or landscape mode. It will automatically update the values whenever the screen is resized-- I think we had done this because the status bar can appear/disappear dynamically on Android so the values will need to be updated.
All that is needed is for the getXCompensation() and getYCompensation() functions to be called wherever you need to apply the difference to any display object.
screenCompensation.lua (4.4 KB)