如何在 Flash AS3 中获取关闭或删除实例的事件

发布于 2024-08-13 08:08:31 字数 1199 浏览 6 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梅窗月明清似水 2024-08-20 08:08:31

您似乎误解了 Event.currentTarget 的含义(或者这只是一个编码错误)。当鼠标悬停在 closeBtn 上而调用 closePanel 时,evt.currentTarget 的值为 closeBtn(因为它是添加侦听器的对象,所以它是处理事件的对象)。由于 closeBtn 不是 DetailView 的子级,因此您不会看到任何事情发生。事实上,如果您要在调试播放器中运行代码的调试版本,您将看到removeChild 调用会导致异常。

假设您希望删除detailPanel,那么您可以简单地忽略currentTarget并删除您已经知道要删除的内容:

public function closePanel(evt:MouseEvent) {
    removeChild(detailPanel);
}

或者...由于MOUSE_UP冒泡,您可以将侦听器添加到detailPanel,并且evt.currentTarget将是detailPanel所以removeChild调用将起作用:

detailPanel.addEventListener(MouseEvent.MOUSE_UP, closePanel, false, 0, true);

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:

public function closePanel(evt:MouseEvent) {
    removeChild(detailPanel);
}

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:

detailPanel.addEventListener(MouseEvent.MOUSE_UP, closePanel, false, 0, true);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文