取消对焦开始按钮

时间:2019-03-31 11:37:50

标签: javascript settimeout

这是我的代码:

document.onkeydown = function(event){
          switch(event.keyCode){
case 32:
                //space
                alert("Space pressed");
                clearInterval(game_interval);
                game_status = "paused";
                break;
}}

使用alert()函数,一切顺利。 但是,如果我删除alert("Space pressed"),则我的代码中有错误。 错误是游戏没有停止。 有帮助吗?

P.S。该代码用于俄罗斯方块游戏。 我希望当用户按下空格键时,俄罗斯方块部分将尽可能向下移动。

预先感谢, 克里斯·帕帕斯(Chris Pappas)

**我试图将clearInteval放在setTimeout函数中,但没有区别。

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);


    }
}

上面的函数被定期调用。

我发现了错误!!! 当我按下开始按钮开始游戏时,当我按下空格键时,按钮仍处于聚焦状态。 当我按下空格时如何停止在开始按钮上触发点击事件?

1 个答案:

答案 0 :(得分: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);
        }

    };

答案是模糊焦点按钮。