【问题标题】:Unfocus start button散焦开始按钮
【发布时间】:2019-03-31 11:37:49
【问题描述】:

这是我的代码:

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


    }
}

上述函数是周期性调用的。

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

【问题讨论】:

  • " 但是如果我删除警报("Space press") 我的代码中有错误" - 那会是什么错误?错误信息是什么?
  • 我想主要的错误是俄罗斯方块没有放在正确的位置。
  • 代码很多行。我不能在这里全部粘贴。
  • 您可以将相关代码粘贴到其他地方并在此处提供链接吗?
  • “我认为主要的错误是俄罗斯方块没有放在正确的位置。” 这是一个错误,而不是错误。错误由解释器/引擎生成,属于特定类型,并伴有一条消息。

标签: javascript settimeout


【解决方案1】:
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);
        }

    };

答案是模糊焦点按钮。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-07
    • 2021-09-03
    • 1970-01-01
    相关资源
    最近更新 更多