我正在做俄罗斯方块游戏。
这是jsfiddle的链接。 https://jsfiddle.net/e9q1mykt/1/
问题/错误在于,玩游戏setInterval(move_tetris_part,interval_time);
时被多次调用,因此下降速度高于正常速度。
我找不到原因。
问题出在keydown和keyup功能上。 A有一个名为job_completed的变量,在按下每个按钮的开始时为true,然后为false。仅当此变量为false时,代码才会执行。
document.onkeydown = function(event){
can_move_left = true;
can_move_right = true;
can_rotate = true;
switch(event.keyCode){
case 37:
//left;
if(game_mode=="started" && job_completed){
job_completed = false;
//code there
job_completed = true;
}
}
case 39:
//left;
if(game_mode=="started" && job_completed){
job_completed = false;
//code there
job_completed = true;
}
case 40:
//left;
if(game_mode=="started" && job_completed){
job_completed = false;
//code there
job_completed = true;
}
case 38:
//left;
if(game_mode=="started" && job_completed){
job_completed = false;
//code there
job_completed = true;
}
}
document.onkeyup = function(event){
if(game_mode=="started"){
switch(event.keyCode){
case 37:
//left
if(job_completed){
game_mode = "started";
game_interval = setInterval(move_tetris_part,interval_time);
console.log("Set interval after pressing left arrow");
}
break;
case 38:
//up
if(job_completed){
game_mode = "started";
part_can_move();
if(part_can_go_down){
game_interval = setInterval(move_tetris_part,interval_time);
console.log("Set interval after pressing up arrow");
}else{
new_part();
}
}
break;
case 39:
//right
if(job_completed){
game_mode = "started";
game_interval = setInterval(move_tetris_part,interval_time);
console.log("Set interval after pressing right arrow");
}
//right
break;
case 40:
//down
if(job_completed){
if(game_mode == "started"){
if(interval_time==interval_time_speed){
clearInterval(game_interval);
console.log("Clear interval while pressing down arrow");
interval_time = interval_time_normal;
document.getElementById("interval_time").innerHTML = interval_time+" ms";
game_interval = setInterval(move_tetris_part,interval_time);
console.log("Set interval after pressing down arrow");
}
}
}
break;
case 32:
//space
if(game_mode=="started" && job_completed){
job_completed = false;
console.log(job_completed);
clearInterval(game_interval);
console.log("Clear interval while pressing space arrow");
static_current_y = current_y;
for(var i=static_current_y;i<squares_y;i++){
current_y = i;
part_can_move();
if(part_can_go_down==false){
current_y = static_current_y;
delete_tetris_part();
current_y = i;
make_tetris_part(false);
clear_completed_lines();
score = score+((squares_y-static_current_y)*10);
document.getElementById("score").innerHTML = score;
new_part();
job_completed = true;
console.log(job_completed);
return 1;
}
}
job_completed = true;
console.log(job_completed);
}
break;
}
}
};
还请检查日志(控制台)。
预先感谢, 克里斯·帕帕斯(Chris Pappas)。
答案 0 :(得分:0)
每个game_interval = setInterval(//...
之前都应加上clearInterval
,我认为在第1680行附近的document.onkeyup
处理程序中,您忽略了这一点。
这是原因:时间间隔应持续滴答,直到清除为止(或者用户关闭窗口,或者世界结束-一种或另一种方式,强制停止)。当您呼叫一个setInterval
而不清除已经勾选的那个时,旧的将继续运行,而新的将开始。因此,每x毫秒一次,第一个将滴答作响,而独立每x毫秒一次,第二个滴答也会响起。每x毫秒两次滴答。添加另一个,它是3个刻度,依此类推,依此类推。
顺便说一句,该脚本很大,因此很难阅读。集中精力使其更干燥,将有助于您查找错误。