Error in space invaders tutorial.

74 views
Skip to first unread message

hexregex

unread,
Feb 26, 2016, 5:30:11 AM2/26/16
to melonJS - A lightweight HTML5 game engine
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.





aaron.g...@gmail.com

unread,
Feb 26, 2016, 9:44:09 AM2/26/16
to melonJS - A lightweight HTML5 game engine
Hi there, thanks for reporting this in.

Yeah it looks like we had the proper code in our test example, but not in the html file. I've updated the tutorial to reflect that.

Just as an FYI, master is our development branch (might roll it back to 3.1.0, instead of 4.0 as we have no breaking changes yet). Both tutorials reflect our most stable release, so if we do any breaking changes, that may invalidate the tutorials until we do a release of the next major version.
Reply all
Reply to author
Forward
0 new messages