0

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?

Unmitigated
  • 76,500
  • 11
  • 62
  • 80
Chris P
  • 2,059
  • 4
  • 34
  • 68
  • 1
    " But if I remove the alert("Space pressed") I have errors in my code" - what errors would that be? What is the error message? – mbojko Mar 31 '19 at 11:43
  • The main error i suppose is that the tetris piece is not posiotionied in the right place. – Chris P Mar 31 '19 at 11:45
  • The code is a lot of lines. I can't paste all here. – Chris P Mar 31 '19 at 11:47
  • Can you paste relevant code somewhere else and give a link here? –  Mar 31 '19 at 11:51
  • If I must guess... `clearInterval` isn't called until after you dismiss the alert (and without the alert, it's called immediately). Probably you need at least one or more execution of whatever the interval executes, to place the bricks right. – mbojko Mar 31 '19 at 11:51
  • @ChrisP is this script is appended from the head tag? that could be the problem, place it in the head tag – weegee Mar 31 '19 at 11:56
  • Yes, it's in the head tag and inside document.addEventListener('DOMContentLoaded', function(){ – Chris P Mar 31 '19 at 11:58
  • 1
    *"The main error i suppose is that the tetris piece is not posiotionied in the right place."* That's a bug, not an error. Errors are generated by the interpreter/engine, are of a specific type, and are accompanied by a message. – ziggy wiggy Mar 31 '19 at 12:13
  • if it's in `document.addEventListener('DOMContentLoaded', function(){` then you might wanna see this link https://stackoverflow.com/questions/39993676/code-inside-domcontentloaded-event-not-working – weegee Mar 31 '19 at 12:13
  • You haven't provided any code that demonstrates a problem. – ziggy wiggy Mar 31 '19 at 12:14

1 Answers1

0
document.getElementsByName("start")[0].onclick = function() {
        this.blur();
        game_status = "started";
        interval_time = 300;
        game_over_rect.setAttribute("display","none");
        game_over_text.setAttribute("display","none");
        if(document.getElementsByName("start")[0].value=="Εκκίνηση"){
            initialize_squares_array();
            part_selected=0;
            new_part();
            console.log(234);
        }else{
            game_interval = setInterval(move_tetris_part,interval_time);
        }

    };

The answer is blur the focused button.

Chris P
  • 2,059
  • 4
  • 34
  • 68