How to dynamically sort display group objects on the run

Hi everyone,

I’ve been looking for an easy way to sort display objects in the same group while the game is still running. So far, I’ve found some answers but those are a bit old so I wanted to know if there is a new and easy way is out there that I can use.

Here is what I’ve found:

https://forums.coronalabs.com/topic/41084-rendering-order-of-groups-children/

There is no API for this so it is very manual I’m afraid.

I do this for my game (in iso) by working out a z value based on the x,y coordinates of the objects.  I then use that to depth sort my layers.

That’s sad to hear since it would’ve been so much easier. Thank you.

If you only have a few items to sort you could try a table.sort() with a custom function to simulate your depth?

Yes. I’ll try something like this or try re-inserting the object if it should be on the front.

depending on your usage, there are some tricks that could potentially be employed IFF you relaxed the “within same group” constraint…

for example, say you had exactly 10 “things” to sort, and for all of which you’ve somehow assigned a “z-index” of 1…10 (ie, whatever value you’d be sorting on otherwise, just make it sequential integers)

so… pre-create 10 display groups in z order then just loop through all objects and insert them into appropriate group, fe

-- pseudocode for i = 1, 10 do local oneThing = tenThings[i] tenGroups[oneThing.z]:insert(oneThing) end

viola! no explicit sorting necessary

Sorry for an answer this late. I had a small break from that project so I forgot to take a look at this topic. I’ll try both of those solutions. Thank you!

There is no API for this so it is very manual I’m afraid.

I do this for my game (in iso) by working out a z value based on the x,y coordinates of the objects.  I then use that to depth sort my layers.

That’s sad to hear since it would’ve been so much easier. Thank you.

If you only have a few items to sort you could try a table.sort() with a custom function to simulate your depth?

Yes. I’ll try something like this or try re-inserting the object if it should be on the front.

depending on your usage, there are some tricks that could potentially be employed IFF you relaxed the “within same group” constraint…

for example, say you had exactly 10 “things” to sort, and for all of which you’ve somehow assigned a “z-index” of 1…10 (ie, whatever value you’d be sorting on otherwise, just make it sequential integers)

so… pre-create 10 display groups in z order then just loop through all objects and insert them into appropriate group, fe

-- pseudocode for i = 1, 10 do local oneThing = tenThings[i] tenGroups[oneThing.z]:insert(oneThing) end

viola! no explicit sorting necessary

Sorry for an answer this late. I had a small break from that project so I forgot to take a look at this topic. I’ll try both of those solutions. Thank you!