Jump to content
This forum will be closing down. Please move to the respective dedicated project forums.

Issue with Space Invaders tutorial


coachablekarma
 Share

Recommended Posts

Hey guys, just trying to get reacquainted with melonJs so I am going through the space invaders tutorial.  I have hit a small roadblock at the adding lasers portion.  I am getting this error when I attempt to shoot.

Uncaught TypeError: Cannot read property 'width' of undefined melonJS.js:12328

at Class.init (melonJS.js:12328)
    at Class (melonJS.js:531)
    at Object.me.pool.api.pull (melonJS.js:4242)
    at Class.update (player.js:32)
    at Class.update (melonJS.js:12166)
    at Object.me.game.api.update [as _patched] (melonJS.js:2874)
    at Object.<anonymous> (debugPanel.js:291)
    at Object.defineProperty.value [as update] (melonJS.js:29274)
    at _renderFrame (melonJS.js:12856)

 

here is the js file for the player

game.Player = me.Sprite.extend({
    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;
    },
    
    update : function(time)
    {
        this._super(me.Sprite, "update", [time]);
            
        if (me.input.isKeyPressed("left"))
        {
            this.pos.x -= this.velx * time / 1000;
        }
        
        if (me.input.isKeyPressed("right"))
        {
            this.pos.x += this.velx * time / 1000;
        }
        
        this.pos.x = this.pos.x.clamp(0, this.maxX);
        
        if (me.input.isKeyPressed("shoot"))
        {
            me.game.world.addChild(me.pool.pull("laser", this.pos.x - game.Laser.width, this.pos.y - game.Laser.height));
        }
                
        return true;
        
    
    }
});

 

any help would be appreicated

Edited by coachablekarma
correction to script name
Link to comment
Share on other sites

laser class:

 

game.Laser = me.Entity.extend({
    
    
    int : function (x,y)
    {

        this._super(me.Entity, "init", [x, y, {width: game.Laser.width, height: game.Laser.height}]);
        this.z = 5;
        this.body.setVelocity(0, 300);
        this.body.collisionType = me.collsion.types.PROJECTILE_OBJECT;
        this.renderable = new (me.Renderable.extend({
            init : function()
            {
                this._super(me.Renderable, "init", [0,0, game.Laser.width, game.Laser.height]);
            },
            destroy : function(){},
            draw : function (renderer)
            {
                var color = renderer.getColor();
                renderer.setColor('#5EFF7E');
                renderer.fillRect(0, 0, this.width, this.height);
                renderer.setColor(color);
            }
        }));
        this.alwaysUpdate = true;
    },
    
    update : function(time)
    {
        this.body.vel.y -= this.body.accel.y * time / 1000;
        if (this.pos.y + this.height <= 0)
        {
            me.game.world.removeChild(this);
        }
        
        this.body.update();
        me.collsion.check(this);
        
        return true;
    }
});

game.Laser.width = 5;
game.Laser.height = 28;

Link to comment
Share on other sites

Weird! I must have been building the wrong version, or looking at the wrong lines. The boilerplate is definitely on 4.1.0.

Ok, last question. How are you adding the laser class to the entity pool? The tutorial shows this:

me.pool.register("laser", game.Laser);

It appears that the call in player.js to me.pool.pull("laser", ...) is instantiating a me.Entity directly, instead of instantiating your game.Laser class. Just going entirely on the stack trace in your first post.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...