Switching objects positions in groups?

What is the most effective and fastest way to switch objects group layer like this:

When (for example) an object1 is moved on the screen and it should be shown before another object2 (when it’s y-value on a map is bigger then y from object2 for example) I want to switch the objects group index position, so the object1 is shown in front of the object2. Because there are a lot of other moving objects on the screen I only want to switch the index for this two objects and can’t work with object1:toFront() here.

Is there a “Best Way” to do this kind of object group switching? This is needed in all kind of games, like for example when characters are moving on a map and they have to appear in front of enemies or buildings or stuff, when their y-value is bigger then the enemies, building… y-value.

What is the fastest (and easiest) way to switch objects in groups to make this switching work fast with a LOT of different objects?

Any help welcome! :slight_smile:

I’m fairly sure Corona’s toFront() and toBack() are quite fast (and I know Lua is pretty fast - going through even as much as 500 objects and calling toFront()/toBack() shouldn’t take any time at all); I know that’s how it’s done in the isometric sample code I’ve seen for Corona - all the objects are inserted into a group, then when the object moves backwards or forwards a reorderLayers() method is used. You could try that and see how it goes; if it ends up being slow you can try splitting your objects up into multiple groups. Each group would then be ordered separately.

You should certainly at least try just doing the “slow” sorting; you might be surprised at the speed unless you have 5,000 objects or something.

  • Caleb

Hi Daniela,

It sounds like you need to constantly manage the z-index of items based on their Y position. So, if a new character appears on screen, it’s possible that a pre-existing character will still need to appear in front of that new character because its Y position is higher. True? If so, the only way you can effectively manage this is to loop through every character on screen when a new object is added, sort them by Y position, and then re-insert them into the same display group going from furthest back (lowest Y value) to furthest front (highest Y value). This shouldn’t be exceedingly performance heavy, unless you have like 1000 characters on the screen at once, but then you’ll probably face other performance issues based on the sheer number of objects.

Now, if these characters are moving all around, shifting positions and so forth, then you’d need to check more often to see if their z-indexes must shift (say, one character happens to move in front of another one). This task would be more complicated, but if you’re using physics, I think a collision system of sorts may work… I could specify a method if that’s what you need.

Best regards,

Brent

Here’s a forum post with some suggestions on how to accomplish this:

http://forums.coronalabs.com/topic/49069-z-indexing-dynamically-generated-creeps/

Thank you for all the fast feedback!

@Brent: That’s exactly what I’m looking for :slight_smile: … but I haven’t looked into the link from Alex above. Maybe there already is a sample there.

Best,

Daniela

I’m fairly sure Corona’s toFront() and toBack() are quite fast (and I know Lua is pretty fast - going through even as much as 500 objects and calling toFront()/toBack() shouldn’t take any time at all); I know that’s how it’s done in the isometric sample code I’ve seen for Corona - all the objects are inserted into a group, then when the object moves backwards or forwards a reorderLayers() method is used. You could try that and see how it goes; if it ends up being slow you can try splitting your objects up into multiple groups. Each group would then be ordered separately.

You should certainly at least try just doing the “slow” sorting; you might be surprised at the speed unless you have 5,000 objects or something.

  • Caleb

Hi Daniela,

It sounds like you need to constantly manage the z-index of items based on their Y position. So, if a new character appears on screen, it’s possible that a pre-existing character will still need to appear in front of that new character because its Y position is higher. True? If so, the only way you can effectively manage this is to loop through every character on screen when a new object is added, sort them by Y position, and then re-insert them into the same display group going from furthest back (lowest Y value) to furthest front (highest Y value). This shouldn’t be exceedingly performance heavy, unless you have like 1000 characters on the screen at once, but then you’ll probably face other performance issues based on the sheer number of objects.

Now, if these characters are moving all around, shifting positions and so forth, then you’d need to check more often to see if their z-indexes must shift (say, one character happens to move in front of another one). This task would be more complicated, but if you’re using physics, I think a collision system of sorts may work… I could specify a method if that’s what you need.

Best regards,

Brent

Here’s a forum post with some suggestions on how to accomplish this:

http://forums.coronalabs.com/topic/49069-z-indexing-dynamically-generated-creeps/

Thank you for all the fast feedback!

@Brent: That’s exactly what I’m looking for :slight_smile: … but I haven’t looked into the link from Alex above. Maybe there already is a sample there.

Best,

Daniela