this method will stack your DisplayObjects properly- it returns a Boolean indicating whether a swap was made in case you need to handle this (graphically or otherwise):
Code:public static function sortChildrenByFauxZ( container:DisplayObjectContainer ) : Boolean { var numChildren:int = container.numChildren; //no need to sort (zero or one child) if( numChildren < 2 ) return false; var depthsSwapped:Boolean; //create an Array to sort children var children:Array = new Array( numChildren ); var i:int = -1; while( ++i < numChildren ) { children[ i ] = container.getChildAt( i ); } //sort by children's y position children.sortOn( "y", Array.NUMERIC ); var child:DisplayObject; i = -1; while( ++i < numChildren ) { child = DisplayObject( children[ i ] ); //only set new depth if necessary if( i != container.getChildIndex( child ) ) { depthsSwapped = true; //set their new position container.setChildIndex( child, i ); } } //returns true if 1 or more swaps were made, false otherwise return depthsSwapped; }




Reply With Quote