-1

单击此链接,大约 2 秒后,您会看到一些船只向您驶来。现在转到另一个窗口 15-20 秒。然后回来,你会看到一堵巨大的敌人墙向你袭来。现在显然这是因为画布使循环继续进行,但只是不让来继续?

所以任何帮助都会很棒,我知道这在游戏过程中不是问题,但如果有人离开窗户,那么它就会把事情搞砸......

我试图通过添加这些监听器来解决这个问题:

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

        window.onfocus = function() {
            // Add logic to pause the game here...
            startLoop();
        };

但这并不能解决问题...

实际循环:

function init()
{

isPlaying = true;
drawBackground();
drawBars();
setUpListeners();
startLoop();

}

进而...

function Loop()
{
if (isPlaying == true)
{
Player1.draw();
requestAnimFrame(Loop);
 drawAllEnemies();

}

}

function startLoop()
{

isPlaying = true;
Loop();
startSpawningEnemies();

}

function stopLoop()
{
isPlaying = false;
stopSpawningEnemies();
}

function spawnEnemy(n) //total enemies starts at 0 and every-time you add to array
{
  for (var x = 0; x < n; x++)
   {

     enemies[totalEnemies] = new Enemy();
     totalEnemies++; 
   }

}

function drawAllEnemies()
{

  ClearEnemyCanvas();
  for(var i = 0; i < enemies.length; i++)
   {
      enemies[i].draw();

   }
}

function startSpawningEnemies()
{
  stopSpawningEnemies();

  spawnInterval = setInterval(function() {spawnEnemy(spawnAmount);}, spawnRate); //this calls spawnEnemy every spawnRate
  /////////spawn 'spawnAmount' enemies every 2 seconds




}


function stopSpawningEnemies()
{

clearInterval(spawnInterval);
}

敌人的实际方法:

function Enemy()  //Object
{

//////Your ships values
this.EnemyHullMax = 1000;
this.EnemyHull = 1000;
this.EnemyShieldMax = 1000;
this.EnemyShield = 347;
this.SpaceCrystalReward = 2684;
this.EnemySpeed = 2; //should be around 6 pixels every-time draw is called by interval, directly linked to the fps global variable
////////////



////Pick Ship
this.type = "Hover";
this.srcX = EnemySrcXPicker(this.type);
this.srcY = EnemySrcYPicker(this.type);

this.enemyWidth = EnemyWidthPicker(this.type);
this.enemyHeight = EnemyHeightPicker(this.type);

this.drawX = EnemydrawXPicker(this.type);
this.drawY = EnemydrawYPicker(this.type);
////


}

Enemy.prototype.draw = function()
{

this.drawX -= this.EnemySpeed;
ctxEnemy.globalAlpha=1;
ctxEnemy.drawImage(spriteImage,this.srcX,this.srcY,this.enemyWidth,this.enemyHeight,this.drawX,this.drawY,this.enemyWidth,this.enemyHeight);

}


function EnemySrcXPicker(type)
{
if (type == "Hover")

    {

        return 906;
      }
}

function EnemySrcYPicker(type)
{
if (type == "Hover")

   {
        return 616;
     }
}

function EnemydrawXPicker(type)
{
if (type == "Hover")

     {
        return Math.floor(Math.random() *  1000) + canvasWidthEnemy;
     }
}

function EnemydrawYPicker(type)
{
if (type== "Hover")

     {
        return Math.floor(Math.random() * (canvasHeightEnemy - 72));
     }
}


function EnemyWidthPicker(type)
{
if (type == "Hover")

     {
        return 90;
     }
}

function EnemyHeightPicker(type)
{
if (type == "Hover")

     {

        return 72;
     }
}
4

1 回答 1

4

取决于循环。

如果您使用 setTimeOut 或 setInterval,则可以。即使窗口失去焦点,循环也会继续。

如果你使用requestFrameAnimation,那么不,当窗口失去焦点时循环将停止。

requestFrameAnimation 是为了解决此类问题而创建的。让你的 PC 为不活跃的东西消耗 CPU 周期是很愚蠢的。

于 2013-06-04T20:16:44.187 回答