我最近添加了 jquery.mouse2touch.min.js插件到我的游戏中,测试后我遇到了问题。我的移动系统由两个 div 组成(一个向左移动玩家,一个向右移动),基本上当你按住 div 时,玩家应该朝一个方向移动,当你松开时,他应该停止。问题是,如果我按下太久然后松开,播放器不会停止。
这是让他移动的代码:
// iControl System
var $div = $('#character');
$('#ios-left').mousedown(function(){
leftTimer = setInterval(function(){
processWalk('left');
},100);
}).mouseup(function(){
$('#character').stop();
clearInterval(leftTimer);
}).mouse2touch();
$('#ios-right').mousedown(function(){
rightTimer = setInterval(function(){
processWalk('right');
},100);
}).mouseup(function(){
$('#character').stop();
clearInterval(rightTimer);
}).mouse2touch();
还有一把 fiddle : http://jsfiddle.net/gqfesrhw/
谢谢(如果有人认为这可能是 mouse2touch 插件的问题,我愿意接受其他建议)
最佳答案
<强> Working Fiddle
计时器的问题,您应该在移动之前清除时间间隔。您的全局变量 TimerWalk
和 clearInterval(TimerWalk);
应该很有用。
这是代码块
$('#ios-left').mousedown(function () {
clearInterval(TimerWalk);
TimerWalk = setInterval(function () {
processWalk('left');
}, 100);
}).mouseup(function () {
$div.stop();
clearInterval(TimerWalk);
}).mouse2touch();
$('#ios-right').mousedown(function () {
clearInterval(TimerWalk);
TimerWalk = setInterval(function () {
processWalk('right');
}, 100);
}).mouseup(function () {
$div.stop();
clearInterval(TimerWalk);
}).mouse2touch();
这只是为了正确移动,您可能需要解决其他问题。
关于javascript - iOS 上的 JQuery Mouseup,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26957080/