2010-10-26 32 views
1

我希望能够快速检查给定的DisplayObject是否是另一个DisplayObject的后代(而不是继承意义上的 - 也就是孩子,孙辈,曾孙,曾曾孙等)。有没有办法检查DisplayObject A是否是DisplayObject B的后代?

似乎有不被本机的方式来做到这一点,我只能想到的两种方式来实现它:

  1. 创建所有的嵌套循环的母亲。似乎有点,我不知道,错了?
  2. 在'孩子'中发送冒泡事件并检查潜在的“父母”是否收到冒泡事件。

我现在正在尝试后者,但会欣赏一些输入。我想创建一个很好的实用静态功能,例如:

static public function isDescendantOf(child:DisplayObject, parent:DisplayObjectContainer):Boolean { 

    var isDescendant: Boolean = false; 

    // perform some magical 
    // check that returns true 
    // if it is a descendant 

    return isDescendant; 
} 

回答

0

好吧我得到它的工作,但它使用了一个讨厌的匿名函数。

想知道它是否可以改进?

static public function isDescendantOf(child:DisplayObject, parent:DisplayObjectContainer):Boolean { 
    const HELLO:String = "hello"; 
    var isDescendant:Boolean = false; 

    parent.addEventListener(HELLO, function(e:Event):void { 
     isDescendant = true; 
    }); 

    child.dispatchEvent(new Event(HELLO, true, false)); 
    return isDescendant; 
} 
相关问题