It looks like you're new here. If you want to get involved, click one of these buttons!
function Sprite:CompareDepthTo(cmp) if cmp == self then return 0 end p1 = self:getParent() p2 = cmp:getParent() if p1 == cmp then --cmp is parent of self return 1 elseif self == p2 then --self is parent of cmp return -1 elseif p1 == p2 and p1 ~= nil then -- the same root return p1:getChildIndex(self) > p1:getChildIndex(cmp) and 1 or -1 else --complicated relationship local p1 = self local p2 = cmp local pp1 = {} local pp2 = {} while p1 ~= nil do pp1[#pp1 + 1] = p1 p1 = p1:getParent() end while p2 ~= nil do pp2[#pp2 + 1] = p2 p2 = p2:getParent() end local m = math.min(#pp2, #pp1) local index = 0 for i = 0, m - 1 do index = i if pp2[#pp2 - i] ~= pp1[#pp1 - i] then break end end if index == 0 then -- not the same root return nil elseif index == m - 1 then -- one of them are parent/root of other, for ex: [19 17 15 4 2 1] <-> [15 4 2 1] return #pp1 > #pp2 and 1 or -1 else local common_p = pp1[#pp1 - index + 1] local child1 = pp1[#pp1 - index] local child2 = pp2[#pp2 - index] return common_p:getChildIndex(child1) > common_p:getChildIndex(child2) and 1 or -1 end end end |
Likes: anneMurielle
Comments
https://github.com/gideros/gideros
https://www.youtube.com/c/JohnBlackburn1975
I also think about "label" sprites by hierarchy index. I'll post when finish
----
Edit: Oop,"label" idea failed because of real-tine adding/removing
Likes: luyimoon
What are the hashes(#) in the code for?
e.g.
pp2[#pp2 + 1] = p2
Sorting sprite children... and the solution of using a .depth variable and passing a sort function : Source
See if it helps, you never get too many options.
eg
a=["fox","goose"] -- keys are 1 and 2
print (#a) -- prints 2
non numerical entries are not accounted for
a.farm=true
print (#a) -- still prints 2
https://github.com/gideros/gideros
https://www.youtube.com/c/JohnBlackburn1975
Thanks john26.
I'm having display hierarchy(z-order) issues in my code at the moment I think. I might have to also think about not having certain sprites tied in with others, as in tying them to the stage instead.