Depth Sorting of objects – Important Classes for games in AS 3.0

December 22nd, 2011 by aabhaanjan

images
[xml]
package
{
/**
* …
* @description : This Class can be used when you need to adjust depth of objects according to their heights.
* @author Aava Rani
*/

public class DepthSortingClass extends MovieClip
{
private var _parent:Object
public function DepthSortingClass()
{
super();
}

public function setSortingParent(__parent:Object):void {
_parent = __parent
}

public function sortDisplayList():void {
var len:uint = _parent.numChildren;
var i, j;
for ( i = 0; i < len - 1; i++ ) for (j = i + 1; j < len; j++) if ( _parent.getChildAt(i).y > _parent.getChildAt(j).y )
_parent.swapChildrenAt( i, j );

}

}

}

[/xml]