Zhafur
Zhafur

Reputation: 1624

AS3 depth sorting with extras

I got an issue recently with depth sorting in my program. Basically, I sort on every frame. The sort should watch the "depth" which is actually the scaleX and scaleY (they're always the same so I'm just watching the scaleX), so those elements which has bigger scaleX should be above of those which has lower but also check the y value and then sort. In one word: sort on scaleX then sort on Y. Any suggestions how could I write such sorting function?

Edit: I wrote this function, if the two display objects aren't in the same "depth" (scaleX), then they're swapping each other.

public static function sortEverythingInContainer(container:DisplayObjectContainer, fromIndex:int = 1) {
        var i : int = 0;
        var j : int = 0;
        var n : int = container.numChildren;
        var child1:DisplayObject;
        var child2:DisplayObject;
        for (i = fromIndex; i < n; ++i){
            j = i;

            child1 = container.getChildAt(j);
            child2 = container.getChildAt(j - 1);
            while (j > fromIndex-1 && (child1.scaleX < child2.scaleX?false:child1.y < child2.y)) {
                container.swapChildren(child1, child2);
                --j;
                if (j <= fromIndex-1)
                    break;
                child1 = container.getChildAt(j);
                child2 = container.getChildAt(j - 1);  
            }

        }
    }

Solution:
I had to sort twice by creating 2 for cycles, first sorts depending on Y, then scale. enter code here

Upvotes: 0

Views: 443

Answers (1)

Ivan Chernykh
Ivan Chernykh

Reputation: 42166

If i understand you right , you can use something like this:

 arr.sortOn("scaleX",  Array.NUMERIC); 
 arr.sortOn("y",  Array.NUMERIC); 

Reference: http://help.adobe.com/en_US/ActionScript/3.0_ProgrammingAS3/WS5b3ccc516d4fbf351e63e3d118a9b90204-7fa4.html

Upvotes: 1

Related Questions