我正在制作一个游戏:http ://www.taffatech.com/DarkOrbit.html
我想要做的是当我杀死一定数量的敌人时它会升级。这部分正在工作,但是我也希望在您升级时增加生成量。
在我的循环函数中:
function Loop()
{
if (isPlaying == true)
{
updateLevel(); //this updates the level but it calls it every frame which might be bad.
Player1.draw();
drawAllEnemies();
updateStats();
requestAnimFrame(Loop);
}
更新级别函数:
function updateLevel()
{
if(Player1.enemiesKilled <3)
{
level = 1;
}
else if(Player1.enemiesKilled > 3 && Player1.enemiesKilled <= 9)
{
level = 2;
}
else if(Player1.enemiesKilled > 9 && Player1.enemiesKilled <=18)
{
level = 3;
}
else if(Player1.enemiesKilled > 18 && Player1.enemiesKilled <= 38)
{
level = 4;
}
else if(Player1.enemiesKilled > 38 && Player1.enemiesKilled <= 70)
{
level = 5;
s
}
else if (Player1.enemiesKilled > 120)
{
level = 6;
}
if(level == 1)
{
spawnAmount = 1;
}
else if(level == 2)
{
spawnAmount = 2;
}
else if(level == 3)
{
spawnAmount = 3;
}
else if(level == 4)
{
spawnAmount = 4;
}
else if(level == 5)
{
spawnAmount = 5;
}
else if(level == 6)
{
spawnAmount = 6;
}
}
生成敌人功能:
function spawnEnemy(number) //total enemies starts at 0 and every-time you add to array
{
for (var x = 0; x < number; x++)
{
enemies[enemies.length] = new Enemy();
}
}
我的初始化:
function init()
{
spawnEnemy(spawnAmount);
drawMenu();
sndIntro.play();
document.addEventListener('click', mouseClicked ,false);
}
我尝试添加: spawnEnemy(spawnAmount); 在我更改了 updateLevel 函数中的生成量全局变量之后,但是由于每帧都会调用它,它会使数百个敌人飞向我。我怎么解决这个问题?
如果我尝试这样的事情,会生成数百个敌人,我假设因为它每帧都会被调用:
if(level == 1)
{
spawnAmount = 1;
spawnEnemy(spawnAmount);
}