Javascript 在固定时间后停止等待输入
我是网络编程新手,我偶然发现了一个我无法解决的问题,我不确定它是否能解决。我正在使用jquery创建一个非常简单的“游戏”,我想让线程停止等待(keydown)输入,继续执行代码,这样我就可以执行简单的向上“跳转”或“左跳转”/“右跳转”。可以吗?Javascript 在固定时间后停止等待输入,javascript,jquery,Javascript,Jquery,我是网络编程新手,我偶然发现了一个我无法解决的问题,我不确定它是否能解决。我正在使用jquery创建一个非常简单的“游戏”,我想让线程停止等待(keydown)输入,继续执行代码,这样我就可以执行简单的向上“跳转”或“左跳转”/“右跳转”。可以吗? 下面是我到目前为止所做工作的代码位: 我想您正在寻找setTimeout() 像这样: setTimeout(function(){ // do something here which will be executed after 5 sec
下面是我到目前为止所做工作的代码位:
我想您正在寻找
setTimeout()
像这样:
setTimeout(function(){
// do something here which will be executed after 5 seconds.
},5000);
您不能停止javascript中的线程,因为它是单线程的 你需要一个游戏循环,它独立于你的按键处理程序运行。否则,您可能侵入keydown处理程序的任何动画可能会在不再进行输入时停止 通过查看您的代码,我可以看到您试图通过在这些按键上创建一个新的setTimeout()来实现这一点。您正在为每一个触发的keydown事件创建它。如果引擎没有意识到您在一次又一次地创建相同的超时,则很可能在某个时候崩溃/冻结浏览器 这样做:在onkeydown处理程序中,您仅将变量
keydowncode
设置为keycode值。然后创建一个像这样的新游戏循环
<script>
var keydownCode = 0;
var isInAnimation = false;
var animatedKeydownCode = 0;
var animationStartTime = 0;
var animationStartValue = 0;
// Lightweight keydown handler:
$(document).keydown(function(key) {
keydownCode = parseInt(key.which,10);
}
$(document).keyup(function(key) {
keydownCode = 0;
}
function animation() {
// here comes your animation logic..
// e.g. keep moving for 100 to 200 milliseconds after keypress
// Milli secs difference from
var nowTime = Date.now();
// Animations get prioritized: Only one animation at the same time!
if(isInAnimation) {
switch(animatedKeydownCode) {
case 37:
var delta = (nowTime-animationStartTime)/100*10;
if(delta > 10) { delta = 10; isInAnimation = false; }; // Animation over!
$('img').left(animationStartValue-delta);
case 37:
var delta = (nowTime-animationStartTime)/200*10;
if(delta > 10) { delta = 10; isInAnimation = false; }; // Animation over!
$('img').top(animationStartValue-delta);
case 39:
var delta = (nowTime-animationStartTime)/100*10;
if(delta > 10) { delta = 10; isInAnimation = false; }; // Animation over!
$('img').left(animationStartValue+delta);
}
// Ready to take new input, if its not outdated
} else {
// If some key is down and no animations active
if(keydownCode > 0) {
switch(keydownCode) {
case 37:
animationStartTime = nowTime;
animatedKeydownCode = keydownCode;
animationStartValue = $('img').left();
isInAnimation = true;
case 38:
// Only start a jump if on bottom
if($('img').css("top") == '390px') {
animationStartTime = nowTime;
animatedKeydownCode = keydownCode;
animationStartValue = $('img').top();
isInAnimation = true;
}
case 39:
animationStartTime = nowTime;
animatedKeydownCode = keydownCode;
animationStartValue = $('img').left();
isInAnimation = true;
}
}
}
window.requestAnimationFrame(animation);
}
window.requestAnimationFrame(animation);
</script>
var-keydownCode=0;
var isInAnimation=false;
var animatedKeydownCode=0;
var animationStartTime=0;
var animationStartValue=0;
//轻量级键控处理程序:
$(文档).keydown(函数(键){
keydownCode=parseInt(key.which,10);
}
$(文档).keyup(函数(键){
keydownCode=0;
}
函数动画(){
//这是你的动画逻辑。。
//例如,按键后保持移动100至200毫秒
//毫秒差于
var nowTime=Date.now();
//动画获得优先级:同一时间只有一个动画!
如果(isInAnimation){
开关(animatedKeydownCode){
案例37:
var delta=(nowTime animationStartTime)/100*10;
如果(delta>10){delta=10;isInAnimation=false;};//动画结束!
$('img').left(animationStartValue增量);
案例37:
var delta=(nowTime animationStartTime)/200*10;
如果(delta>10){delta=10;isInAnimation=false;};//动画结束!
$('img').top(animationStartValue增量);
案例39:
var delta=(nowTime animationStartTime)/100*10;
如果(delta>10){delta=10;isInAnimation=false;};//动画结束!
$('img')。左(animationStartValue+delta);
}
//准备接受新输入(如果不是过时的)
}否则{
//如果某个关键点已关闭且没有活动动画
如果(按键下降代码>0){
开关(按键关闭代码){
案例37:
animationStartTime=nowTime;
animatedKeydownCode=keydownCode;
animationStartValue=$('img').left();
isInAnimation=true;
案例38:
//只有在底部时才开始跳跃
if($('img').css(“top”)=='390px'){
animationStartTime=nowTime;
animatedKeydownCode=keydownCode;
animationStartValue=$('img').top();
isInAnimation=true;
}
案例39:
animationStartTime=nowTime;
animatedKeydownCode=keydownCode;
animationStartValue=$('img').left();
isInAnimation=true;
}
}
}
window.requestAnimationFrame(动画);
}
window.requestAnimationFrame(动画);
这不是一个完整的游戏,你需要自己调整它以获得一个正常的游戏。例如,你可能想在没有输入的情况下增加一些重力,让马里奥再次下来。这不是你问题的答案,但你确定要将DOM用于游戏。你考虑过使用画布吗?你的答案确实有帮助,但你需要做的一件事是你的答案中遗漏了一个我更感兴趣的答案:如何创造重力,如何知道是否没有输入?另外,我试着运行你的代码,但它不起作用,我知道我问了很多问题,但是你能看一看,或者编辑一下让它起作用吗?那真的很棒!这里是:提前谢谢,你已经帮了很多忙了。