xReference, yReference problems

Hello, thanks for reading my issue.

I’m currently trying to get an object to rotate around an external point. Much like the moon rotating around the Earth, I’d like the same side to constantly face the point around which it rotates.

I’ve currently been trying to use object.xReference and object.yReference to accomplish this, but I can’t seem to get either one to function in any noticeable way. Even when I copy and paste the demo code from the API, all I can see when running it is a single square (which is actually the two squares on the exact same point):

local rect1 = display.newRect(0, 0, 50, 50)
rect1:setFillColor(170, 170, 170)
rect1.x = 100
rect1.y = 100

local rect2 = display.newRect(0, 0, 50, 50)
rect2:setFillColor(255, 255, 255)

rect2.xReference = 50
rect2.x = 100
rect2.y = 100

 I’m using Runtime:addEventListener to run an enterFrame function that constantly increments the rotation of my object, and currently I can get it to spin in place. No matter, though, where I set the reference point, all it will do is spin in place. Here’s the snippet of my code that isn’t working:

    local background = display.newRect( -44, -44, screenW+88, screenH+88 )

    background:setFillColor( 0.3, 0.4, 1 )

    local globe = display.newCircle( halfW, 800, 600 )

    globe:setFillColor( 0, .3, 0 )

    local rotRect = display.newRect( 50, 50, 40, 40 )

    rotRect:setFillColor( 1, 0, 0 )

    rotRect.xReference = 200

    rotRect.yReference = 200

    group:insert( background )

    group:insert( globe )

    group:insert( rotRect )

    local function rectSpin(e)

        rotRect.rotation = rotRect.rotation + 2

    end

    Runtime:addEventListener( “enterFrame”, rectSpin )

I’ve tried moving the .xReference and .yReference to different spots in the code to see if it was some sort of scoping issue, but still nothing… 

Any thoughts would be greatly appreciated!

Thank you!

Unless you are using an old version, xReference and yReference are deprecated and no longer available.  You should now be using anchorX and anchorY: http://docs.coronalabs.com/daily/api/type/DisplayObject/anchorX.html

Thanks for the response! If my understanding of anchorX and anchorY is correct, aren’t they used to reference an internal spot on an object? The API says it ranges from 0 to 1, 0 being the left or top and 1 being right right or bottom.

As I understand it, using the sun and the earth as an example, wouldn’t earth.anchorX=0.5, earth.anchorY=0.5 be the center of the earth, the point the earth rotates around? I’m trying to figure out how to get the earth to revolve around the center of the sun: sun.anchorX = 0.5, sun.anchorY=0.5. Theoretically the earth should be able to rotate around its own central anchor while simultaneously revolving around the sun’s central anchor.

Is there some part of anchoring that I’m not understanding?

Thanks again, I really appreciate the help! :slight_smile:

Yes I guess you will need to work around that.

Maybe you can make a group and in the group have an invisible rect larger than the target object, then put the anchor on the group. You might need anchorChildren on the group.

Also found this one that might help, its using a container: http://forums.coronalabs.com/topic/42041-migration-xyreference-anchorxy-not-interchangeable/

Thank you sir, this definitely sets me on the right path :slight_smile:

I really appreciate it!

Just to add a little more… An object can’t have 2 points to rotate around.  Jon’s suggestion is the right way.

Rob

Unless you are using an old version, xReference and yReference are deprecated and no longer available.  You should now be using anchorX and anchorY: http://docs.coronalabs.com/daily/api/type/DisplayObject/anchorX.html

Thanks for the response! If my understanding of anchorX and anchorY is correct, aren’t they used to reference an internal spot on an object? The API says it ranges from 0 to 1, 0 being the left or top and 1 being right right or bottom.

As I understand it, using the sun and the earth as an example, wouldn’t earth.anchorX=0.5, earth.anchorY=0.5 be the center of the earth, the point the earth rotates around? I’m trying to figure out how to get the earth to revolve around the center of the sun: sun.anchorX = 0.5, sun.anchorY=0.5. Theoretically the earth should be able to rotate around its own central anchor while simultaneously revolving around the sun’s central anchor.

Is there some part of anchoring that I’m not understanding?

Thanks again, I really appreciate the help! :slight_smile:

Yes I guess you will need to work around that.

Maybe you can make a group and in the group have an invisible rect larger than the target object, then put the anchor on the group. You might need anchorChildren on the group.

Also found this one that might help, its using a container: http://forums.coronalabs.com/topic/42041-migration-xyreference-anchorxy-not-interchangeable/

Thank you sir, this definitely sets me on the right path :slight_smile:

I really appreciate it!

Just to add a little more… An object can’t have 2 points to rotate around.  Jon’s suggestion is the right way.

Rob