Don't remove and re-add children, that's much more expensive
and will trigger events. Instead, sort the children and then set their indicies. git-svn-id: svn+ssh://src.earth.threerings.net/nenya/trunk@461 ed5b42cb-e716-0410-a449-f6a68f950b19
This commit is contained in:
@@ -36,26 +36,26 @@ public class DisplayUtil
|
|||||||
*
|
*
|
||||||
* comp is a function that takes two DisplayObjects, and returns int -1 if the first
|
* comp is a function that takes two DisplayObjects, and returns int -1 if the first
|
||||||
* object should appear before the second in the container, 1 if it should appear after,
|
* object should appear before the second in the container, 1 if it should appear after,
|
||||||
* and 0 if the order does not matter.
|
* and 0 if the order does not matter. If omitted, Comparators.COMPARABLE
|
||||||
|
* will be used- all the children should implement Comparable.
|
||||||
*/
|
*/
|
||||||
public static function sortDisplayChildren (container :DisplayObjectContainer, comp :Function) :void
|
public static function sortDisplayChildren (
|
||||||
|
container :DisplayObjectContainer, comp :Function = null) :void
|
||||||
{
|
{
|
||||||
var numChildren :int = container.numChildren;
|
var numChildren :int = container.numChildren;
|
||||||
|
// put all children in an array
|
||||||
var children :Array = new Array(numChildren);
|
var children :Array = new Array(numChildren);
|
||||||
|
var ii :int;
|
||||||
// pull the display children into an array.
|
for (ii = 0; ii < numChildren; ii++) {
|
||||||
// guess that removing children from the end of a DisplayObjectContainer
|
children[ii] = container.getChildAt(ii);
|
||||||
// is more efficient than removing them from the beginning.
|
|
||||||
for (var i :int = numChildren - 1; i >= 0; --i) {
|
|
||||||
children[i] = container.removeChildAt(i);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// stable sort the array
|
// stable sort the array
|
||||||
ArrayUtil.stableSort(children, comp);
|
ArrayUtil.stableSort(children, comp);
|
||||||
|
|
||||||
// add children back to the container
|
// set their new indexes
|
||||||
for each (var child :DisplayObject in children) {
|
for (ii = 0; ii < numChildren; ii++) {
|
||||||
container.addChild(child);
|
container.setChildIndex(DisplayObject(children[ii]), ii);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user