【问题标题】:JavaScript process device touchscreen being held downJavaScript 处理设备触摸屏被按住
【发布时间】:2017-10-05 04:41:06
【问题描述】:

我正在制作一个 JavaScript 画布游戏,我需要检测设备上的触摸屏是否被按住而不是被点击。我让它开始触摸,但我不知道如何在按住方法时继续调用它。

所以我添加了事件处理程序

function addTouchEventHandlers() 
{      
screen.canvas.addEventListener( 'touchstart', touchStart);      
screen.canvas.addEventListener( 'touchend', touchEnd );   
}

我有触摸开始和触摸结束

function touchStart(e) {      
if (gameStarted) {         
// Prevent players from inadvertently          
// dragging the game canvas         
e.preventDefault();       
}};

function touchEnd(e) {      
    var x = e.changedTouches[0].pageX;  
    var y = e.changedTouches[0].pageY; 

    if (gameStarted) {         
        if (x > screen.width/2) 
        {            
           processRightTap();            
        }         
        else if (x < screen.width/2 && y < screen.height/2) 
        {                 
           processTopLeftTap();   <- I want to keep calling this method when held down     
        }
        else if (x < screen.width/2 && y > screen.height/2) 
        {                       
           processBottomLeftTap();  <- I want to keep calling this method when held down           
        }  
        // Prevent players from double         
       // tapping to zoom into the canvas         
        e.preventDefault();       
}   
};

方法只是改变我的精灵的位置

function processBottomLeftTap() {      
 ship.y += 4;
}

function processTopLeftTap() {      
ship.y -= 4;
}

他们的东西类似于键盘方法isPressed和isDown吗? 或者我将如何围绕该方法创建一个while循环? 类似的东西。

while(The screen is being touched at X position)
{
processTopLeftTap();
}

【问题讨论】:

    标签: javascript android ios device touchscreen


    【解决方案1】:

    您可以在touchStart 中使用setInterval(),在回调中使用processTopLeftTap(),然后在touchEnd 上调用clearInterval()

    【讨论】:

      猜你喜欢
      • 2023-03-11
      • 1970-01-01
      • 2011-04-27
      • 1970-01-01
      • 1970-01-01
      • 2014-09-02
      • 2017-07-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多