Tutorial: Extending anchor points

In Corona, all display objects have an anchor point. Essentially, this can be imagined as a “pin” point within the object’s bounds around which all transforms occur — x/y positioning, rotation, and scaling. For example, if the anchor point is defined at the top-left corner of an object, rotation of the object will cause it to rotate around that point.

History and background

In legacy versions of Corona, this concept was known as the reference point and it was handled by two deprecated approaches. The first was to use the object:setReferencePoint() API which accepted 9 possible constants as means to set the point at either the center of the object or at specific points around the object’s edges. For example, you could specify display.TopLeftReferencePoint to “pin” the object at its top-left corner, or display.TopCenterReferencePoint for the horizontal center point along the object’s top edge.

Alternatively, you could define a more fine-tuned reference point by setting the xReference or yReference properties of the object. This allowed developers to set the reference point to any point within the object’s bounds, or even to a theoretical point outside the object’s bounds by setting values beyond its edges.

Current method

In modern versions of Corona, both of the above methods have been replaced by anchor points. However, instead of setting a constant or content area value, anchor points are typically defined as decimal values between 0.0 and 1.0, where 0.0 represents the top or left edge, 0.5 represents the center, and 1.0 represents the bottom or right edge. As an example, if you wanted to mimic the legacy display.TopLeftReferencePoint, you could set both anchor points to 0.0 (or simply 0):

1

2

3

object.anchorX=0

object.anchorY=0

In this way, anchor points can achieve what the previous methods could — however, one thing that was initially “eliminated” with the introduction of anchor points was the ability to set the point outside of the object’s bounds, previously achieved by setting the object’s xReference or yReference properties to values beyond its edges.

In specific, the default behavior of anchor points is that they are limited to the range between 0.0 and 1.0, so values set outside this range will be clamped to the respective limit.

Extending anchor points

To re-introduce the concept of non-clamped anchor points, our engineers have added a new "isAnchorClamped" option for display.setDefault(), available in Daily Build 2015.2733 and later. When set to false, you can define anchor points outside of the object’s bounds:

1

2

display.setDefault("isAnchorClamped",false)

Now, anchor points set to values outside the 0–1 range will be honored:

1

2

3

4

local rect=display.newRect(200,240,50,50)

rect.anchorX=-1

rect.anchorY=1.5

transition.to(rect,rotation=135,delay=500)

Example usage

One example where this approach is useful is rotating an object around another object, like a planet orbiting its star. If the anchor point is clamped to points inside or at the object’s edge, the object can only rotate in a limited perspective. Now, with "isAnchorClamped" set to false, you can easily make the planet orbit the star:

1

2

3

4

5

6

7

8

9

10

11

display.setDefault("isAnchorClamped",false)

local sun=display.newCircle(display.contentCenterX,display.contentCenterY,24)

sun:setFillColor(1.0,0.78,0)

local earth=display.newCircle(display.contentCenterX,display.contentCenterY,6)

earth:setFillColor(0,0.5,0.7)

earth.anchorX=-10.0

transition.to(earth,time=8000,rotation=-360,iterations=0)

Conclusion

With this new setting, you now have access to the full power that reference points provided in legacy versions of Corona, including points both inside and outside of the object’s bounds. Remember, however, that this feature is only available in Daily Build 2015.2733 or later.

Disclaimer: SDKNews.com only syndicates the blog entries from various SDK blogs.
We are not the creator/author of these entries (posts). Product names, brand names
and company names mentioned on this site may be trademarks of their respective owners.