取消焦点开始按钮



这是我的代码:

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

有了alert()功能,一切顺利。但是如果我删除alert("Space pressed")我的代码中有错误。错误是游戏没有停止。有什么帮助吗?

附言此代码适用于俄罗斯方块游戏。我希望当用户按下空格按钮时,俄罗斯方块部分将尽可能向下移动。

提前感谢,克里斯·帕帕斯

** 我试图将 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);

    }
}

上述函数会定期调用。

我发现了错误!!当我按下开始按钮开始游戏时,当我按下空格按钮时,按钮仍然聚焦。当我按下空格时,如何停止触发开始按钮上的单击事件?

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

答案是模糊聚焦按钮。

最新更新