如何在 Flash AS3 中获取关闭或删除实例的事件
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)
}
}
}
我怎样才能删除这个班级的孩子。当我按下关闭按钮时,需要删除窗口。但我没有正确地处理这个事件。我怎样才能删除这个。
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)
}
}
}
How can i remove the child of this class. when i press on the close button it needs to be remove the window. But am not getting this event properly. how can i remove this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您似乎误解了 Event.currentTarget 的含义(或者这只是一个编码错误)。当鼠标悬停在 closeBtn 上而调用 closePanel 时,evt.currentTarget 的值为 closeBtn(因为它是添加侦听器的对象,所以它是处理事件的对象)。由于 closeBtn 不是 DetailView 的子级,因此您不会看到任何事情发生。事实上,如果您要在调试播放器中运行代码的调试版本,您将看到removeChild 调用会导致异常。
假设您希望删除detailPanel,那么您可以简单地忽略currentTarget并删除您已经知道要删除的内容:
或者...由于MOUSE_UP冒泡,您可以将侦听器添加到detailPanel,并且evt.currentTarget将是detailPanel所以removeChild调用将起作用:
You seem to have a misunderstanding of what Event.currentTarget is meant to be (or it's just a coding error). At the time closePanel is called as a result of the mouse up on closeBtn the value of evt.currentTarget is closeBtn (since it is the object to which you added the listener, it is the object handling the event). Since closeBtn is not a child of DetailView you won't see anything happen. In fact, if you were to run a debug build of your code in a debug player you'll see that the removeChild call results in an exception.
Assuming that you want the detailPanel to be removed then you can simply disregard the currentTarget and remove what you already know you want to remove:
OR ... Since MOUSE_UP bubbles you can add the listener to the detailPanel and evt.currentTarget will be the detailPanel so the removeChild call will work: