Actionscript 3 “如何追踪”;“深度”;或显示对象的堆叠顺序?
如何使用AS3跟踪显示对象的“深度”或堆叠顺序Actionscript 3 “如何追踪”;“深度”;或显示对象的堆叠顺序?,actionscript-3,Actionscript 3,如何使用AS3跟踪显示对象的“深度”或堆叠顺序 我正在试图找出我的精灵是否在另一个精灵后面…容器。getChildIndex(child)应该这样做,它返回子精灵的索引容器。getChildIndex(displayObject) 但这只会告诉您它有多深,不一定是前面有什么东西。函数比较两个DisplayObject实例,以确定哪个实例在显示列表中的“深度”更高: private function getDepth(clip:DisplayObject):uint { var depth
我正在试图找出我的精灵是否在另一个精灵后面…
容器。getChildIndex(child)
应该这样做,它返回子精灵的索引容器。getChildIndex(displayObject)代码>
但这只会告诉您它有多深,不一定是前面有什么东西。函数比较两个DisplayObject实例,以确定哪个实例在显示列表中的“深度”更高:
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;
}
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;
}
private function higher(a:DisplayObject,b:DisplayObject):DisplayObject
{
//父链
var ac:Array=[a];
var bc:Array=[b];
//指向单个节点的指针
变量an:DisplayObject=a.parent;
变量bn:DisplayObject=b.parent;
while(an!=null){
交流推力(an);
an=父母;
}
while(bn!=null){
bc.推送(bn);
bn=bn.父母;
}
var acl:int=ac.length;
var bcl:int=bc.length;
变量n:int=Math.min(acl,bcl);
变量i:int=0;
对于(;ica.getChildIndex(bn))
返回a;
其他的
返回b;
}
注意:如果其中一个对象不在显示列表中,则函数返回null
。您可以将其更改为返回另一个对象
您几乎可以肯定地对此进行优化,但这是第一个切入点。只是使用向量的answer的重构版本,如果您调用更高的(a,a.parent),它不会返回奇怪的结果
parents()也可用于其他用途:
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;
}
public function higher(a:DisplayObject,b:DisplayObject):DisplayObject
{
var aParents:向量=父代(a);
var b父母:向量=父母(b);
var commonDepth:int=Math.min(aParents.length,bParents.length);
for(变量深度:int=0;深度<公共深度;深度++)
if(a租金[深度]!=b租金[深度])
打破
如果(深度==0 | |深度==commonDepth)
返回null;
var common祖先:DisplayObjectContainer=aParents[depth].parent;
if(common祖先.getChildIndex(aParents[depth])>common祖先.getChildIndex(bParents[depth]))
返回a;
其他的
返回b;
}
私有函数父函数(d:DisplayObject):向量。
{
var结果:向量=新向量。;
while(d!=null)
{
结果:未移位(d);
d=d.父母;
}
返回结果;
}
这是一个修订版,它是从一个版本中删除的。
即,当深度匹配时,从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;
}
/**
*@param ifDepthsMatchReturnObjectA
*@return显示列表中较高的DisplayObject。
*如果它们位于同一深度,则可以选择返回'null'。
*/
公共函数highestOf(a:DisplayObject,b:DisplayObject,ifDepthsMatchReturnObjectA:Boolean=false):DisplayObject
{
var aParents:Vector.=a;
var b参数:向量=ancestorsOf(b);
var commonDepth:int=Math.min(aParents.length,bParents.length);
for(变量深度:int=0;深度<公共深度;深度++)
if(a租金[深度]!=b租金[深度])
打破
如果(深度==0 | |深度==commonDepth)
返回null;
var common祖先:DisplayObjectContainer=aParents[depth].parent;
var adepthoncommon祖先:int=common祖先.getChildIndex(aParents[depth]);
var-bdepthoncomonsensort:int=commonsensort.getChildIndex(bParents[depth]);
if(AdeptHonCommonAsentor>BDeptHonCommonAsentor)
返回a;
else if(AdeptHonCommonAsentor
将参数键入DisplayObject会更有用(例如:TextFields、MovieClips等)。您完全正确。无论如何,考虑到经过验证的答案,我误解了这个问题,您可以比较显示对象的子索引,但只有当它们共享同一父对象时,它才起作用。在一般情况下,我建议使用其中一种实现,找到两个显示对象最接近的共同祖先,然后比较它们的第一个不常见祖先索引+1似乎对我有用。遇到了一个问题(没有考虑到当对象处于同一深度时会发生什么),因此我发布了另一个关于这个问题的轻微变化。CommonAsentor是a和b的最低共同祖先,aParents[depth]!=bParents[depth],因此它们不能具有相同的子索引,除非您继承了DisplayObjectContainer end重写的getChildIndex()…没有相同的深度,除非a==b,在这种情况下,函数无论如何都可以返回a或b!您是否有“else return ifDepthsMatchReturnObjectA?a:null;”有用的示例?对我来说,这只是死代码,如果深度匹配,它只意味着a==b。我发现只有当a和b没有共同祖先时才返回null更有用