Game Development Reference
In-Depth Information
Initializing the Game
As usual, the initialize method calls a list of methods that set up a new game. The first method is used to initialize
the game arrays and set a few properties (see Listing 11-16). The other initializing methods create the game sprites,
hero ship boundaries, and set the controls. Lastly, the background music is started.
Listing 11-16. The Initializing Methods for the Game
p.initialize = function () {
this.Container_initialize();
this.setProperties();
this.buildStarField();
this.buildSprites();
this.setWalls();
this.setControls();
createjs.Sound.play(game.assets.SOUNDTRACK);
}
p.setProperties = function () {
this.heroBulletPool = [];
this.heroBullets = [];
this.enemyPool = [];
this.enemies = [];
this.enemyBulletPool = [];
this.enemyBullets = [];
this.stars = [];
this.explosionPool = [];
this.betweenLevels = false;
this.enemyLastSpawnTime = 0;
}
The setProperties method initializes the arrays in the game. It is imperative that you not initialize array
properties when declaring them in your classes. Doing so will cause these arrays to be shared among all instances
of the class. For example, you will be adding bullet objects to the heroBullets array during gameplay. When you are
finished with the Game instance, the final value of heroBullets will carry over to a new instance of the game. This can
easily be avoided by creating a function when initializing the game to initialize all of the class's arrays.
Creating the Game Sprites
Several sprite objects are initialized at the beginning of the game. A function is created for creating all stars in the star
field and another to create the hero ship, the object pools, and the HUD elements. These two functions are shown in
Listing 11-17.
Listing 11-17. Creating the Stars and Game Sprite Object Pools
p.buildStarField = function () {
var star;
var numStars = 20;
for (i = 0; i < numStars; i++) {
star = new createjs.Sprite(spritesheet, 'star3');
star.speed = Utils.getRandomNumber(100, 200);
star.x = Math.random() * screen_width;
star.y = Math.random() * screen_height;
 
Search WWH ::




Custom Search