depth/rendering order

hi there

I have a quick question.
I have a number of enemy sprites which are moving around the screen randomly. they have all been inserted into a group. Is there any way to order the sprites within the group so the sprites with the greatest y position are rendered last. Is there are swap depth function??

I’ve had a quick look on the API’s but haven’t seen anything.

I was thinking this maybe a way to do it. On every frame remove all the sprites from the enemy group and the reinsert them based on there y position. The only way I could figure out how to do this would be to create a depth list of all the enemy’s. Check each enemy’s y position against all the remaining enemy’s. If it has the lowest y value then insert it into the group and remove it from the list and loop through it until the depth list empty. it seems very wasteful and could be very slow. I haven’t coded anything up yet I’m a bit unsure on where to start.

any advice with this would to greatly appreciated. [import]uid: 12378 topic_id: 5122 reply_id: 305122[/import]

http://developer.anscamobile.com/reference/index/objecttofront
http://developer.anscamobile.com/reference/index/objecttoback [import]uid: 12108 topic_id: 5122 reply_id: 16941[/import]

thanks again jhocking can’t believe I missed that :slight_smile: [import]uid: 12378 topic_id: 5122 reply_id: 16942[/import]

Hi all

I am try to implement the tofront to order my enemy’s
it prints the enemy’s in order of there y value but when I
call the “:toFront()” the sprites depths change inconsistanly.

I must be doing something wrong but I’m not sure what.

here is my code

Thanks

[lua] function self:sortDepth()

local depthList = {}
for i = 1,self.enemy.numChildren do
depthList[i] = self.enemy[i].y

end

sortList = {}

for name,value in pairs(depthList) do
sortList[#sortList+1] = name
end

function byval(a,b)
return depthList[a] < depthList[b]
end

table.sort(sortList,byval)

for k=1,#sortList do
print (sortList[k]…’ '…depthList[sortList[k]])
self.enemy[sortList[k]]:toFront()
end

end[/lua] [import]uid: 12378 topic_id: 5122 reply_id: 17137[/import]

dunno sorry, can’t run that code while I’m at work

pretty clever approach though, it wouldn’t have occurred to me to use table.sort [import]uid: 12108 topic_id: 5122 reply_id: 17618[/import]