Javascript jQuery移动多个事件,如keyup/keydown

Javascript jQuery移动多个事件,如keyup/keydown,javascript,jquery,jquery-mobile,mobile,multi-touch,Javascript,Jquery,Jquery Mobile,Mobile,Multi Touch,我目前正在开发一款适用于移动和非移动的html5游戏 移动用户界面现在的工作方式是,当你按住屏幕右侧时,角色向右移动。按住左端,角色向左移动。点击屏幕中央进行拍摄,然后向上滑动进行跳跃。所有这些都很好 但是,我想让它在移动到右边的同时,用户也可以点击屏幕进行拍摄,或者向上滑动进行跳跃。例如,在非移动版本中,按住向右箭头键的同时,如果同时按下顶部箭头,则向右移动时将跳转。这些事件不会相互阻碍。然而,触摸事件似乎一次只能触发一个。如果我握住右侧跑步,然后轻触,我将停止跑步。事实上,我也不会射击,所以

我目前正在开发一款适用于移动和非移动的html5游戏

移动用户界面现在的工作方式是,当你按住屏幕右侧时,角色向右移动。按住左端,角色向左移动。点击屏幕中央进行拍摄,然后向上滑动进行跳跃。所有这些都很好

但是,我想让它在移动到右边的同时,用户也可以点击屏幕进行拍摄,或者向上滑动进行跳跃。例如,在非移动版本中,按住向右箭头键的同时,如果同时按下顶部箭头,则向右移动时将跳转。这些事件不会相互阻碍。然而,触摸事件似乎一次只能触发一个。如果我握住右侧跑步,然后轻触,我将停止跑步。事实上,我也不会射击,所以这两个项目似乎只是相互抵消了

我不确定这是否是一个语义错误,或者这是不可能的,但是任何建议/帮助都将不胜感激。如果用jQuery事件无法做到这一点,也许有一种创造性的方法可以用JS或框架实现这一点

以下是事件的代码(如果有帮助的话):

$(文档).on('touchstart','#game',函数(e){
//触摸的x坐标
var touchX=e.originalEvent.touch[0].pageX;
`在此处输入代码`//我们正在触摸画布的哪一部分?
var leftSide=touchX=.90*width;//右侧为宽度的10%
if(左侧| |右侧){
//如果他们在跳跃或射击,除非他们在空中,否则他们只能跳跃
如果((player.currentAction!==“跳跃”| |(player.currentAction===“跳跃”&(player.y===地面位置| | | |!无障碍碰撞(player)))){
var作用;
if(leftSide)action='向左移动';
如果(右侧)操作=‘向右移动’;
玩家。动作(动作);
player.lastAction=动作;
//不要添加重复动作,因为按下键时会不断调用keyup
如果(!inArray(player.currentActions,action)){
player.currentActions.push(动作);
}
}
}
});
//释放tap时,更新当前操作
$(文档).on('touchend','#game',function(){
//如果我们向左或向右移动,松开钥匙时停止
//还要确保我们这样做的时候不是在地面上就是在平台上
//我们不想打破半空中轨道
var movingLeftOrRight=player.vx!==0;
如果(移动左或右&(!无障碍碰撞(玩家)| |玩家.y==地面位置)){
player.action('standing');//停止player
}
//删除与密钥关联的操作
如果(player.vx>0)从数组中移除(player.currentActions,“向右移动”);
如果(player.vx<0)从数组中移除(player.currentActions,“向左移动”);
如果(movingLeftOrRight)player.lastAction=null;
});
//向上滑动时向上跳
$(文档).on('swipeup','#game',function(){
如果((player.currentAction!==“跳跃”| |(player.currentAction===“跳跃”&(player.y===地面位置| | | |!无障碍碰撞(player))))
玩家。动作(“跳跃”);
});
//在中间屏幕点击时点火(非触摸启动,以避免与跳跃冲突)
$(文档).on('tap','#game',函数(e){
var leftSide=e.pageX=.90*width;//右侧宽度的10%
如果(!(左侧|右侧)){
玩家。动作(“射击”);
}
});

谢谢大家!

我认为您需要一个用于多点触摸手势的javascript库。值得一看-hammer.js
$(document).on('touchstart', '#game', function(e) {
                        //x coordinate of touch
                        var touchX = e.originalEvent.touches[0].pageX;
                    `enter code here`    //what part of the canvas are we touching?
                        var leftSide = touchX <= .10 * width; //left 10% of width
                        var rightSide = touchX >= .90 * width; //right 10% of width
                        if (leftSide || rightSide) {
                            //if they are jumping or shooting they cant do anything but jump unless they are in the air
                            if ((player.currentAction !== 'jumping' || (player.currentAction === 'jumping' && (player.y === groundLocation || !noObstacleCollisions(player))))) {
                                var action;
                                if (leftSide) action = 'moving left';
                                else if (rightSide) action = 'moving right';
                                player.action(action);
                                player.lastAction = action;
                                //dont add duplicate actions, since keyup is constantly called when held down
                                if (!inArray(player.currentActions, action)) {
                                    player.currentActions.push(action);
                                }
                            }
                        }
                    });
                    //when tap is released update current actions
                    $(document).on('touchend', '#game', function() {
                        //if we're moving left or right stop when we let go of the key
                        //also make sure we're either on the ground or on a platform when we do that
                        //we dont want to break midair trajectory
                        var movingLeftOrRight = player.vx !== 0;
                        if (movingLeftOrRight && (!noObstacleCollisions(player) || player.y === groundLocation)) {
                            player.action('standing'); //stop player
                        }
                        //remove action associated with key
                        if (player.vx > 0) removeFromArray(player.currentActions, 'moving right');
                        if (player.vx < 0) removeFromArray(player.currentActions, 'moving left');
                        if (movingLeftOrRight) player.lastAction = null;
                    });
                    //jump up when swiping up
                    $(document).on('swipeup', '#game', function() {
                        if ((player.currentAction !== 'jumping' || (player.currentAction === 'jumping' && (player.y === groundLocation || !noObstacleCollisions(player)))))
                            player.action('jumping');
                    });
                    //fire on middle screen tap (not touchstart to not conflict with jumping)
                    $(document).on('tap', '#game', function(e) {
                        var leftSide = e.pageX <= .10 * width; //left 10% of width
                        var rightSide = e.pageX >= .90 * width; //right 10% of width
                        if (!(leftSide || rightSide)) {
                            player.action('shooting');
                        }
                    });