Corona SDK Pro Tip of the Day #13
Reference points

Set reference points by strings like 'TopLeft'.

If you remember the previous version of the graphics engine, it used a rather clumsy construction for setting a reference point.


My eyes hurt every time I saw it. So I quickly made a helper function, which didn't require so many characters to type.

setRP(object, 'TopLeft')  

I usually stick this function into my app module app.setRP(...).

Fortunately, when Corona Labs introduced Graphics 2.0 and replaced reference points with .anchorX and .anchorY, it took me ONE minute to adapt my function to the new API, resulting in blazing fast conversion of old projects to the new graphics realities.

Here is how it looks right now for me. I removed everything from app.lua except setRP.

local _M = {}

local referencePoints = {  
    TopLeft      = {0, 0},
    TopRight     = {1, 0},
    TopCenter    = {0.5, 0},
    BottomLeft   = {0, 1},
    BottomRight  = {1, 1},
    BottomCenter = {0.5, 1},
    CenterLeft   = {0, 0.5},
    CenterRight  = {1, 0.5},
    Center       = {0.5, 0.5}
function _M.setRP(object, rp)  
    local anchor = referencePoints[rp]
    if anchor then
        object.anchorX, object.anchorY = anchor[1], anchor[2]
        error('No such reference point: ' .. tostring(rp), 2)

return _M  

Pretty neat, right?

Indie Game Developer