我的目标是,当你触摸屏幕的左半部分时,它会移动一个物体向左移动,当触摸右半部分时移动,当屏幕未受影响时根本不移动。
尝试
window.addEventListener('touchstart', function (e) {
e.preventDefault();
var x = e.touches[0].screenX;
if(x < window.innerWidth/2){
chase.dx = -chase.speed;
}
else if(x > window.innerWidth/2){
chase.dx = chase.speed;
}
else {
chase.dx = 0;
}
});
window.addEventListener('touchend', function (e) {
e.preventDefault();
var x = e.touches[0].screenX;
if (x > window.innerWidth/2 && chase.dx < 0)
chase.dx = -chase.speed;
if (x > window.innerWidth/2 && chase.dx > 0)
chase.dx = 0;
if (x < window.innerWidth/2 && chase.dx < 0)
chase.dx = 0;
if (x < window.innerWidth/2 && chase.dx > 0)
chase.dx = chase.speed
});