单击时AS3删除对象

时间:2012-08-13 12:48:08

标签: actionscript-3

我想在点击对象时将其设置为null,并且我正在尝试实现此代码:

        public function onClick(evt:MouseEvent):void{
        var thisPatient = evt.target;
        thisPatient = null;
    }

然而,元素仍在舞台上。

4 个答案:

答案 0 :(得分:4)

public function onClick(evt:MouseEvent):void{
    var thisPatient = evt.target;
    (thisPatient as DisplayObject).parent.removeChild(thisPatient);
    //or if thisPatient is this
    parent.removeChild(this);
}

但允许孩子自行移除是不好的做法。更正确的解决方案是调度事件,因为父母必须决定删除或不删除子。

public function onClick(evt:MouseEvent):void{
    dispatchEvent(new Event("removeMe", true));
}

//parent's code...
child.addEventListener("removeMe", removeHandler);

答案 1 :(得分:3)

将其设置为null并不足够。您还必须使用removeElement()removeChild()将其从父容器中删除,具体取决于您使用的容器类型。

答案 2 :(得分:2)

你必须做removeChild(thisPatient),如果你把对象放在另一个对象中,你必须做parent.removeChild(thisPatient)

答案 3 :(得分:1)

根据我的经验,注册一个事件对象,您必须删除所有事件。所有活动将完全手动删除。 removeChild对象不会释放所有事件。 removeChild但是,发生了精细的内存泄漏。这是因为您没有删除该事件。在删除对象之前,必须删除该事件。