Hundreds of moving objects depth problem.

I have a problem about mc depth. I prepared around 200 enemy mc objects. These enemy objects moves on game area with their random path. The problem is depth order. When an enemy pass near by another enemy display order goes wrong.
I have 2D area. I try to give 3D effect visually depending on their Y coordinate value. If Y coordinate of an enemy is higher than other this enemy should show at top. If Y koordinate is lower than another this enemy should show on back. When all enemy objects moving their depth should calculate dynamically. I defined an array. I m sorting all enemy objects depending their Y value first. Then I set their depth value after this calculation. But so many objects with random move requares huge processor work. How can I do this simply?

Some quick searching reveals this:

They suggest bubblesort. @kirupa has a tutorial on bubblesort here:

If you look back even further, I think he has an ActionScript tutorial on the subject.

Hopefully this stimulates your thinking.

Thanks krilnon. Your links are very helpful. However I m searching another way of display ordering. With using AS2 solution was very simple because it is possible to set object’s depth higher how much I want. It is not necessary to sorting. Depth value can be more than number of objects. Unfortunatelly AS3 doesn’t allow this. Now I m thinking. If I create 1000 Empty movieclips AS3 allows me set depth value up to 5000. If my enemy objects moving between 0 and 5000 (Y component of its coordinate). This solution causing another problem. Main stage movement goes slow when I drag it.

My another poor idea is calculation a constant depending on (numberOfObjects / maxYvalue) each object’s depth can set depending on this ratio (may multiplying its Y value and this ratio).

I think setChildIndex is good but I like need freedom of swapDepths() in AS3.
Thanks for your attention again krilnon.

So you have addChild, addChildAt, setChildIndex. Maybe more i forget

I had the exact problem you describe a few months ago. Coincidentally it was also 200 objects. I was using starling and what i found is that changing child index upwards of 200 times at 60fps was just not possible. Now, starling is different, but i would suggest you try simple those few functions on even just half the objects in the game loop just to see if it can handle it. In starling it could not

In my scenerio i ended up doing some tricks to limmit how often the depths were changed because it seemed it just couldn’t handle so many rearranging per frame