Hello,
The space invaders tutorial errors out when the enemies first reach the edge of the screen and try to move downwards.
Here is play.js on completion of the tutorial (before Challenges):
game.PlayScreen = me.ScreenObject.extend({
/**
* action to perform on state change
*/
checkIfLoss: function (y) {
if (y >= this.player.pos.y) {
this.reset();
}
},
onResetEvent: function() {
me.game.world.addChild(new me.ColorLayer("background", "#000000"), 0);
me.game.world.addChild(me.pool.pull("player"), 1);
this.enemyManager = new game.EnemyManager();
this.enemyManager.createEnemies();
me.game.world.addChild(this.enemyManager, 2);
me.input.bindKey(me.input.KEY.LEFT, "left");
me.input.bindKey(me.input.KEY.RIGHT, "right");
me.input.bindKey(me.input.KEY.A, "left");
me.input.bindKey(me.input.KEY.D, "right");
me.input.bindKey(me.input.KEY.SPACE, "shoot", true);
},
/**
* action to perform when leaving this screen (state change)
*/
onDestroyEvent: function() {
me.input.unbindKey(me.input.KEY.LEFT);
me.input.unbindKey(me.input.KEY.RIGHT);
me.input.unbindKey(me.input.KEY.A);
me.input.unbindKey(me.input.KEY.D);
me.input.unbindKey(me.input.KEY.SPACE);
}
});
The error thrown:
Uncaught TypeError: Cannot read property 'pos' of undefined
On this line of checkIfLoss:
if (y >= this.player.pos.y) {
The tutorial states:
"The PlayScreen is our current game state. It holds the reference to the
player, and it has the ability to reset the state. So let's add the
logic for checking a lose condition there."
Problem:
I can't find where "player" is referenced by the PlayScreen in the instructions or source from the tutorial.
My Fix:
In onResetEvent I replaced this line:
me.game.world.addChild(me.pool.pull("player"), 1);
with these two lines:
this.player = me.pool.pull("player");
me.game.world.addChild(this.player, 1);Now space invaders executes without error.
Please fix the tutorial or explain what I did wrong.
Thank you. :-)
I'm using:
Ubuntu 14.04
Chrome Version 48.0.2564.97 (64-bit)melonJS 4.0.0 pulled from git.