JavaScript 错误?按向上和向左箭头键时没有空格键事件
我正在开发一个小游戏,我尝试接收来自向上、向下、向左、向右和空格键的按键输入。
在 keyDown 上我将一些布尔值设置为 true 并在 keyDown 上再次设置为 false。在主循环中,我检查这些布尔值,以便角色可以向 8 个方向之一移动并同时射击。
一切正常:东北 + 空间,东南 + 空间,西南 + 空间 但西北+空间不起作用。空格键的事件不会被触发。
function onKeyDown(evt){
switch(evt.keyCode){
case up:
pressedUp=true;
break;
case right:
pressedRight = true;
break;
case down:
pressedDown = true;
break;
case left:
pressedLeft = true;
break;
case space:
spacePressed=true;
break;
}
updateCamera();
}
function onKeyUp(evt){
switch(evt.keyCode){
case up:
pressedUp=false;
break;
case right:
pressedRight = false;
break;
case down:
pressedDown = false;
break;
case left:
pressedLeft = false;
break;
case space:
spacePressed = false;
break;
}
}
I'm developing a little game and I try to receive the key input from arrow up, down,left, right and space.
On keyDown i set some booleans to true und on keyDown to false again. In the main loop I check those booleans, so the character can move in 1 of 8 directions and shoot at the same time.
Everything works: North-East + Space, South-East + Space, South-West + Space
but North-West + Space doesn't work. The event for the space bar doesn't get fired.
function onKeyDown(evt){
switch(evt.keyCode){
case up:
pressedUp=true;
break;
case right:
pressedRight = true;
break;
case down:
pressedDown = true;
break;
case left:
pressedLeft = true;
break;
case space:
spacePressed=true;
break;
}
updateCamera();
}
function onKeyUp(evt){
switch(evt.keyCode){
case up:
pressedUp=false;
break;
case right:
pressedRight = false;
break;
case down:
pressedDown = false;
break;
case left:
pressedLeft = false;
break;
case space:
spacePressed = false;
break;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我做了一些测试,在所有浏览器以及 Flash 和 AIR 应用程序中都有相同的行为,所以对我来说,这很可能是由我的键盘(型号:Cherry JK-0100DE)引起的。
我的解决方法是使用另一个键而不是空格键 - 可能使用 X 或 Y。
I did some tests and I have the same behaviour throughout all browsers as well as in Flash and AIR applications, so for me it's very likely caused by my Keyboard (Model: Cherry JK-0100DE).
My workaround is to use another key instead of Space - probably going with X or Y.