Hi all
I am having some trouble ordering sprites within a display group.
I want to order them so the enemy’s closer to the bottom of the screen are rendered last.
line 27 prints out the order correctly i.e.
1 1 - 0
2 2 - 20
3 3 - 40
4 5 - 80
5 4 - 140
but if I uncomment line 28 --enemyHolder[k]:toFront()
I get outputs like this
1 4 - 0
2 5 - 20
3 2 - 40
4 1 - 80
5 3 - 140
I am new to lua and corona so I’m not sure if I am going about this in the correct way.
any help with this would be very much appreciated
thanks
Mac
here is a code example
[lua]display.setStatusBar(display.HiddenStatusBar)
local enemyArray = {}
local enemyHolder = display.newGroup();
function sortDepth()
local depthList = {}
for i = 1,enemyHolder.numChildren do
depthList[i] = enemyHolder[i].y
end
list = {}
for name,value in pairs(depthList) do
list[#list+1] = name
end
function byval(a,b)
return depthList[a] < depthList[b]
end
table.sort(list,byval)
for k=1,#list do
print (k…" "…list[k] … " - " … depthList[list[k]])
–enemyHolder[k]:toFront()
end
print(’ ')
end
function touched(event)
local t = event.target
local dp = function()
sortDepth()
end
if event.phase == “ended” and t.hit == false then
transition.to(t, {time = 200, y = t.y+20, onComplete = dp})
end
end
function createEnemy()
local _touched = function(event)
touched(event)
end
local _n = display.newGroup()
local img = display.newImage(“enemy.png”)
_n.hit = false;
_n.speed = math.random(300,500);
_n.x = (enemyHolder.numChildren * 20)
_n.y = (enemyHolder.numChildren * 20)
_n:insert(img)
_n:addEventListener( “touch”, _touched )
enemyHolder:insert(_n);
end
for i=1,5 do createEnemy() end[/lua] [import]uid: 12378 topic_id: 5304 reply_id: 305304[/import]
