Here is my code:
document.onkeydown = function(event){
switch(event.keyCode){
case 32:
//space
alert("Space pressed");
clearInterval(game_interval);
game_status = "paused";
break;
}}
With the alert()
function everything goes well.
But if I remove the alert("Space pressed")
I have errors in my code.
The error is the game doesn't stop.
Any help?
P.S. This code is for a tetris game. I want when the user press the space button the tetris part will move as far down as possible.
Thanks in advance, Chris Pappas
** I tried to place clearInteval in setTimeout function, but no difference.
function move_tetris_part(){
//check if part can move down
part_can_move();
if(part_can_go_down==false){
clearInterval(game_interval);
new_part();
}else{
make_help_part();
if(new_piece==false){
delete_tetris_part();
}else{
new_piece = false;
}
current_y = current_y+1;
make_tetris_part(false);
}
}
The above function is called periodically.
I found the bug!!! When i press the begin button to start the game the button is still focused when i press the space button. How can i stop triggering the click event on begin button when i press the space?