Game Development Reference
In-Depth Information
this.enemies.splice(i, 1);
this.removeChild(enemy);
this.spawnEnemyExplosion(enemy.x, enemy.y);
enemy.reset();
this.enemyPool.returnSprite(enemy);
}
else {
enemy.y = enemy.nextY;
}
}
}
Before the bullets and enemies are ultimately placed in their new positions, you need to check if they were
deemed dead during the update process. In the case of a bullet, it is reset in the same way it is when passing below
the screen. With the enemies, a few more things need to done. Other than its reset actions, the scoreboard should be
updated, and an explosion should be spawned in its place.
A few things were spawned in the update/render process. These sprites were spawned in a series of spawning
functions that will be covered next.
Building the Spawn Functions
When a bullet is fired, an explosion occurs, or a new enemy ship should appear, they are spawned. The functions that
were called to create these new spawns are shown in Listing 11-25.
Listing 11-25. The Spawn Functions Create New Enemy Ships, Bullets, and Explosions
p.spawnEnemyShip = function () {
var enemy = this.enemyPool.getSprite();
enemy.y = -enemy.getBounds().height;
enemy.x = Utils.getRandomNumber(enemy.getBounds().width,
screen_width - enemy.getBounds().width);
this.addChild(enemy);
this.enemies.push(enemy);
}
p.spawnEnemyBullet = function (enemy) {
var bullet = this.enemyBulletPool.getSprite();
bullet.currentAnimationFrame = 1;
bullet.y = enemy.y;
bullet.x = enemy.x;
this.addChildAt(bullet,0);
this.enemyBullets.push(bullet);
}
p.spawnHeroBullet = function () {
var bullet = this.heroBulletPool.getSprite();
bullet.x = this.heroShip.x;
bullet.y = this.heroShip.y - this.heroShip.getBounds().height / 2;
this.addChildAt(bullet, 0);
this.heroBullets.push(bullet)
}
p.spawnEnemyExplosion = function (x, y) {
var explosion = this.explosionPool.getSprite();
 
Search WWH ::




Custom Search