2011-03-24 82 views

回答

7

container.getChildIndex(displayObject);

的指数,但只有这样才能告诉你它有多深,不一定在它前面有任何东西。

+0

您可以比较显示对象的子索引,但它只在共享同一个父对象时才起作用。在通用情况下,我建议使用找到两个显示对象的最接近的共同祖先的实现之一,然后比较它们的第一个不常见的祖先索引 – jauboux 2013-07-19 14:12:02

0

container.getChildIndex(child)应该这样做,它返回子

0
private function getDepth(clip:DisplayObject):uint 
{ 
    var depth:uint = 0; 
    var currentClip:DisplayObject = clip; 
    while (currentClip.parent && currentClip.parent != this) 
    { 
     depth++; 
     currentClip = currentClip.parent; 
    } 
    return depth; 
} 
+0

键入参数作为DisplayObject会更有用(IE:TextFields,MovieClips等)。 ) – TheDarkIn1978 2011-03-24 17:40:34

+1

你是对的。无论如何,考虑到已验证的答案,我误解了这个问题 – Kodiak 2011-03-24 21:23:34

2

函数比较两个DisplayObject实例进行,以确定哪一个是在显示列表上的更高的“深度”:

private function higher(a:DisplayObject, b:DisplayObject):DisplayObject 
{   
    // Parent chains 
    var ac:Array = [a]; 
    var bc:Array = [b]; 

    // Pointers to individual nodes 
    var an:DisplayObject = a.parent; 
    var bn:DisplayObject = b.parent; 

    while (an != null) { 
     ac.push(an); 

     an = an.parent; 
    } 

    while (bn != null) { 
     bc.push(bn); 

     bn = bn.parent; 
    } 

    var acl:int = ac.length; 
    var bcl:int = bc.length; 

    var n:int = Math.min(acl, bcl); 
    var i:int = 0; 

    for (; i < n; i++) { 
     an = ac[acl - i - 1]; 
     bn = bc[bcl - i - 1]; 

     // First uncommon ancestor 
     if (an != bn) 
      break; 
    } 

    var ca:DisplayObjectContainer = an.parent; 
    if (!ca) 
     return null; 

    if (ca.getChildIndex(an) > ca.getChildIndex(bn)) 
     return a; 
    else 
     return b; 
} 

注意:如果目的之一是不显示列表上,则函数返回null。您可以将其更改为返回其他对象。

你几乎可以肯定地优化这个,但这是第一次切割。

2

只是一个使用向量的Manish答案的重构版本,如果您调用较高(a,a.parent),将不会返回奇怪的结果。

父母()可以用于其他用途太:

public function higher(a:DisplayObject, b:DisplayObject):DisplayObject 
{ 
    var aParents:Vector.<DisplayObject> = parents(a); 
    var bParents:Vector.<DisplayObject> = parents(b); 
    var commonDepth:int = Math.min(aParents.length, bParents.length); 
    for (var depth:int = 0; depth < commonDepth; depth++) 
     if (aParents[depth] != bParents[depth]) 
      break; 
    if (depth == 0 || depth == commonDepth) 
     return null; 
    var commonAncestor:DisplayObjectContainer = aParents[depth].parent; 
    if (commonAncestor.getChildIndex(aParents[depth]) > commonAncestor.getChildIndex(bParents[depth])) 
     return a; 
    else 
     return b; 
} 

private function parents(d:DisplayObject):Vector.<DisplayObject> 
{ 
    var result:Vector.<DisplayObject> = new Vector.<DisplayObject>; 
    while (d != null) 
    { 
     result.unshift(d); 
     d = d.parent; 
    } 
    return result; 
} 
+0

+1似乎正在为我工​​作。碰到一个问题(没有考虑当物体处于相同深度时会发生什么),所以我发布了另一个细微的变化。 – Pup 2014-04-26 08:02:57

+0

commonAncestor是a和b的最低共同祖先,aParents [深度]!= bParents [深度],因此它们不能具有相同的子索引,除非您从DisplayObjectContainer继承了end overriden getChildIndex()...没有例如深度相同,除非=== b,在这种情况下函数可以返回a或b! – jauboux 2014-06-19 12:44:38

0

这是一个什么样的版本Manish才做完jauboux的修订版。
即,当深度匹配时,从highestOf()添加null返回值。

/** 
* @param ifDepthsMatchReturnObjectA 
* @return Whichever DisplayObject is higher on the display list. 
*   Optionally returns `null` if they're at the same depth. 
*/ 
public function highestOf(a:DisplayObject, b:DisplayObject, ifDepthsMatchReturnObjectA:Boolean = false):DisplayObject 
{ 
    var aParents:Vector.<DisplayObject> = ancestorsOf(a); 
    var bParents:Vector.<DisplayObject> = ancestorsOf(b); 
    var commonDepth:int = Math.min(aParents.length, bParents.length); 
    for (var depth:int = 0; depth < commonDepth; depth++) 
     if (aParents[depth] != bParents[depth]) 
      break; 
    if (depth == 0 || depth == commonDepth) 
     return null; 
    var commonAncestor:DisplayObjectContainer = aParents[depth].parent; 
    var aDepthOnCommonAncestor:int = commonAncestor.getChildIndex(aParents[depth]); 
    var bDepthOnCommonAncestor:int = commonAncestor.getChildIndex(bParents[depth]); 
    if (aDepthOnCommonAncestor > bDepthOnCommonAncestor) 
     return a; 
    else if (aDepthOnCommonAncestor < bDepthOnCommonAncestor) 
     return b; 
    else 
     return ifDepthsMatchReturnObjectA ? a : null; 
} 

/** 
* @return Whether a is higher than b. 
*/ 
public function isHigher(a:DisplayObject, b:DisplayObject):Boolean 
{ 
    return highestOf(a, b) === a; 
} 

/** 
* @return All ancestors of given display. 
*/ 
private function ancestorsOf(display:DisplayObject):Vector.<DisplayObject> 
{ 
    var result:Vector.<DisplayObject> = new Vector.<DisplayObject>; 
    while (display != null) 
    { 
     result.unshift(display); 
     display = display.parent; 
    } 
    return result; 
} 
+0

你有一个示例,其中“else返回ifDepthsMatchReturnObjectA?a:null;”有用吗?对我来说,这只是死代码,如果深度匹配,它只是意味着一个=== b。我发现只有当a和b没有共同的祖先时才返回null更有用 – jauboux 2014-06-19 12:57:35