I need to center an object that is inside another object on a screen and for this to be relative to the position of the second object
For example:
-------- screen --------
| |
| -------- |
| | text | |
| -------- |
------------------------
Code example to center text on a screen in Lua
:
local x = math.floor(screenWidth / 2 - width / 2)
local y = math.floor(screenHeight / 2 - height / 2)
CodePudding user response:
Just center the object relative to your parent, then move it to its absolute position on the screen:
local x = parentX math.floor((parentWidth - width) / 2)
local y = parentY math.floor((parentHeight - height) / 2)
since Lua 5.3, you may use floor division (//
) to shorten this:
local x = parentX (parentWidth - width) // 2
local y = parentY (parentHeight - height) // 2