在子类中处理MouseEvent.CLICK、MOUSE_DOWN

发布于 2024-11-07 12:11:41 字数 972 浏览 5 评论 0原文

我正在构建一个按钮面板,它们包含在 Canvas 容器中。为此,我创建了一个 MyButton 类,它是 UIComponent 的子类。 MyButton 类还有另外 2 个子类:MyRadioButtonMyMenuButton,它们在 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. MyMenuButtoncreates 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 技术交流群。

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

发布评论

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

评论(4

逆流 2024-11-14 12:11:41

我设法捕捉到 CLICK 事件作为 MOUSE_DOWNMOUSE_DOWN 之间的时间差异。 MOUSE_UP

编辑:短/长解释:由于某种原因,我的 MOUSE_DOWN/MOUSE_UP 事件监听器干扰了 MouseEvent.CLICK。有人建议我放弃监听 MouseEvent.CLICK ,而是在 MouseEvent.MOUSE_DOWN 中启动计时器,并再次检查 MouseEvent.MOUSE_UP< 中的计时器/代码>。如果差异小于 0.1(您可以设置另一个阈值),则实际上是一次点击。

现在是一些示例代码:

public class MyButton extends UIComponent
{
   ...
   private var _clickTime : Number = 0.;

   public function void MyButton()
   {
       super();
       addEventListener(MouseEvent.MOUSE_DOWN, handleMouse);
       addEventListener(MouseEvent.MOUSE_UP, handleMouse);
   }

   protected function checkForClick(event:MouseEvent) : Boolean
   {
      var down : Boolean = event.type == MouseEvent.MOUSE_DOWN;

      if (down)
         _clickTime = getTimer();
      else {
          var diff : Number  = getTimer() - _clickTime;
          if (diff/1000. < 1.) {
              handleClick();
              return true;
          }
      }
      return false;
   }

   protected function handleClick(event:MouseEvent) : void
   {
       // handle the click here
   }

   protected function handleMouse(event:MouseEvent) : void
   {
       checkForClick(event);
       ... 
       // take care of your button graphic state
   }

免责声明:此代码适用于我,并且适合我和我的应用程序需求。如果您不同意这种方式或者有更好的方式建议,请在评论中提出。由于这个答案回答了我自己的问题,并且添加此编辑只是因为我被要求这样做,所以如果您不同意它,请不要投票。

I managed to catch CLICK event as timing difference between MOUSE_DOWN & MOUSE_UP.

EDIT: A short/long explanation: For some reason my listeners for MOUSE_DOWN/MOUSE_UP events were interfering with MouseEvent.CLICK. I was suggested by somebody to give up on listening to MouseEvent.CLICK and instead start a timer in MouseEvent.MOUSE_DOWN and check again the timer in MouseEvent.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:

public class MyButton extends UIComponent
{
   ...
   private var _clickTime : Number = 0.;

   public function void MyButton()
   {
       super();
       addEventListener(MouseEvent.MOUSE_DOWN, handleMouse);
       addEventListener(MouseEvent.MOUSE_UP, handleMouse);
   }

   protected function checkForClick(event:MouseEvent) : Boolean
   {
      var down : Boolean = event.type == MouseEvent.MOUSE_DOWN;

      if (down)
         _clickTime = getTimer();
      else {
          var diff : Number  = getTimer() - _clickTime;
          if (diff/1000. < 1.) {
              handleClick();
              return true;
          }
      }
      return false;
   }

   protected function handleClick(event:MouseEvent) : void
   {
       // handle the click here
   }

   protected function handleMouse(event:MouseEvent) : void
   {
       checkForClick(event);
       ... 
       // take care of your button graphic state
   }

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.

权谋诡计 2024-11-14 12:11:41

像这样调用超类方法

override protected handleMouse(event:MouseEvent) : void
{
  super.handleMouse(event);
  // subclass specific handling
}

call the superclass method like so

override protected handleMouse(event:MouseEvent) : void
{
  super.handleMouse(event);
  // subclass specific handling
}
丘比特射中我 2024-11-14 12:11:41

如果我理解正确,您只需要确保在您的重写处理程序中通过调用 super - 来结束每个处理程序

override protected handleMouse(event:MouseEvent):void {
// my logic here for the subclass
trace("blah blah blah");

//now redispatch up to the superclass.
super.handleMouse(event);

}

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 -

override protected handleMouse(event:MouseEvent):void {
// my logic here for the subclass
trace("blah blah blah");

//now redispatch up to the superclass.
super.handleMouse(event);

}
笑梦风尘 2024-11-14 12:11:41

我不是 100% 确定你在问什么,但我会重写这样的点击操作..

基类:

public class MyButton extends UIComponent
{
    /**
     * Constructor
     */
    public function MyButton()
    {
        // listeners
        addEventListener(MouseEvent.CLICK, _click);
        // ...other listeners
    }

    /**
     * Called on dispatch of MouseEvent.CLICK
     */
    private function _click(e:MouseEvent):void
    {
        doClick();
    }

    /**
     * Override this method and fill with actions to
     * do when this is clicked
     */
    protected function doClick():void
    {
        trace("base class was clicked");
    }
}

和扩展类:

public class MyOtherButton extends MyButton
{
    /**
     * Override doClick
     */
    override protected function doClick():void
    {
        super.doClick();
        trace("extending class was clicked");
    }
}

仔细检查你的代码后:

你已经为一个函数添加了两个不同的侦听器,它们不应该具有单独的函数,因为它们是由对比事件调用的吗?

this.addEventListener(MouseEvent.MOUSE_DOWN, handleMouse);
this.addEventListener(MouseEvent.MOUSE_UP, handleMouse);

另外,根据下面的代码判断,您似乎希望通过单击事件触发handleMouse,但事实并非如此。

override protected handleMouse(event:MouseEvent) : void
{
  // subclass specific handling
}

要么是这样,要么您忘记覆盖 handleClick - 这是您在点击事件上运行的方法。您可能需要添加此代码:

override protected handleClick(event:MouseEvent) : void
{
  // subclass specific handling for a click
}

I'm not 100% sure what you're asking, but I'd override click actions like this..

Base class:

public class MyButton extends UIComponent
{
    /**
     * Constructor
     */
    public function MyButton()
    {
        // listeners
        addEventListener(MouseEvent.CLICK, _click);
        // ...other listeners
    }

    /**
     * Called on dispatch of MouseEvent.CLICK
     */
    private function _click(e:MouseEvent):void
    {
        doClick();
    }

    /**
     * Override this method and fill with actions to
     * do when this is clicked
     */
    protected function doClick():void
    {
        trace("base class was clicked");
    }
}

And the extending class:

public class MyOtherButton extends MyButton
{
    /**
     * Override doClick
     */
    override protected function doClick():void
    {
        super.doClick();
        trace("extending class was clicked");
    }
}

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?

this.addEventListener(MouseEvent.MOUSE_DOWN, handleMouse);
this.addEventListener(MouseEvent.MOUSE_UP, handleMouse);

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.

override protected handleMouse(event:MouseEvent) : void
{
  // subclass specific handling
}

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:

override protected handleClick(event:MouseEvent) : void
{
  // subclass specific handling for a click
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文