Javascript 画布自动运行开关窗口单击?

Javascript 画布自动运行开关窗口单击?,javascript,canvas,Javascript,Canvas,单击另一个窗口并返回时,画布自动运行 我添加了关键侦听器,当您单击wasd或向上,左,右,向下时,这些侦听器会使飞船移动 这一切都很好,直到你点击两个按钮,如向上和向右,然后点击另一个选项卡或窗口,然后回来。然后它会继续移动,而不是听你的输入 我认为问题是,当你点击离开页面时,画布永远无法检查键是否未按下。但我如何让所有键在你点击离开屏幕时表现为未按下? 当你离开屏幕时,敌人也会消失 按下按钮时会发生以下情况: Player.prototype.checkKeys = function() //

单击另一个窗口并返回时,画布自动运行

我添加了关键侦听器,当您单击
w
a
s
d
向上
向下
时,这些侦听器会使飞船移动

这一切都很好,直到你点击两个按钮,如向上和向右,然后点击另一个选项卡或窗口,然后回来。然后它会继续移动,而不是听你的输入

我认为问题是,当你点击离开页面时,画布永远无法检查键是否未按下。但我如何让所有键在你点击离开屏幕时表现为未按下? 当你离开屏幕时,敌人也会消失 按下按钮时会发生以下情况:

Player.prototype.checkKeys = function() //these functions are in the PLayer class
{


    if(this.isUpKey == true)//if true 
{
    if(Player1.drawY >= 0)
  {
 this.drawY -= this.Speed; 
  }
}

if(this.isRightKey == true)
{

 if(Player1.drawX <= (canvasWidthShips - this.playerWidth))
  {
  this.drawX += this.Speed;
  }
}

if(this.isDownKey == true)
{
  if(Player1.drawY <= (canvasHeightShips - this.playerHeight))
  {
  this.drawY += this.Speed;
  }
}

if(this.isLeftKey == true)
{
if(Player1.drawX >= 0)
 {
  this.drawX -= this.Speed;
  }
}
};

暂停比赛可以吗?你可以在浏览器窗口上监听,当它失去焦点时会被触发,然后在事件处理程序中暂停游戏

window.onblur = function() {
                // Add logic to pause the game here...
                Paused = true;
            };

这是可行的,但是我怎样才能取消暂停呢?我不想在画布上点击继续?有一个窗口不是onblur吗?是的,有:
window.onfocus
window.onblur
window.onblur = function() {
                // Add logic to pause the game here...
                Paused = true;
            };