Rendering order of objects in a group

Hi,

I know you can control the rendering order of objects with toFront or toBack and also insert them in a certain position. In our case we have a set of moving objects and we want to render them by their y coordinate values so that the bottom most object is at the front and the upper most object is at the back.

Is there a simple or elegant way of doing this or do we have to manually sort the group’s children ourselves?

Why not just create them in the order that you want?  

If all your objects are in a table you could sort them using the function below and then loop through and insert them.

[lua]

function ySort(t)

   function compare(a,b)

        return a.y < b.y

    end

table.sort(t, compare)

end

ySort(objects)

[/lua]

*double post

#JonPM, the object are constantly moving while we want them to keep being orders according to their current y position not their initial position… Our game has some psedu 3Dness to it and the y coordinate represents the “depth” of the object. we have a ground on which you can walk and you can also walk a bit up and down but it’s actually like your walking in the Z axis… don’t know if it makes sense :slight_smile: but think about games like “Double Dragon”…

Thanks @nick_sherman, we have something like that I was just wondering if I can avoid sorting them each frame… actually we did solve this better by having each object track its peers and sorting itself inside the parent group. 

Why not just create them in the order that you want?  

If all your objects are in a table you could sort them using the function below and then loop through and insert them.

[lua]

function ySort(t)

   function compare(a,b)

        return a.y < b.y

    end

table.sort(t, compare)

end

ySort(objects)

[/lua]

*double post

#JonPM, the object are constantly moving while we want them to keep being orders according to their current y position not their initial position… Our game has some psedu 3Dness to it and the y coordinate represents the “depth” of the object. we have a ground on which you can walk and you can also walk a bit up and down but it’s actually like your walking in the Z axis… don’t know if it makes sense :slight_smile: but think about games like “Double Dragon”…

Thanks @nick_sherman, we have something like that I was just wondering if I can avoid sorting them each frame… actually we did solve this better by having each object track its peers and sorting itself inside the parent group.