Position Helper for Corona SDK Graphics 2.0
A module to position display objects on different screen resolutions. This helper will only work with the newest version of Corona (aka 2.0). Anchors and screen orientations are internally managed to provide consistency.
Recent changes:
- Storyboard replaced with Composer API
- Refreshed build.settings with portrait orientation as a default
- Removed all print() logs
To position you just call the function and pass the displayObject
magnet:topLeft( displayObject )
If you need to have a margin of 10px from the left this is how you do it:
magnet:topLeft( displayObject, 10 )
or same as before but with 20 px from the top:
magnet:topLeft( displayObject, 10, 20)
If you want to reposition after a screen rotation all you need to do is call the following function on the orientation event:
magnet:updateCurrentOrientation()
magnet:top(obj, marginY)
magnet:right(obj, marginX)
magnet:bottom(obj, marginY)
magnet:left(obj, marginX)
magnet:center(obj, marginX, marginY)
magnet:topLeft(obj, marginX, marginY)
magnet:topRight(obj, marginX, marginY)
magnet:bottomLeft(obj, marginX, marginY)
magnet:bottomRight(obj, marginX, marginY)
magnet:topCenter(obj, marginX, marginY)
magnet:centerRight(obj, marginX, marginY)
magnet:bottomCenter(obj, marginX, marginY)
magnet:centerLeft(obj, marginX, marginY)
Or a generic call with the align as a parameter:
magnet:alignTo(sAlign, obj, ...)
where sAlign is the method name in a string like:
magnet("bottomRight", myObject, xMargin, yMargin)
magnet:getPercentX( percent )
magnet:getPercentY( percent )
When positioning display objects inside snapshots you'll need to provide an additional parameter "parent" as the current snapshot that contains it. Otherwise it won't work!
local snapshot = display.newSnapshot(width, height)
local circle = display.newCircle(snapshot.group, 0, 0, 10)
magnet:center(circle, 0, 0, snapshot) --add the parent