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!