Game Development Reference
In-Depth Information
nextX = this.leftWall;
}
}
else if (this.rightKeyDown) {
nextX += velocity;
if (nextX > this.rightWall) {
nextX = this.rightWall;
}
}
else if (this.downKeyDown) {
nextY += velocity;
if (nextY > this.floor) {
nextY = this.floor;
}
}
else if (this.upKeyDown) {
nextY -= velocity;
if (nextY < this.ceiling) {
nextY = this.ceiling;
}
}
this.heroShip.nextX = nextX;
this.heroShip.nextY = nextY;
}
p.updateEnemies = function () {
var enemy, i, velY;
var len = this.enemies.length - 1;
for (i = len; i >= 0; i--) {
enemy = this.enemies[i];
velY = enemy.speed * this.delta / 1000;
enemy.nextY = enemy.y + velY;
if (enemy.nextY > screen_height) {
enemy.reset();
this.enemyPool.returnSprite(enemy);
this.removeChild(enemy);
this.enemies.splice(i, 1);
}
}
}
The hero ship's next position is factored by calculating its velocity. Its velocity is calculated by comparing its speed
against the current delta value. The keys currently being pressed on the keyboard determine what direction the ship
should travel; if the new position should put the ship out of bounds, the values are adjusted accordingly to keep it in.
The enemy ships are animated automatically, and their positions are also calculated by using the delta technique
used with the stars and hero ship. Much like the stars, when an enemy travels past the screen, it needs to be reset.
In the case of an enemy, which was spawned from an object pool, its reset process is a little more detailed. First,
the reset method is called on it. If you recall, this function creates a new random type and updates its properties
accordingly. Next, it is returned back to the object pool from where it was spawned and removed from the display list.
When the enemy was spawned, a reference to it was pushed into the enemyShips array for updating, so it needs to be
removed from it to prevent further updates.
this.enemies.splice(i, 1);
Search WWH ::




Custom Search