Actionscript 3 动作脚本3。如何删除从另一个函数创建的子函数?

Actionscript 3 动作脚本3。如何删除从另一个函数创建的子函数?,actionscript-3,flash,actionscript,parent-child,children,Actionscript 3,Flash,Actionscript,Parent Child,Children,我首先添加了具有函数的子项,然后需要使用函数第三个将其删除 function first(event:MouseEvent):void { addChild(test); //here add child button.addEventListener(MouseEvent.CLICK, second); //here add listener to second function } function second(event:MouseEvent):void {

我首先添加了具有函数的子项,然后需要使用函数第三个将其删除

function first(event:MouseEvent):void {
   addChild(test); //here add child
   button.addEventListener(MouseEvent.CLICK, second); //here add listener to second function
}

    function second(event:MouseEvent):void {
       third(event); //here I call third function 
}

   private function third(event:Event):void {
       removeChild(test); //here should delete child, but I got error
}

但是我得到了以下错误:
ArgumentError:error#2025:提供的DisplayObject必须是调用者的子对象。
如何成功删除子对象?你能帮帮我吗?谢谢。

有两种方法。首先,保留对已添加剪辑的引用:

_private var clip:Sprite;

public function first():void {
    addChild(clip);
}

public function second():void {
    removeChild(clip);
}
将添加/删除的片段作为变量有助于在多个功能中使用它。但是,如果存在一些问题,例如不使用类和在多个位置使用代码,则始终可以使用
name
属性:

public function first():void {
    var child:Sprite = new Sprite(); // create new instance so it's not visible anywhere
    child.name = 'myFancySprite';
    addChild(child);
}

public function second():void {
    var child:Sprite = getChildByName('myFancySprite') as Sprite; // get it
    removeChild(child); // remove it
}

谢谢你的回答,我误解了你的第一种方法,我想这是我尝试过的,但是你的第二种方法和名字对我有用。非常感谢。