As recently established in the forum and confirmed by Corona staff, groups containing other groups with anchorChildren = true produces unpredictable results. The upshot is nested groups and layers with different anchor points no longer work in G2 whereas they did in G1… Is this a bug or is there another name for a feature that used to be possible but no no longer is?
Hopefully this is a temporary situation. But we can’t wait around until it is fixed, so have started writing a function to implement nested groups that can have different anchor points.
The following code uses the example of a few shapes to make a vehicle. It is made up of a group with 3 other groups inserted, each of which can have its own anchor position.
So far, the code is nearly there… It can take an object where ever it is on the screen and put the anchor where you provide to the anchorGroup function with object name and the new anchors x and y e.g.
anchorGroup(box, 1,1)
The trick is not to use anchorChildren = true which is where the crazy factor enters the equation with unpredictable results. Instead, images and groups are sent by their center point to 0,0, anchors added and then sent back to its original position via contentBounds.
Problem is that once groups are inserted into groups, changing the anchor to anything other than 0.5, 0.5 does provide an offset effect which leads to unpredictable effects. G1 also allowed referencePoints to be changed on an object without that object changing position which is possible on the first run of this function on an object but not if you call it again…
This is where some help would be fantastic. Hope it’s all clear and someone is up for the challenge of bringing back some of the awesomeness of setReference…
local gp\_carBody = display.newGroup() local gp\_wheel1 = display.newGroup() local gp\_wheel2 = display.newGroup() local gp\_car = display.newGroup() -- Anchor Group local function anchorGroup (obj, anchorX, anchorY) local obj = obj local anchorNewX = anchorX or 0.5 local anchorNewY = anchorY or 0.5 local anchorOldX = obj.anchorX or 0.5 local anchorOldY = obj.anchorY or 0.5 local offsetX = obj.width \* ( anchorNewX - anchorOldX ) local offsetY = obj.height \* ( anchorNewY - anchorOldY ) local centerX = obj.x local centerY = obj.y -- Determine whether obj is a single image (numChildren = nil) or a group that has already been prepped for anchor (hasAnchor) if obj.numChildren ~= nil then -- NOT an image, IS a group. obj.hasAnchor = true -- CALCULATE the max and min x and y of group's children's bounding area. local bounds = obj.contentBounds centerX = (bounds.xMax - bounds.xMin) / 2 + bounds.xMin centerY = (bounds.yMax - bounds.yMin) / 2 + bounds.yMin print("center x and y = ", centerX, centerY) -- Move the group's children so that their relationship to the center of the group is at 0 local objX, objY local i for i = 1, obj.numChildren do objX, objY = obj[i]:localToContent(0,0) obj[i].x = objX - centerX - offsetX obj[i].y = objY - centerY - offsetY end -- Now the Group contents are properly organised around the anchor at x and y = 0 end -- Set the anchor if passed in or default to center --print("anchors = ", anchorOldX, anchorOldY, anchorNewX, anchorNewY) obj.anchorX, obj.anchorY = anchorNewX, anchorNewY obj.x = centerX + offsetX obj.y = centerY + offsetY print("object x and y = ", obj.x, obj.y) print("object width and height = ", obj.width, obj.height) print("--------") end -- body positioning local body = display.newRect( 1472, 922, 748, 228 ) body:setFillColor( 255/255, 255/255, 255/255 ) -- top positioning local top = display.newRect( 1454, 758, 276, 260 ) top:setFillColor( 255/255, 255/255, 255/255 ) -- tyre1 positioning local tyre1 = display.newCircle( 1265, 1041, 75 ) tyre1:setFillColor( 3/255, 145/255, 222/255 ) -- cap1 positioning local cap1 = display.newCircle( 1265, 1041, 53 ) cap1:setFillColor( 255/255, 255/255, 255/255 ) -- tyre2 positioning local tyre2 = display.newCircle( 1689, 1041, 75 ) tyre2:setFillColor( 3/255, 145/255, 222/255 ) -- cap2 positioning local cap2 = display.newCircle( 1689, 1041, 53 ) cap2:setFillColor( 255/255, 255/255, 255/255 ) -- Organise Groups gp\_carBody:insert(body) gp\_carBody:insert(top) anchorGroup(gp\_carBody) gp\_wheel1:insert(tyre1) gp\_wheel1:insert(cap1) anchorGroup(gp\_wheel1, 0,0) gp\_wheel1.xScale = 0.5 gp\_wheel2:insert(tyre2) gp\_wheel2:insert(cap2) anchorGroup(gp\_wheel2) gp\_wheel2.xScale = 0.5 gp\_car:insert(gp\_carBody) gp\_car:insert(gp\_wheel1) gp\_car:insert(gp\_wheel2) anchorGroup(gp\_car) -- animation transition.to (gp\_car, { time = 4000, x = -200 } ) transition.to (gp\_wheel1, { time = 500, rotation = -360, iterations=8} ) transition.to (gp\_wheel2, { time = 500, rotation = -360, iterations=8} ) transition.to (gp\_carBody, { time = 250, y = -200, iterations=16, transition=easing.continuousLoop } )