sorting display groups?

hello,

it _seems_ that the order that you add objects into display groups is the order in which they are drawn to screen… but there isn’t any way of controlling their position in the drawing order other than with toFront or toBack…

so can you table sort a display group to change the compositing order?

for example create 3 imageRects and assign them an arbitrary property to sort with - say ‘z’ 

if i then change the ‘z’ value can i re-sort the display group and have it affect the compositing order?

something like:

[lua]

local function compare( a, b )
return a.z < b.z 
end

local group=display.newGroup()

local p1 = display.newImageRect(group,…)

p1.z=5

local p2 = display.newImageRect(group,…)

p2.z=15

local p2 = display.newImageRect(group,…)

p2.z=25

table.sort(  group , compare )

[/lua]

cheers

andy

Hi Andy,

This submission into the Corona Code Exchange is focused on children with a display group, but I imagine it could be easily adapted to just treat display groups as children of a parent display group.

http://code.coronalabs.com/code/depth-sortable-display-group

Best regards,

Brent

that actually does what i’m after, thanks Brent!

looking at the code, it achieves it by copying all the groups objects to a temporary table, sorting that and then inserting them back into 

the display group. while this is very cool (thanks rakoonic!) it seems that a more optimal approach would be a native sort function on the display group… i guess i’ll put in a feature request :slight_smile:

cheers!

andy

Hi Andy,

This submission into the Corona Code Exchange is focused on children with a display group, but I imagine it could be easily adapted to just treat display groups as children of a parent display group.

http://code.coronalabs.com/code/depth-sortable-display-group

Best regards,

Brent

that actually does what i’m after, thanks Brent!

looking at the code, it achieves it by copying all the groups objects to a temporary table, sorting that and then inserting them back into 

the display group. while this is very cool (thanks rakoonic!) it seems that a more optimal approach would be a native sort function on the display group… i guess i’ll put in a feature request :slight_smile:

cheers!

andy