[英]JavaScript process device touchscreen being held down
I am making a JavaScript canvas game and I need to detect if the touchScreen on a device is being held down as apposed to being tapped. 我正在做一个JavaScript画布游戏,我需要检测是否已按下设备上的touchScreen。 I have it working to pick up a touch but I cant figure out how to keep calling a method while it is held down.
我正在努力进行触摸,但是我想不出如何在按住该方法的同时继续调用该方法。
So I add the event handelers 所以我添加了事件处理程序
function addTouchEventHandlers()
{
screen.canvas.addEventListener( 'touchstart', touchStart);
screen.canvas.addEventListener( 'touchend', touchEnd );
}
I have touch start and touch end 我有触摸开始和触摸结束
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();
}
};
The Methods simply change my sprites y position 方法只是改变我的精灵的位置
function processBottomLeftTap() {
ship.y += 4;
}
function processTopLeftTap() {
ship.y -= 4;
}
Is their something similar to the keyboard method isPressed and isDown? 它们类似于键盘方法isPressed和isDown吗? or how would I create a while loop around the method?
或如何围绕该方法创建一个while循环? something like this.
这样的事情。
while(The screen is being touched at X position)
{
processTopLeftTap();
}
您可以在touchStart
使用setInterval()
,在touchStart
使用processTopLeftTap()
,然后在touchEnd
上调用clearInterval()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.