Z-order of graphic objects

Is there a way to set a z-order of the graphic objects drawn to the screen?

I’m running into a problem where when I draw “sprite A” to the screen at lets say y=250 and “sprite B” at y=200, when sprite B moves on the x axis it looks like it’s walking on top of sprite A’s head.

Thanks. [import]uid: 8446 topic_id: 3501 reply_id: 303501[/import]

reinsert A in to the scene/group. it’s in the docs!

object.parent:insert( object )

or

myDisplayGroup:insert( object )

to set a specific z-index you’ll have to implement your own sorting algorithm i guess [import]uid: 6645 topic_id: 3501 reply_id: 10525[/import]

Thanks jmp909, I must’ve overlooked the docs.

Is it performant to reinsert objects whenever a new object is created? I have about 20 sprites at any given time.
Also, what happens to the objects that are already drawn to the screen when a reinsert occurs? Do they temporarily disappear?

Thanks! [import]uid: 8446 topic_id: 3501 reply_id: 10529[/import]

The drawing comes after your code had run in a frame. [import]uid: 5712 topic_id: 3501 reply_id: 10559[/import]

re-inserting an object that is already on screen just takes it out of its current z-Index and places it on top of everything else

[import]uid: 6645 topic_id: 3501 reply_id: 10562[/import]

Thanks both for the input.

This is what’s happening:

  • 4 sprites are drawn to the screen (z-index works great ), which can be represented as:
    [sprite1][z-index=1]
    [sprite2][z-index=2]
    [sprite3][z-index=3]
    [sprite4][z-index=4]

  • Then when other sprites are created, the order becomes as follows and the problem begins:
    [sprite5][z-index=5] | [sprite1][z-index=1]
    [sprite6][z-index=6] | [sprite2][z-index=2]
    [sprite7][z-index=7] | [sprite3][z-index=3]
    [sprite8][z-index=8] | [sprite4][z-index=4]

Reinserting the elements to the group works great when there are always a certain number of elements on the screen, but in my case where objects are drawn dynamically it gets a bit tricky and I don’t really have a clue on how to fix this, so any ideas would be kindly appreciated.

Thanks in advance. [import]uid: 8446 topic_id: 3501 reply_id: 10564[/import]

you’ll need to write a sorting function that re-orders the whole list (by re-inserting everything)

try here:
http://developer.anscamobile.com/forum/2010/10/09/initializing-groups-layers
[import]uid: 6645 topic_id: 3501 reply_id: 10572[/import]

Thanks jmp909, that sorting technique did the trick!
[import]uid: 8446 topic_id: 3501 reply_id: 10649[/import]