I have looked around the forums since the Graphics 2.0 public release, and haven’t found an answer to this issue. In reading the documentation, it seemed like setting anchorChildren = true would make a GroupObject have the behaviors of a normal displayObject for all intents and purposes. This seems to generally be true, except for when it comes to “localToContent”. With a true displayObject, localToContent will take a point relative to the displayObject’s center regardless of its anchor points and returns where that point lies on that stage. With a GroupObject, the input is relative to the group’s origin, which makes sense. However, when you set anchorChildren = true on a GroupObject, I would expect the input to localToContent to be relative to the center of the composite set of it’s children, but it is not. It remains as whatever the origin of the GroupObject still is (which has no relation at all to the anchor points on the group, and is now hidden when anchorChildren = true). In the code snippet below, I would expect the green dot (representing the localToContent output of the input (0,0)) to be in the center of the screen rather than on the upper left bounds of the square)
[lua]
–Corona version: 2013.2100
local displayGroup = display.newGroup()
local groupRect = display.newRect(0,0,100,100)
groupRect.anchorX = 0
groupRect.anchorY = 0
displayGroup:insert(groupRect)
displayGroup.anchorChildren = true
displayGroup.x = display.contentCenterX
displayGroup.y = display.contentCenterY
local originInContentX, originInContentY = displayGroup:localToContent(0,0)
–I would expect this to show up in the center of the screen!
local originCircle = display.newCircle(originInContentX, originInContentY, 5)
originCircle:setFillColor(0,1,0)
–these are just two lines intersecting at the center for display purposes
local vLine = display.newLine(display.contentCenterX,0,display.contentCenterX, display.viewableContentHeight)
vLine:setStrokeColor(1,0,0)
vLine.strokeWidth = 5
local hLine = display.newLine(0,display.contentCenterY,display.viewableContentWidth, display.contentCenterY)
hLine:setStrokeColor(1,0,0)
hLine.strokeWidth = 5
[/lua]
This may seem like a fringe issue, but it comes into play, for example, if you are trying to change the anchor point on a displayObject (which could be a group with anchorChildren = true) without having it visibly move. I can do this with a normal displayObject with the help of localToContent (I can locate the local position of the future anchor point in content space, store it, update the anchor point, then change the (x,y) coordinates of the displayObject using contentToLocal in the displayObject’s parent with the stored coordinates). This is seemingly impossible with “anchored” groups because there is no way to determine the local coordinate of an anchorPoint because the anchor points are not related to the origin of the group. The only way I can find to have this behavior is to guarantee that every group’s origin is also the center of the composite set of it’s children when it is created, which is not desirable. I also realize that their might be a solution using contentBounds(), but I believe this fails if there is any transformations on the group, or if the group is nested in other groups with transformations (I know that other forum answers have suggested that nesting groups within groups and/or using anchorChildren should be avoided unless absolutely necessary, but that use case seems super common (and super useful)). On that note, it would be awesome if there was the ability to get the coordinates of a local point on an object in the coordinate space of any of its nested parent stack (and vice versa).
Thanks in advance, and I apologize for this question being so long.
P.S. Absolutely lovin’ Corona since the Graphics 2.0 update, keep up the great work!