I have a list containing display objects from throughout the application (insertion order).
I want to process the list either top-down (parent, child) or bottom up (child, par开发者_StackOverflow社区ent). The only requirement is that either a parent is processed before any child or vice versa a child before its parent.
What is a good approach?
Edit: This question is not about sorting a list. It's about performantly retrieving the depth of a particular display object.
Edit2: Example
Display list:
A (root)
B1
C1
C2
D1
B2
C3
D2
D3
C4
D4
B3
C5
D5
B4
C6
B5
My list:
list = [E1, F4, A, B2, B1, C3, ..., N9, N8]
Bottom-up:
N9, N8, F4, E1, C3, B2, B1, A
Top-down:
A, B2, B1, C3, E1, F4, N9, N8
Note:
Does not matter if N9 before N8 or N8 before N9. Imporant is that any N is before M (first run) or any M before its children N* (second run).
If I understand you right:
// bottom up
var num:int = parent.numChildren;
var child:DisplayObject = null;
for( var i:int = num - 1; i >= 0; i-- )
{
child = parent.getChildAt( i );
// do whatever
}
// top down
num = parent.numChildren;
for( i = 0; i < num; i++ )
{
child = parent.getChildAt( i );
// do whatever
}
Edit:
Ok, based on your example, you can use recursion to do this. Something like:
private var m_order:Vector.<DisplayObject> = new Vector.<DisplayObject>;
private function _doSomething( dObj:DisplayObject ):void
{
// do my thing here
// add to our order vector
this.m_order.push( dObj );
var container:DisplayObjectContainer = dObj as DisplayObjectContainer;
if( container == null || container.numChildren == 0 )
return;
var len:int = container.numChildren;
for ( var i:int = 0; i < len; i++ )
this._doSomething( container.getChildAt( i ) );
}
// then start it off with
this._doSomething( root );
Provided that you can do it top down first, this should be grand. To come back up (bottom-up), just reverse-traverse the m_order Vector
.
If you need to do it bottom up first, then create the array first, then reverse-traverse, then traverse normal.
It's the easiest way I can think of. Also, not German, Irish :)
Flex components have nestLevel, but I don't think pure Flash/As3 objects do.
精彩评论