通过按键移动元素(多个)
对角线移动不起作用,并在左长按/右同时发出问题,
但是在双击按键时,船变得疯狂!
$(document).bind('keydown', function(e) {
var box = $("#plane"),
left = 37,
up = 38,
right = 39,
down = 40
if (e.keyCode == left) {
box.animate({left: "-=5000"},3000);
}
if (e.keyCode == up) {
box.animate({top: "-=5000"},3000);
}
if (e.keyCode == right) {
box.animate({left:"+=5000"},3000);
}
if (e.keyCode == down) {
box.animate({top: "+=5000"},3000);
}
});
$(document).bind('keyup', function() {
$('#plane').stop();
});
Diagonal move not working and issue on left-longPress/right simultaneous
But on double keypress the ship goes crazy!
$(document).bind('keydown', function(e) {
var box = $("#plane"),
left = 37,
up = 38,
right = 39,
down = 40
if (e.keyCode == left) {
box.animate({left: "-=5000"},3000);
}
if (e.keyCode == up) {
box.animate({top: "-=5000"},3000);
}
if (e.keyCode == right) {
box.animate({left:"+=5000"},3000);
}
if (e.keyCode == down) {
box.animate({top: "+=5000"},3000);
}
});
$(document).bind('keyup', function() {
$('#plane').stop();
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我搞乱了类似的事情,这是我遇到的一个有效的解决方案。
Demo
延迟的问题似乎是大多数浏览器将采用第一个输入(在 keyDown 上)然后他们在一遍又一遍地运行这些功能之前有半秒的延迟。如果我们不记得按下按键时的功能,而是有一个时间间隔检查一个关联数组,在该数组中存储按下的键,这似乎可以平滑移动。它还允许同时按下多个键,这意味着对角线移动。然后我们将使用 keyup 事件删除相应的键。
在此解决方案中,您有两种方法来管理正在移动的元素的速度。
我发现 20 毫秒的间隔频率可以让您实现相当平滑的运动。
我意识到这是一个非常古老的线程,但我想无论如何我都会做出贡献。
I messed around with a similar thing and here's a solution I came across that works.
Demo
The problem with the delay seems to be that most browsers will take the first input (on keyDown) and then they have half a second delay before running the functions over and over. If we don't recall the function on keydown, but rather have an interval checking an associative array where we store what keys are being held down, that seems to smooth out movement. It also allows for multiple keys to be pressed at once, which means diagonal movement. Then we'll remove the respective keys by using the keyup event.
In this solution you have two ways of managing the speed of the element you're moving.
I find that 20 ms on the interval frequency gives you fairly smooth movement.
I realize that this is a really old thread, but I figured I'd contribute anyway.
关于该间隔,
http://jsfiddle.net/fbFuW/21/
about that interval,
http://jsfiddle.net/fbFuW/21/
在游戏开发中,最好在键盘周围创建一个包装器。
这样的包装器应该实现以下内容:
Event.preventDefault()
来防止浏览器执行默认操作(例如,在箭头键上滚动页面),keydown
事件是否不是Event.repeat
Event。关键
Event.key
) 触发的函数操作,迭代该组按下的键并调用相关操作函数修复对角玩家移动,您可以通过两种方式实现,使用旋转角度,或者将玩家对角移动
1 / Math.sqrt(2)
(大约~ 0.7071
代码>) 距离。在下面的示例中,我将使用角度(以弧度为单位):In game development it's best to create a wrapper around the keyboard.
Such wrapper should implement the following:
Event.preventDefault()
to prevent the browser doing default stuff (like i.e. page scrolling on arrow keys)keydown
event is not anEvent.repeat
Event.key
Event.key
)To fix the diagonal player movement, you can do it in wo ways, using an angle of rotation, or move the player diagonally by
1 / Math.sqrt(2)
(approximately~ 0.7071
) distance. In the following example I'll use angles (in radians):