“.parent”的动态数量

发布于 2024-09-15 17:58:20 字数 249 浏览 1 评论 0原文

我有2个对象。在MovieClip的不同深度。我想知道对象 A 的父对象是否与对象 B 的父对象相同。我想动态地将“.parent”添加到对象 A/B,直到它们达到相同的级别(具有相同的父对象)。我怎样才能做到这一点?

我的想法是有类似的东西

objectA = objectA + ".parent"

并让它循环直到达到目标。但这不是添加更多“.parent”层的正确方法。有人知道应该如何编码吗?

I have 2 object. In different depths of MovieClip. I would like to know if object A parent is same with object B parent. I wanted to dynamically add '.parent' to object A/B till it both reaches the same level (having the same parent object). How can I do that?

My idea was to have something like

objectA = objectA + ".parent"

and make it loop till it reaches the target. But this is not the correct way to add in more layers of '.parent'. Anyone know how it should be coded?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

辞取 2024-09-22 17:58:20

您可以使用包含方法

公共函数 contains(child:DisplayObject):Boolean
确定指定的显示对象是 DisplayObjectContainer 实例的子级还是实例本身。搜索包括整个显示列表,其中包括此 DisplayObjectContainer 实例、孙子、曾孙等。

function haveCommonParent(a:DisplayObject, b:DisplayObject):Boolean
{
  for(var p:DisplayObjectContainer = a.parent; p != null; p = p.parent)
  {
      if(p.contains(b))
          return true;
  }
  return false;
}

对于巨大的显示列表可能会很慢。

更新:获取共同的父级(如果有)。如果两者都在舞台上,这将返回一个 Stage 对象。

function getCommonParent(a:DisplayObject, b:DisplayObject):DisplayObjectContainer
{
  for(var p:DisplayObjectContainer = a.parent; p != null; p = p.parent)
  {
      if(p.contains(b))
          return p;
  }
  return null;
}

You can use contains method

public function contains(child:DisplayObject):Boolean
Determines whether the specified display object is a child of the DisplayObjectContainer instance or the instance itself. The search includes the entire display list including this DisplayObjectContainer instance, grandchildren, great-grandchildren, and so on.

function haveCommonParent(a:DisplayObject, b:DisplayObject):Boolean
{
  for(var p:DisplayObjectContainer = a.parent; p != null; p = p.parent)
  {
      if(p.contains(b))
          return true;
  }
  return false;
}

Might be slow for huge display lists.

Update: get the common parent, if any. This will return a Stage object if both are on stage.

function getCommonParent(a:DisplayObject, b:DisplayObject):DisplayObjectContainer
{
  for(var p:DisplayObjectContainer = a.parent; p != null; p = p.parent)
  {
      if(p.contains(b))
          return p;
  }
  return null;
}
小姐丶请自重 2024-09-22 17:58:20
var mc:MovieClip;
while (mc.parent != null && mc.parent != targetParent)
{
    mc = mc.parent;
}
var mc:MovieClip;
while (mc.parent != null && mc.parent != targetParent)
{
    mc = mc.parent;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文