在子类中处理MouseEvent.CLICK、MOUSE_DOWN
我正在构建一个按钮面板,它们包含在 Canvas 容器中。为此,我创建了一个 MyButton
类,它是 UIComponent 的子类。 MyButton 类还有另外 2 个子类:MyRadioButton
和 MyMenuButton
,它们在 MouseEvent.MOUSE_DOWN
上有另一种行为。 MyMenuButton
创建并显示一个菜单,我从 XML 构建该菜单,并且构建正常。
我在超类中添加侦听器,如下所示:
this.addEventListener(MouseEvent.CLICK, handleClick);
this.addEventListener(MouseEvent.MOUSE_DOWN, handleMouse);
this.addEventListener(MouseEvent.MOUSE_UP, handleMouse);
它是在创建阶段完成的。
在我的子类中,我重写了handleMouse处理程序。
override protected handleMouse(event:MouseEvent) : void
{
// subclass specific handling
}
这些按钮对象添加到画布容器中,如下所示:
在类 MyButtonsContainer.as
中:
this.rowChildren.addChild(button);
这些按钮完美地绘制到位。问题在于行为: 该事件不会到达超类的handleClick 处理程序。这实际上就是问题所在——为什么会这样? 提前致谢
编辑:看来 MOUSE_DOWN & MOUSE_UP 会干扰 CLICK 事件。当我删除它们的监听器时,我可以单击处理程序。如何使它们生活在一起?
I'm building a panel of buttons, that are enclosed in Canvas container. For this purpose I created a MyButton
class, which is a subclass of UIComponent. MyButton class has 2 other subclasses: MyRadioButton
and MyMenuButton
, which have another behavior on MouseEvent.MOUSE_DOWN
. MyMenuButton
creates and shows a menu, that I build from XML and it builds ok.
I add listeners in the superclass like this:
this.addEventListener(MouseEvent.CLICK, handleClick);
this.addEventListener(MouseEvent.MOUSE_DOWN, handleMouse);
this.addEventListener(MouseEvent.MOUSE_UP, handleMouse);
It's done at the creation stage.
In my subclasses I override handleMouse
handler.
override protected handleMouse(event:MouseEvent) : void
{
// subclass specific handling
}
These button objects are added to canvas container as following:
in class MyButtonsContainer.as
:
this.rowChildren.addChild(button);
These buttons draw perfectly and in place. The problem is the behavior:
The event doesn't come to handleClick handler of superclass. And that's the question actually - why can it be?
Thanks in advance
EDIT: It appears that MOUSE_DOWN & MOUSE_UP interfere with CLICK event. When I remove listeners to them I get to click handler.How to cause them live together?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我设法捕捉到
CLICK
事件作为MOUSE_DOWN
和MOUSE_DOWN
之间的时间差异。MOUSE_UP
。编辑:短/长解释:由于某种原因,我的
MOUSE_DOWN/MOUSE_UP
事件监听器干扰了MouseEvent.CLICK
。有人建议我放弃监听MouseEvent.CLICK
,而是在MouseEvent.MOUSE_DOWN
中启动计时器,并再次检查MouseEvent.MOUSE_UP< 中的计时器/代码>。如果差异小于 0.1(您可以设置另一个阈值),则实际上是一次点击。
现在是一些示例代码:
免责声明:此代码适用于我,并且适合我和我的应用程序需求。如果您不同意这种方式或者有更好的方式建议,请在评论中提出。由于这个答案回答了我自己的问题,并且添加此编辑只是因为我被要求这样做,所以如果您不同意它,请不要投票。
I managed to catch
CLICK
event as timing difference betweenMOUSE_DOWN
&MOUSE_UP
.EDIT: A short/long explanation: For some reason my listeners for
MOUSE_DOWN/MOUSE_UP
events were interfering withMouseEvent.CLICK
. I was suggested by somebody to give up on listening toMouseEvent.CLICK
and instead start a timer inMouseEvent.MOUSE_DOWN
and check again the timer inMouseEvent.MOUSE_UP
. If the difference less than 0.1 (you can put there another threshold) then it was a click actually.Now some sample code:
DISCLAIMER: This code works for me and good for me and my application needs. If you disagree with this way or have a better way to suggest, please do it in comments. Since this answer answers my own question and this EDIT was added only because I was asked to do so, please don't downvote it if you don't agree with it.
像这样调用超类方法
call the superclass method like so
如果我理解正确,您只需要确保在您的重写处理程序中通过调用 super - 来结束每个处理程序
If I'm understanding you correctly, you just need to make sure that in your overriden handlers you end each one with a call to super -
我不是 100% 确定你在问什么,但我会重写这样的点击操作..
基类:
和扩展类:
仔细检查你的代码后:
你已经为一个函数添加了两个不同的侦听器,它们不应该具有单独的函数,因为它们是由对比事件调用的吗?
另外,根据下面的代码判断,您似乎希望通过单击事件触发handleMouse,但事实并非如此。
要么是这样,要么您忘记覆盖
handleClick
- 这是您在点击事件上运行的方法。您可能需要添加此代码:I'm not 100% sure what you're asking, but I'd override click actions like this..
Base class:
And the extending class:
After closely inspecting your code:
You've added two different listeners to one function for one, shouldn't these have seperate functions as they are called by contrasting events?
Also, it seems like you're expecting handleMouse to be fired by a click event judging by the below code, which isn't the case.
Either that, or you're forgetting to override
handleClick
as well - which is the method that you're running on a click event. You possibly need to add this code: