melonjs/tutorial-space-invaders

Fails with melonJS 6.10

Closed this issue · 3 comments

Can't follow the tutorial, I get an error:

Uncaught TypeError: Cannot read property 'width' of undefined

when I add unbindKeys in method onDestroyEvent() in file play.js in partApplying Movement

When I comment it out I get the same error after adding EnemyManager in part Enemy movement

Can't proceed with the tutorial, unfortunately.

Browsers: Chrome, Firefox

obiot commented

not sure to understand where the issue is in your code to be honest, but you can have a look compared to the final step :
http://melonjs.github.io/tutorial-space-invaders/tutorial_step5/index.html

maybe there is an error (apologies if it is) in the tutorial writing itself, but I do not see it right now

I just had the same issue but found the error. It's right after "Applying Movement" where it redefines the init method of player.js. { image : image } suddenly becomes image. Fixed that and the rest was easy enough to follow. Thanks for the on-ramp. Off to try making a side scroller now.

obiot commented

indeed, good catch !!! the code example is :

init : function () {
    var image = me.loader.getImage("player");
    this._super(me.Sprite, "init", [
        me.game.viewport.width / 2 - image.width / 2,
        me.game.viewport.height - image.height - 20,
        image
    ]);
    this.velx = 450;
    this.maxX = me.game.viewport.width - this.width;
},

where it should be

init : function () {
    var image = me.loader.getImage("player");
    this._super(me.Sprite, "init", [
        me.game.viewport.width / 2 - image.width / 2,
        me.game.viewport.height - image.height - 20,
        { image : image }
    ]);
    this.velx = 450;
    this.maxX = me.game.viewport.width - this.width;
},