package com.test{
import flash.display.Sprite;
import flash.display.MovieClip;
import com.greensock.*;
import com.greensock.easing.*;
import com.test.CreateRoundRectButton;
import flash.events.*;
import flash.net.*;
public class DetailView extends MovieClip {
private var detailPanel:MovieClip=new MovieClip();
private var detailData:Object;
private var closeBtn:Sprite;
private var DetailForm:DetailViewForm=new DetailViewForm();
public function DetailView() {
createPanel();
addChild(detailPanel)
detailPanel.addChild(DetailForm);
}
private function createPanel()
{
closeBtn=new CreateRoundRectButton(30,30,10,1,0xFFFFFF,"X",0x000000);
closeBtn.x=DetailForm.width - 25;
closeBtn.y=2;
closeBtn.addEventListener(MouseEvent.MOUSE_UP, closePanel,false,0,true);
DetailForm.addChild(closeBtn)
}
public function closePanel(evt:MouseEvent) {
removeChild(evt.currentTarget)
}
}
}
如何删除此课程的孩子。当我按下关闭按钮时,它需要删除窗口。但我没有正确地得到这个事件。我怎么能删除这个。
答案 0 :(得分:3)
您似乎对Event.currentTarget的含义存在误解(或者只是编码错误)。当closeBtn上的鼠标向上调用closePanel时,evt.currentTarget的值为closeBtn(因为它是您添加侦听器的对象,它是处理事件的对象)。由于closeBtn不是DetailView的子项,因此您不会看到任何事情发生。实际上,如果您要在调试播放器中运行代码的调试版本,您将看到removeChild调用导致异常。
假设您希望删除detailPanel,那么您可以简单地忽略currentTarget并删除您想要删除的内容:
public function closePanel(evt:MouseEvent) {
removeChild(detailPanel);
}
OR ...由于MOUSE_UP冒泡,您可以将侦听器添加到detailPanel,evt.currentTarget将是detailPanel,因此removeChild调用将起作用:
detailPanel.addEventListener(MouseEvent.MOUSE_UP, closePanel, false, 0, true);