正在按下JavaScript过程设备触摸屏
问题描述:
我正在制作一个JavaScript帆布游戏,并且我需要检测设备上的触摸屏是否被扣下来以便轻拍。我有工作去接触,但我不知道如何在按住时调用方法。正在按下JavaScript过程设备触摸屏
所以我添加事件handelers
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()
,然后调用clearInterval()
上touchEnd
。