尝试从 eventLlistener 获取 id 时出现问题

发布于 2024-10-13 16:28:10 字数 907 浏览 3 评论 0原文

我正在尝试通过事件监听器将 id 发送到它的监听函数。现在,当我仅使用代码创建按钮时,这可以工作,但是当我决定使用现有的 movieClips 时,它停止了,我不知道为什么。

这是我的按钮代码。

private function addButtons(){
        button1 = new ButtonMC();
        //add the buttons
        button1.id = 1;
        button1.addEventListener(MouseEvent.CLICK, gameClick, false, 0, true);
        button1.buttonMode = true;
        addChild(button1);
        trace("button1 = "+button1.id);

跟踪

发现button1.id没有问题。到目前为止还可以。 当我运行此函数时,侦听器函数报告为“未定义”。这是我的监听器函数代码。 (不是全部......只是相关部分)。

private function gameClick(evt:MouseEvent):void{
        trace("clicked "+evt.target.id);
        var gameTypeID = evt.target.id;

该代码

位于它自己的单独的类中,该类生成开始屏幕。 movieClip 按钮是库中导出为 ButtonMC 的 MC 的副本。我完全不明白为什么这现在不起作用。当它确实工作时,“生成”按钮仍然是正常的影片剪辑,所以我看不出有什么区别。也许这是一个范围问题,但是由于所有这些代码都在同一个类中,我不明白为什么这会成为问题。

有人能发现我的白痴并让我知道吗?非常感谢您一如既往的帮助。

I'm trying to send an id via an event listener to it's listening function. Now this worked before when I created the buttons just using code, but when I decided to use existing movieClips it stopped and I can't work out why.

Here's my button code.

private function addButtons(){
        button1 = new ButtonMC();
        //add the buttons
        button1.id = 1;
        button1.addEventListener(MouseEvent.CLICK, gameClick, false, 0, true);
        button1.buttonMode = true;
        addChild(button1);
        trace("button1 = "+button1.id);

}

the trace finds the button1.id no problem. So ok so far.
When I run this the listener function reports back as 'undefined'. Here is my listener function code. (not all of it.. just the relevant part).

private function gameClick(evt:MouseEvent):void{
        trace("clicked "+evt.target.id);
        var gameTypeID = evt.target.id;

}

This code sits in it's own separate class which generates the start screen. The movieClip buttons are duplicates of an MC in the library which is exporting as ButtonMC. I'm totally stumped as to why this is not working now. When it did work the "generated" buttons where still normal movieClips so I see no difference there. Maybe it's a scope thing, but with all of this code in the same class I can't see why that would be the issue.

Can anyone spot my idiocy and let me know please? Your help as always is greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

梦亿 2024-10-20 16:28:10

我的第一个猜测是,在 ButtonMC 实例中单击的实际形状或精灵被注册为 event.target。您可以测试这个理论并在侦听器中跟踪 getQualifiedClassName(event.target) 。或者您可以立即尝试修复并添加 button1.mouseChildren = false; 以查看其是否有效。

My first guess would be that the actual shape or sprite that is clicked within the ButtonMC instance, is registered as the event.target. You could test this theory and trace getQualifiedClassName(event.target) in your listener. Or you could try the fix right away and add button1.mouseChildren = false; to see if it works.

等风来 2024-10-20 16:28:10

来自鼠标事件的 target 属性指的是实际单击的 DisplayAsset - 在本例中,是按钮内的其他 DisplayAsset。您可能会注意到目标parent是按钮(或parentparent, ETC)。

无论如何,使用 currentTarget 来获取添加了事件的目标,或者将按钮的 mouseChildren 设置为 false (这样,它的子项本身不会捕获鼠标事件)。

The target property coming from a mouse event refers to the DisplayAsset actually clicked - in this case, some other DisplayAsset inside your button. You'll probably notice that the target's parent is the button (or the parent's parent, etc).

Anyway, use currentTarget instead to get the target that had the event added, or set mouseChildren as false for the button (that way, its children won't capture mouse events themselves).

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