【问题标题】:How do I listen for mouse button hold如何收听鼠标按钮按住
【发布时间】:2020-05-08 21:25:23
【问题描述】:

我正在使用 Cocos2d-JS,需要找到一种方法来监听鼠标按键。似乎有限,因为只有 onMousemove、onMouseDown 和 onMouseUp。所有这些都只会被解雇一次。我如何使用它们来检测鼠标按钮何时被按住?我不能只使用 onMouseDown,因为如果单击按钮,它将用于执行操作。

【问题讨论】:

    标签: cocos2d-js


    【解决方案1】:

    我可以为此建议以下解决方案。你可以有一个柜台。 onMouseDown 将启动一个方法,该方法将通过setInterval 增加计数器并检查计数器是否达到目标值。如果达到该值,它将触发您希望通过按住鼠标按钮触发的任何内容。 onMouseUp 将清除间隔计数器。这是简化的代码。假设这些是对象的方法。

    onMouseDown: function() {
        this.launchTimer();
    },
    
    launchTimer: function() {
       //Timer will update every 100ms
       this.counter = 0;
       this.timer = setInterval(() => {
    
           //Assume our target value is 1s or 1000ms
           if (this.counter === 1000) {
               //Launch your function here
               this.onMouseHold();
               this.clearTimer();
             } else {
               this.counter += 100;
             }
           }, 100);
    
        this.timer();
    },
    
    clearTimer: function() {
       this.counter = 0;
       clearInterval(this.timer);
    },
    
    onMouseUp: function() {
       this.clearTimer();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-06-07
      • 2010-09-15
      • 1970-01-01
      • 1970-01-01
      • 2018-03-30
      • 1970-01-01
      • 1970-01-01
      • 2019-09-25
      相关资源
      最近更新 更多