i am trying to get things running with anchorChidren, but it seems to be very buggy to me. has anyone come up with a neat solution to this problem yet?
We have a library module “anchorGroups”. It takes care of all the repositioning that happens when you create groups automatically. You can also use it to change the position of the anchor group on the fly without the group jumping around visibly.
The main problem with anchorChildren is when items within the group are moving in their own orbit and increasing the bounds of the group. What is required then is to create a null shape that is as big as the group ever gets when the item inside adjusts its position.
Anyway, this works great. You require it as anchorGroups:
local anchor = require(“anchorGroups”)
And apply it with this to a group you have created, lets say groupName:
anchor.new(groupName, 1, 1) – Bottom right.
--====================================================================-- -- PROCESS GROUPS --====================================================================-- local M = {} -- Anchor Group M.new = function (obj, anchorX, anchorY) local obj = obj obj.name = obj.name or "nameless" --print("Group obj = ", obj.name) local anchorX, anchorY = anchorX, anchorY 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 ) -- 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 if obj.width \>0 and obj.height \>0 then -- isn't an empty group if not obj.anchorChildren then -- IS a non anchored group -- CALCULATE the Center of the group, add to key obj.xOrig, obj.yOrig local bounds = obj.contentBounds obj.xOrig = (bounds.xMax - bounds.xMin) / 2 + bounds.xMin obj.yOrig = (bounds.yMax - bounds.yMin) / 2 + bounds.yMin --print(obj.name.." xOrig, yOrig = ", obj.xOrig, obj.yOrig) --print(obj.name.." xMin, xMax = ", bounds.xMin, bounds.xMax) --print(obj.name.." Width, height = ", bounds.xMax-bounds.xMin, bounds.yMax-bounds.yMin) -- Set group to this position obj.x, obj.y = obj.xOrig, obj.yOrig -- Set .anchorChildren = true to slam group origin into center obj.anchorChildren = true end end end -- Set the anchor if passed in or default to center if obj.width \>0 and obj.height \>0 then -- isn't an empty group obj.x = obj.x + offsetX obj.y = obj.y + offsetY obj.anchorX, obj.anchorY = anchorNewX, anchorNewY --print("imgMode = ", imgMode) --print(obj.name, " anchors = ", anchorNewX, anchorNewY) else --print (obj.name, " = an empty group") end end return M
hi kilopop
thanks for the class. i gave it a try yesterday but it did not really help to solve my problems. but it was a quick run, so i hope to be wrong :).
right now i am using a group for the character, and inside i change sprite animations. to make it work i had to use display.newContainer instead of display.newGroup. positioning inside the group is fine now, but the parent group itself gets missplaced during gameplay (and corrected withing a few ms). an anchorX and anchorY is set on the parent group, which stays the same for all the level (so not changing any anchors etc on the parent group). pretty confusing.
let me try your class again!