corona
corona copied to clipboard
Add the ability for a display group to sort itself
Hey would it be possible to add a sort()
function to display groups?
Currently I give each display object a calculated z value and do a table.sort
to depth sort the display objects. This requires creating a tmp table for the display objects to be inserted to, sorted and then reinserted into the display group. I imagine doing this in the engine would be much faster than lua. Currently groups with 1k+ objects can take a frame or two to sort.
local function depthSort(group)
--depth sort this
local objects = {}
for i = 1, group.numChildren do
objects[#objects+1] = group[i]
end
table.sort( objects, function(a,b) return a.z < b.z end )
for i = 1, #objects do
group:insert( objects[i] )
end
end
depthSort(group)
It would be great for devs to be able to simply call group:sort()
to perform a low-level depth sort. If no z value is given the object is simply ignored so this would not affect any current code.
Is there any chance to keep them sorted on every new insert?
If this is your only option I can try to implement it next week, it shouldn't take more than a couple of hours .
In the end I went to a sorted insert system. I now binary search the display group to decide where to insert new objects. It was a pain but in the end much faster than resorting the entire group in lua.