Convenience function for setting anchor points

I’ve been writing lots of code lately and I find that I keep having to type out

myobject.anchorX, myobject.anchorY = number, number

So I checked the Corona docs to see if there if there was a convenience function for this but didn’t see anything. I decided to create my own and wrote the code below. Theoretically, it should work with all display objects and widgets that support the use of anchor points.

Just slap this code in your main.lua or put it in a file that gets required in main.lua and you should be good to go. To use it, simply create your display object like normal, and then call

myobject:setAnchor(  numberX, numberY )

and the anchorX and anchorY will be set accordingly. Enjoy!

UPDATE: Added functions to grab and release touch focus in touch event handlers.

local widget = require("widget") local libraries = {display, widget} for i, library in pairs(libraries) do for k,v in pairs(library) do if(type(library[k]) == "function" and k:sub(1,3) == "new") then local oldFunction = library[k] library[k] = function(...) local displayObject = oldFunction(...) --Takes either one integer (1-9) as shorthand or two numbers function displayObject:setAnchor(...) assert(..., "Bad call to setAnchor: number expected, got nil") local args = {...} local referencePoints = {{0,0}, {.5,0}, {1,0}, {0,.5}, {.5,.5}, {1,.5}, {0,1}, {.5,1}, {1,1}} local anchorX, anchorY = unpack( #args \> 1 and args or referencePoints[args[1]] ) self.anchorX, self.anchorY = anchorX, anchorY end function displayObject:grabFocus() display.getCurrentStage():setFocus( self ) self.isFocus = true end function displayObject:releaseFocus() display.getCurrentStage():setFocus( nil ) self.isFocus = nil end return displayObject end end end end

P.S. Use at your own risk! I don’t know how future updates to Corona will affect this code. Also I have not extensively tested it with every display object that Corona can create.

I posted a similar function on github a while back:

https://github.com/alanthomson/CoronaSDKReferencePoint2.0/blob/master/refPointConversions.lua

It’s slightly different to yours, as I created it when Corona changed from Graphics 1.0 to Graphics 2.0 and didn’t want to have to change all of my setref calls to anchor properties.

All you have to do is require it, and then you can use the old setReferencePoint functions. I’ve actually tweaked it in my own code since then, so that instead of writing “display.BottomLeftReferencePoint” etc, I can just use a grid position number from 1-9 (where 1 is TL and 9 is BR)

Ah good one! Wish I had found that before. I like your idea of using a single number to indicate the reference point so I included it above. It will take a single argument (1-9) or two arguments (0,0  0,1  etc.). I kept the functionality for passing two numbers because you can set anchor points to any decimal between 0 and 1.

I also added convenience functions to grab/release focus when you’re touching a display object. My fingers were about to fall off having to write this everywhere:

display.getCurrentStage():setFocus( self )
self.isFocus = true

I love how easy it is to extend Lua/Corona functionality!

The problem with this is you don’t own these objects ; Corona do. It is possible, perhaps unlikely but still possible that either they may add their own methods to this namespace. It is also possible that “isFocus” (which I think is your member) may be used in a later implementation of Corona.

It is probably a good idea to program defensively against this - to start with, rather than using isFocus, call it something like __vinces_isFocus which is less likely to collide. Additional to that, when you do the decoration, check that the functions you are decorating aren’t there ; they might not be now, but they might be in the future.

I posted a similar function on github a while back:

https://github.com/alanthomson/CoronaSDKReferencePoint2.0/blob/master/refPointConversions.lua

It’s slightly different to yours, as I created it when Corona changed from Graphics 1.0 to Graphics 2.0 and didn’t want to have to change all of my setref calls to anchor properties.

All you have to do is require it, and then you can use the old setReferencePoint functions. I’ve actually tweaked it in my own code since then, so that instead of writing “display.BottomLeftReferencePoint” etc, I can just use a grid position number from 1-9 (where 1 is TL and 9 is BR)

Ah good one! Wish I had found that before. I like your idea of using a single number to indicate the reference point so I included it above. It will take a single argument (1-9) or two arguments (0,0  0,1  etc.). I kept the functionality for passing two numbers because you can set anchor points to any decimal between 0 and 1.

I also added convenience functions to grab/release focus when you’re touching a display object. My fingers were about to fall off having to write this everywhere:

display.getCurrentStage():setFocus( self )
self.isFocus = true

I love how easy it is to extend Lua/Corona functionality!

The problem with this is you don’t own these objects ; Corona do. It is possible, perhaps unlikely but still possible that either they may add their own methods to this namespace. It is also possible that “isFocus” (which I think is your member) may be used in a later implementation of Corona.

It is probably a good idea to program defensively against this - to start with, rather than using isFocus, call it something like __vinces_isFocus which is less likely to collide. Additional to that, when you do the decoration, check that the functions you are decorating aren’t there ; they might not be now, but they might be in the future.