简体   繁体   中英

how to increase spawn rate over time in javascript game

I'm coding a game in javascript and I have this line of code:

setInterval(function () {spawnEnemy()}, 3000);

currently this spawns an enemy every 3000 milliseconds, but I want it to lower that rate by twenty percent every ten seconds, so that eventually it's spawning enemies really fast. I tried doing this by nesting setInterval's but all I succeeded in doing was crashing my browser. Can anyone provide the correct code for what I'm trying to accomplish?

Something along the lines of:

var interval = 3000;

function spawn() {
   var adjustmentPercentage = (0.2 / 10000) * interval 
   spawnEnemy();
   interval = interval - (interval * adjustmentPercentage);

   setTimeout(spawn, interval);

}

Using setTimeout allows you to change the delay each time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM