Game Development Reference
In-Depth Information
this.addChild(star);
this.stars.push(star);
}
}
p.buildSprites = function () {
this.heroShip = new game.HeroShip();
this.heroShip.on(this.heroShip.EXPLOSION_COMPLETE, this.checkGame,
this);
this.heroShip.x = screen_width / 2;
this.heroShip.y = screen_height - this.heroShip.getBounds().height;
this.heroBulletPool = new game.SpritePool(game.Bullet, 20);
this.enemyBulletPool = new game.SpritePool(game.Bullet, 20);
this.enemyPool = new game.SpritePool(game.EnemyShip, 10);
this.explosionPool = new game.SpritePool(game.Explosion, 10);
this.healthMeter = new game.HealthMeter();
this.scoreboard = new game.Scoreboard();
this.lifeBox = new game.LifeBox(this.numLives);
this.addChild(this.heroShip, this.healthMeter, this.scoreboard,
this.lifeBox);
}
The stars created in buildStars are randomly positioned in the container and added to the stars array. These
are stored in an array so they can be accessed and moved during the game loop.
Several elements are created in the buildSprites method. First, the hero ship is created and positioned at the
bottom of the screen. A listener is set on the sprite for when the ship has finished exploding. When this event occurs,
the check game is called to evaluate what should happen next. This will be covered in the “Building the Check
Functions” section.
Next, the enemy ships, bullets, and explosions are created by using the SpritePool class. These pools are set to
the appropriate game properties and will be used to add and recycle sprite objects during the game. Finally, the HUD
elements are instantiated and added to the container.
Setting Up the Controls
The controls are set up similarly to the paddle controls in Break-It. However, this time you have the ability to move
up and down and fire bullets. The bounds for the hero ship are first created in setWalls . Next, the controls are set
by adding keyboard listeners to the document , which will fire the appropriate handlers. These methods are shown
in Listing 11-18.
Listing 11-18. Setting Up the Walls and Keyboard Controls
p.setWalls = function () {
this.leftWall = this.heroShip.getBounds().width / 2;
this.rightWall = screen_width - this.heroShip.getBounds().width / 2;
this.floor = screen_height - this.heroShip.getBounds().height;
this.ceiling = screen_height - (this.heroShip.getBounds().height *
3);
}
p.setControls = function () {
document.onkeydown = this.handleKeyDown.bind(this);
document.onkeyup = this.handleKeyUp.bind(this);
}
 
Search WWH ::




Custom Search