如何删除 movieclip 中的 child

How to remove the child inside movieclip

我在舞台上有一个名为 scenePage 的 MovieClip,还有一个名为 char_panel 的由角色 MC 组成的 MovieClip。其中之一是 char1_mc.

当我点击 char1_mc 时,这会创建一个新实例 (newChar),在 scenePage 中添加一个 child。我在舞台上还有一个名为 btn_remove 的按钮,单击它应该会删除 scenePage 上的 newChar。问题是,当我单击按钮时,scenePage 中的 child(newChar) 不会删除。

我试过使用

scenePage.removeChild(newChar);

但是它给我一个错误提示,"Parameter child must non-null."有没有其他方法可以访问 scenePage 中的实例?我真的需要访问 scenePage 上的 child。

这是 char_panel 里面的脚本,其中 char1_mc 是,character1 是 char1_mc 的 class 名称,在我为 AS3 导出后:

import flash.display.MovieClip;

var newChar: MovieClip;

char1_mc.addEventListener(MouseEvent.CLICK, showChar1);
function showChar1(e:MouseEvent):void
{
    newChar = new character1();
    newChar.height = 215;
    newChar.width = 220;
    newChar.x = 20;
    newChar.y = 202.60;
    MovieClip(root).scenePage.addChild(newChar);
}

这是 btn_remove 的脚本,在主时间线中。

btn_remove.addEventListener(MouseEvent.CLICK, remove_character);
function remove_character(e:MouseEvent):void
{
    scenePage.removeChild(newChar);
}

提前致谢:)

您的问题属于范围之一。

主要时间线和您的 char_panel 有不同的范围。这意味着一个中的变量在另一个中不可用。

要从主时间轴到达 newChar,您必须向下钻取到适当的范围。

假设char_panel是主时间轴上对象的实例名称。如果是这样,您必须在主时间轴代码上执行此操作:

scenePage.removeChild(char_panel.newChar);

也就是说 "remove the newChar object that is part of the char_panel scope"