1

我希望能够快速检查给定的 DisplayObject 是否是另一个 DisplayObject 的后代(不是继承意义上的 - 即子、孙、曾孙、曾曾孙等)。

似乎没有本地方法可以做到这一点,我只能想到两种方法来实现它:

  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;
}
4

2 回答 2

7

圣角驼鹿,事件……

parent.contains(child);

请参阅DisplayObjectContainer.contains()的参考。

于 2010-10-26T11:31:11.353 回答
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;
}
于 2010-10-26T11:23:49.667 回答