JavaScript 处理设备触摸屏被按住

JavaScript process device touchscreen being held down

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

所以我添加了事件处理器

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();       
}   
};

方法简单地改变了我的精灵 y 位置

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

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

它们是不是类似于键盘方法isPressed和isDown? 或者我将如何围绕该方法创建一个 while 循环? 像这样。

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

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