O'Reilly logo
  • Scott Noyes thinks this is interesting:

// while looping over each bullet … if (bullet.pos.x >= w + 20) { bullet.dead = true; }


Cover of HTML5 Games: Novice to Ninja


As shown, this adds another loop through the bullets container inside the main game loop. But, we're going to loop through the bullets container anyway and call each bullet's update(), so why not put it there instead? My bullet.update() increments this.pos.x, then checks if it is out of range and sets this.dead. However, if you do this with the baddies, you'll struggle to call the game over function from inside the scope of the baddie's update method.