NOTE: This tutorial is outdated and has been replaced by the Transforms and Anchors guide and Extending Anchor Points tutorial.

    • If you’re using the normal ones (TopLeft, Center, CenterRight, etc.), then Anchor points will actually save you quite a bit of code and time. obj.anchorX = 0; obj.anchorY = 0 is less than obj:setReferencePoint(display.TopLeftReferecenePoint). Secondly, they can be set as defaults, so if you want to left, center align a bunch of display.newText() objects, you can set the default, create the fields, and then set the default back to center when done.

      Now if you are using variable reference points using .xReference and .yReference, it’s probably a bit of a trade off. I found it a bit faster to determine the anchor point (92 pixels in / 412 total width) rather than having to do 412 / 2 – 92 and remembering to make it negative. But either works.

      • What about adding % option to the list? Like using “anchorX = 50%” to centre objects. It will be extending “V2 anchorX” exactly like “V1 center reference point”, that way we wont have to calculate x and y of objects.

      • no, anchorX and anchorY isn’t at all thought through. Having them in ratios is bad, because corona uses ratios nowhere else. It’s not what you get from corona, so you will always have to calculate them.

        this would be acceptable, but I really don’t get why corona limits the ratios to be >0 and <1.

  1. Following up on my original post, would it be possible to include a function that would allow the “old” style using pixel values, and have it automatically convert? Something like:


    Having these functions automatically do the division by total width and height and setting both anchor points? This might be related to my very personal workflow, but the alternative seems to be lots and lots of painstaking manual measurements in photoshop and mindnumbing divisions.

    • You can easily write an helper function that accepts and object and x, y coordinates and converts to 0.0-1.0 values:

      local min, max = math.min, math.max
      local function setAnchorCoordinates( object, xReference, yReference )
      object.anchorX, object.anchorY = min( 1, max( 0, xReference / object.width ) ), min( 1, max( 0, yReference / object.height ) )

      • I found this idea very useful, but it does have a bug. You need to add .5 to the values generated because xReference or yReference assume that 0 is the center of the object (unless they are in a group it seems).

        function setAnchorCoordinates( object, xReference, yReference )
        object.anchorX = math.min( 1, math.max( 0, (xReference / object.width)+.5) )
        print(“x”,object.anchorX,” width “, object.width)
        object.anchorY = math.min( 1, math.max( 0, (yReference / object.height)+.5 ) )
        print(“y”,object.anchorY,” width “, object.height)

  2. Hey Rob. Great tutorial – any word on Physics? I’ve been having problems with it in my game – something about hybrid mode not obeying the anchor points…


    • Yes, xReference and yReference are not available in the G2.0 engine, even in v1 mode, as we could not simulate them properly in the G2.0 engine

      • Hmmm… That explains why my graphics were all screwed up, even in compatibility mode. Just checking: I am still able to finish my (big) current project and build for iOS 7 using the current (non-g2.0) builds, right?

  3. Krishna Raj Salim says:

    Hi, this is not working with my corona version: 2013.1202 (2013.8.28), even the sample code doe not seem to work. I still get the object rotation with respects to it’s visual centre point .

    • Hi Krishna. This is a Graphics 2.0 feature which is only available to Pro and Enterprise subscribers and is only in daily builds numbered greater than 2000.

  4. Hello there,

    I’ve been testing the new anchor points but I seem to be running into some kind of issue when it’s used with texturepacker sprites.

    Basically when I set the anchorX of my image to 0 (was previously display.CenterLeftReferencePoint), it loses it “hitbox” and doesn’t work with eventListener attached. Essentially it’s just

    myImage.anchorX = 0

    It doesn’t register the touch event unless I click on the very left side of the image.

    I’ve tried this with a regular display.newRect and it works perfectly for the rectangle however it’s not working with display.newSprite with sprites from texturepacker.

    Hope you understand what I meant.


  5. Hello,
    I used negative yReference in my project for rotating gauge pointer.
    But I cant migrate it to Graphics 2.0.
    Because anchorX and anchorY doesnot support negative value.
    I think it have to support negative values.
    And also it have to support number range more then 0 to 1.

    Thanks :)

  6. Nice tutorial, but I have this huge confusion! The object is rotating at an anchor point os obviously its x, y coordinates are never changing, what If I had to determine collision of an object with this hand which is continuously rotating? I mean how do I get points along the length of its hands with its changing position, anyone, please?

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>