Javascript 当触摸屏激活时,onmousedown和onmouseup不工作

Javascript 当触摸屏激活时,onmousedown和onmouseup不工作,javascript,html,onclick,onmousedown,onmouseup,Javascript,Html,Onclick,Onmousedown,Onmouseup,我的网页上有两个按钮,用来作为游戏的触摸屏控件,每个按钮在按下时都会朝一个方向移动,它们都完全按照预期的方式操作,但只有在电脑上的鼠标点击它们时才可以。当点击触摸屏时,所有其他按钮都可以工作,但没有一个使用onmousedown和onmousedup。有人知道发生了什么事以及如何解决吗 左边 正确的 var canvas=document.getElementById(“myCanvas”); var b=document.getElementById(“b”); var ctx=canv

我的网页上有两个按钮,用来作为游戏的触摸屏控件,每个按钮在按下时都会朝一个方向移动,它们都完全按照预期的方式操作,但只有在电脑上的鼠标点击它们时才可以。当点击触摸屏时,所有其他按钮都可以工作,但没有一个使用onmousedown和onmousedup。有人知道发生了什么事以及如何解决吗



左边 正确的 var canvas=document.getElementById(“myCanvas”); var b=document.getElementById(“b”); var ctx=canvas.getContext(“2d”); var x=canvas.width/2; 变量y=画布高度-40; var-dx=2; var-dy=-2; var-ballRadius=10; 高度=10; 宽度=75; var-pallex=(canvas.width-pallewidth)/2; var-rightspressed=false; var leftPressed=false; 风险值难度=10 风险值得分=0 var gameRunning=false var=false 函数pailemoveleft(){pailex-=5; 如果(叶片x<0){ x=0; }} 函数papperMoveRight(){papperX+=5; 如果(划桨X+划桨宽度>画布宽度){ pallex=canvas.width-pallewidth; }} 函数touchleet(){leftPressed=true;} 函数liftLeft(){leftPressed=false;} 函数touchRight(){rightPressed=true;} 函数liftRight(){rightspressed=false;} 函数playGame(){ b、 style.display='block' canvas.style.display='block' 函数playPause(){ 如果(暂停==真){暂停=假} else{paused=true} } 函数{ ctx.beginPath(); 弧(x,y,球半径,0,数学PI*2); ctx.fillStyle=“#0095DD”; ctx.fill(); ctx.closePath(); } 函数drawblade(){ ctx.beginPath(); ctx.rect(桨x,canvas.height-桨高,桨宽,桨高); ctx.fillStyle=“#0095DD”; ctx.fill(); ctx.closePath(); } 函数drawScore(){ ctx.font=“50px”; ctx.fillStyle=“#0095DD”; ctx.fillText(“分数:”+分数,8,20); } 函数绘图(){ 如果(暂停===错误){ clearRect(0,0,canvas.width,canvas.height); 牵引杆(); 抽桨(); drawScore(); x+=dx; y+=dy; 如果(rightM=true){mapperMoveRight();} 如果(leftM=true){moveleft();} 如果(x+dx>canvas.width-ballRadius | | x+dx画布高度和半径){ 如果(x>桨叶x&&x<桨叶x+桨叶宽度){ dy=-dy; 难度=难度+1.5; 分数=分数+1; } 否则{ x=画布宽度/2; y=画布高度-40; dy=-dy; 警惕(“游戏结束!你得分”+得分); clearRect(0,0,canvas.width,canvas.height); 间隔时间; 得分=0; canvas.style.display='none'; b、 style.display='none'; } } 如果(右键按下){ 右键(); } else if(左键按下){ 左移(); } } } 文档。addEventListener(“keydown”,keyDownHandler,false); 文件。addEventListener(“keyup”,keyUpHandler,false); 函数keyDownHandler(e){ 如果(e.key==“Right”| e.key==“ArrowRight”){ rightspressed=true; } else if(e.key==“Left”| | e.key==“ArrowLeft”){ leftPressed=true; } } 函数keyUpHandler(e){ 如果(e.key==“Right”| e.key==“ArrowRight”){ 右压=假; } else if(e.key==“Left”| | e.key==“ArrowLeft”){ leftPressed=false; } } var interval=setInterval(绘图,难度);} 隐藏或显示游戏
暂停/玩游戏





播放“保持活动状态”
你需要使用ontouchstart和ontouchend来代替onmousedown和onmouseup。

看看这篇文章