Let say I have an object located at top left corner. How do I rotate this object around center coordinate? I assume that I need to set the anchorX and anchorY to the center coordinate. But, I don’t think it will work because anchorX and anchorY only need the value from 0 to 1.
The default value for anchorX and anchorY are 0.5, the center of the object. So unless you’ve changed the anchor, by default it’s going to rotate around the center.
Rob
While anchorX/Y need values from 0-1, those values cover the entire object. They’re kind of like percentages, so .75 would be 75% from the top (anchorY) or left (anchorX) edge. 1 would be 100%, or all the way from the top or left edge, .2 would be 20% from the top or left edge, etc.
Using anchorX/Y you’re able to set the point to *any* location on an object, as opposed to just 9 locations with the old setReferencePoint() system.
Jay
Thank you for the explanation guys. Now I understand about anchorX and anchorY. Appreciate it. :)
Now, I have another question regarding this. Based on the picture above, I would like to rotate the rectangle object around the center of the screen. How can I achieve this? Thank you.
Not a fan of moties, I guess …
A simpler way is to stick it in a group. By default, groups rotate around (0,0) so if you put your square at -200,-100 or something, it will rotate around the offset 0,0
grp = display.newGroup() grp.x, grp.y = 160,240 square = display.newRect(grp,-80,-140,60,60) transition.to(grp,{ time = 10000, rotation = 3600 })
Great engineers.
I like the parent display group option, too. Get’s the system to do the work for you.
I found the solution for this by using xReference and yReference
Apparently, xReference and yReference is deprecated in Graphic 2.0 if I’m not mistaken. Is there other way to replace this in Graphic 2.0?
xReference and yReference are not available in Graphics 2.0.
Did you try paulscottrobson’s suggestion? It’s a good one.
For other folks that might need it, I adapted some code from another post in the old developer portal for rotating objects:
EDIT: cleaned up the code
local r = 80 local numPoints = 4 local xCenter = 200 local yCenter = 200 local radius = 50 local ffR = .5 local angleStep = 2 \* math.pi / numPoints local t = {} local circle = {} for i = 1, numPoints do circle[i] = display.newCircle(0, 0, 10) circle[i].x = xCenter + radius\*math.cos(i\*angleStep) circle[i].y = yCenter + radius\*math.sin(i\*angleStep) group1:insert( circle[i] ) end local function onUpdate( ) ffR = ffR+.5 for i = 1, numPoints do t[i] = (ffR \* .014)\*i circle[i].x = (r \* math.cos(t[i]))+50 circle[i].y = (r \* math.sin(t[i]))+200 end end Runtime:addEventListener( "enterFrame", onUpdate )
The default value for anchorX and anchorY are 0.5, the center of the object. So unless you’ve changed the anchor, by default it’s going to rotate around the center.
Rob
While anchorX/Y need values from 0-1, those values cover the entire object. They’re kind of like percentages, so .75 would be 75% from the top (anchorY) or left (anchorX) edge. 1 would be 100%, or all the way from the top or left edge, .2 would be 20% from the top or left edge, etc.
Using anchorX/Y you’re able to set the point to *any* location on an object, as opposed to just 9 locations with the old setReferencePoint() system.
Jay
Thank you for the explanation guys. Now I understand about anchorX and anchorY. Appreciate it. :)
Now, I have another question regarding this. Based on the picture above, I would like to rotate the rectangle object around the center of the screen. How can I achieve this? Thank you.
Not a fan of moties, I guess …
A simpler way is to stick it in a group. By default, groups rotate around (0,0) so if you put your square at -200,-100 or something, it will rotate around the offset 0,0
grp = display.newGroup() grp.x, grp.y = 160,240 square = display.newRect(grp,-80,-140,60,60) transition.to(grp,{ time = 10000, rotation = 3600 })
Great engineers.
I like the parent display group option, too. Get’s the system to do the work for you.
I found the solution for this by using xReference and yReference
Apparently, xReference and yReference is deprecated in Graphic 2.0 if I’m not mistaken. Is there other way to replace this in Graphic 2.0?
xReference and yReference are not available in Graphics 2.0.
Did you try paulscottrobson’s suggestion? It’s a good one.