Game Development Reference
In-Depth Information
This is done in the same way that the bricks were removed in Break-It, and is the reason for backwards looping
through the enemyShips array. Updating the bullets is done in a similar fashion.
Updating the Hero and Enemy Bullets
The bullets act very similar to the enemy ships. The only real difference is that hero bullets travel up instead of down.
Listing 11-23 shows these two bullet updating methods.
Listing 11-23. Updating the Hero and Enemy Bullets
p.updateHeroBullets = function () {
var bullet, i, velY;
var len = this.heroBullets.length - 1;
for (i = len; i >= 0; i--) {
bullet = this.heroBullets[i];
velY = bullet.speed * this.delta / 1000;
bullet.nextY = bullet.y - velY;
if (bullet.nextY < 0) {
this.heroBulletPool.returnSprite(bullet);
this.removeChild(bullet);
this.heroBullets.splice(i, 1);
}
}
}
p.updateEnemyBullets = function () {
var bullet, i, velY;
var len = this.enemyBullets.length - 1;
for (i = len; i >= 0; i--) {
bullet = this.enemyBullets[i];
velY = bullet.speed * this.delta / 1000;
bullet.nextY = bullet.y + velY;
if (bullet.nextY > screen_height) {
this.enemyBulletPool.returnSprite(bullet);
this.removeChild(bullet);
this.enemyBullets.splice(i, 1);
}
}
}
Building the Render Functions
Rendering the sprites is pretty straightforward. All of the calculations were handled in the updating process. Other
than a few checks that are first needed for the bullets and enemies, the render process simply puts the sprites in their
new position. These render functions are shown in Listing 11-24.
 
Search WWH ::




Custom Search