diffictul situation of ordering objects in a group

Hello,

My situation is difficult to describe so take a look at this diagram first…

vr4wgy.png

As you can see the players are standing in a column but there is no order between them. I want the bottom ones to come to front and as it goes up it go to the back of the guy in the front. All the players are assigned to a display group.

Anyone please? I really need to solve it :frowning:

show some code and we can better help

I don’t know if I’m understanding your problem correctly, but I’ll give it a shot.

object:toFront() will move a display object to the front of its display group, so basically… iterate through the players in whatever order you want them, and call object:toFront() on each.

Like memo said, you need to sort the group based on Y coordinates.

I would suggest you create a sorting function, and the sorting function creates a new group, and then populates it with the new objects in descending order of Y coordinate.  Then insert the new group back into the main group, and delete the old one.

Also Do NOT use display:remove() or object:removeSelf() on the objects, only use :insert() into the new group. Then remove the old group, and insert the new one in your main display group.  When you remove() an object, the sdk nerfs it (used to anyways), and it could behave erratically later.

@mpappas your idea seems logical but can you please provide a working code? it seems like my mind is not working at the moment :confused:

thanks in advance!

as per all previous advice…

assume you have your objects in an array (ie, table with numeric keys) (we don’t care HOW they got there, just that they’re there, for discussion’s sake)
local objList = { obj1, obj2, obj3, obj4, obj5, obj6 }

now sort that list on y ascending (“back” to “front”, painter’s algorithm):

local function sortfn(a, b ) return a.y < b.y end
table.sort(objList, sortfn)

now iterate the table by numeric keys

for i=1,#objList do – or k,v in ipairs
objList[i]:toFront()
end

hth

It’s actually quite easy, don’t over think things… Objects are ordered by the order of which you insert them into a group:
 

local playerGroup=display.newGroup() local players={} local numOfPlayers=10 for i=numOfPlayers,1,-1 do players[i]=display... playerGroup:insert(players[i]) end -- player 1 will be in front of everyone, -- player 2 will be behind player 1 but in front of player 3, etc...

Tried it in an enterFrame event, didn’t work. still the group is messed up…

Players are not static, they move and change position throughout the game!!

This procedure looks complicated, can you provide a sample code please?

I found out that…it works when tried with indexed tables, but since my table use keys instead of indexes, it doesn’t work.

Here is a sample code if u wanna try…

local objList = { } local obj1 = display.newCircle(0,0,0) objList["obj1"] = obj1 local obj2 = display.newCircle(0,0,0) objList["obj2"] = obj2 local obj3 = display.newCircle(0,0,0) objList["obj3"] = obj3 local obj4 = display.newCircle(0,0,0) objList["obj4"] = obj4 local obj5 = display.newCircle(0,0,0) objList["obj5"] = obj5 local obj6 = display.newCircle(0,0,0) objList["obj6"] = obj6 obj1.y =10 obj2.y =40 obj3.y =20 obj4.y =50 obj5.y =60 obj6.y =30 local function sortfn(a, b ) return a.y \< b.y end --no matter if u coment this line or not, function will always produce the same result... table.sort(objList, sortfn) for k,v in pairs(objList) do --objList[i]:toFront() print(objList[k].y) end

RESULTS…

40 30 50 60 10 20

re-read my original post, you missed something important…

>> for k,v in pairs(objList) do

nope – pairs() defeats the whole purpose of sorting a list!, as the order of iteration with pairs() is undefined. you MUST iterate numerically, either with “for i=1,#table” or “for k,v in ipairs(table)” (note the “i” in ipairs, critical difference)

once sorted back-to-front, and iterated in numeric order, toFront() will indeed work.

[EDIT] it occurs to me that THIS is the part you might not “get”:

-- assuming your current list is by alpha keys: local anUnsortableList = ..whatever, your current list as is -- so MAKE a sortable list: local aSortableList = {} for k,v in pairs(anUnsortableList) do aSortableList[#aSortableList+1] = v end -- then sort it local function sortfn(a, b ) return a.y \< b.y end table.sort(aSortableList,sortfn) -- then order your objects in the display for i=1,#aSortableList do aSortableList[i]:toFront() end -- done, no longer need this aSortableList = nil

@davebollinger this is exactly what i wanted…i don’t have words to thank you, i was looking for this for a long time :slight_smile:

I am using it in an enterFrame function so i hope it is ok to do this every frame?

Thanks again!!!

Anyone please? I really need to solve it :frowning:

show some code and we can better help

I don’t know if I’m understanding your problem correctly, but I’ll give it a shot.

object:toFront() will move a display object to the front of its display group, so basically… iterate through the players in whatever order you want them, and call object:toFront() on each.

Like memo said, you need to sort the group based on Y coordinates.

I would suggest you create a sorting function, and the sorting function creates a new group, and then populates it with the new objects in descending order of Y coordinate.  Then insert the new group back into the main group, and delete the old one.

Also Do NOT use display:remove() or object:removeSelf() on the objects, only use :insert() into the new group. Then remove the old group, and insert the new one in your main display group.  When you remove() an object, the sdk nerfs it (used to anyways), and it could behave erratically later.

@mpappas your idea seems logical but can you please provide a working code? it seems like my mind is not working at the moment :confused:

thanks in advance!

as per all previous advice…

assume you have your objects in an array (ie, table with numeric keys) (we don’t care HOW they got there, just that they’re there, for discussion’s sake)
local objList = { obj1, obj2, obj3, obj4, obj5, obj6 }

now sort that list on y ascending (“back” to “front”, painter’s algorithm):

local function sortfn(a, b ) return a.y < b.y end
table.sort(objList, sortfn)

now iterate the table by numeric keys

for i=1,#objList do – or k,v in ipairs
objList[i]:toFront()
end

hth

It’s actually quite easy, don’t over think things… Objects are ordered by the order of which you insert them into a group:
 

local playerGroup=display.newGroup() local players={} local numOfPlayers=10 for i=numOfPlayers,1,-1 do players[i]=display... playerGroup:insert(players[i]) end -- player 1 will be in front of everyone, -- player 2 will be behind player 1 but in front of player 3, etc...

Tried it in an enterFrame event, didn’t work. still the group is messed up…

Players are not static, they move and change position throughout the game!!

This procedure looks complicated, can you provide a sample code please?