Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat
Compare sprites depth (z-index) - Gideros Forum

#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

# Compare sprites depth (z-index)

Member
edited March 2013
I try to compare sprites depth (z-index) by using the following code
 ```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```
return :
+ (1): means self stays above
+ (-1): self stays below
+ (0): compare self itself
+ (nil): unknown, there's no relationship

I'm afraid of its performance, any better solution?

P/S: I also attach sample code.

Likes: anneMurielle

+1 -1 (+1 / -0 ) Share on Facebook

• Maintainer
Your solution looks good. But it z-depth is so important why not put everything in a flat hierarchy? You could make every sprite a child of stage and then just compare index numbers.
• Member
edited March 2013
@john26: I thought about it, but get problems in find put a way to number all sprites, especially real-time adding/removing child.

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
• Member
n00b question:

What are the hashes(#) in the code for?

e.g.
pp2[#pp2 + 1] = p2
• Member
The # gets the length of a table.
• Guru
edited March 2013
Just throwing an idea, did you check this post :
Sorting sprite children... and the solution of using a .depth variable and passing a sort function :
You can pass in a sort function, so you should be able to do something like:
 ```-- descending order table.sort(objects, function(a,b) return a.depth > b.depth end)```
Source

See if it helps, you never get too many options.
twitter@TheWindApps Artful applications : The Wind Forest. #art #japan #apps
• Maintainer
@loucsam. The # operator returns the number of entries in the table with numerical keys

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
• Member
@loucsam. The # operator returns the number of entries in the table with numerical keys

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.