coachablekarma Posted February 13, 2017 Share Posted February 13, 2017 (edited) 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 February 13, 2017 by coachablekarma correction to script name Quote Link to comment Share on other sites More sharing options...
Parasyte Posted February 14, 2017 Share Posted February 14, 2017 What does your "laser" class look like? If you copied from the tutorial directly, did you accidentally leave off the width and height properties? See the end of the code snippet for `game.Laser`: http://melonjs.github.io/tutorial-space-invaders/#part3 Quote Link to comment Share on other sites More sharing options...
coachablekarma Posted February 14, 2017 Author Share Posted February 14, 2017 Hey, thanks for the response. I have those two values in the lasers class. When I try to debug the game it is showing those values for the width and height. Not sure why it is giving that error though. I can post the lasers class as well as soon as I get to my PC. Quote Link to comment Share on other sites More sharing options...
coachablekarma Posted February 15, 2017 Author Share Posted February 15, 2017 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; Quote Link to comment Share on other sites More sharing options...
Parasyte Posted February 17, 2017 Share Posted February 17, 2017 What version of melonJS are you using? I couldn't find any recent releases that lined up with the stack trace you provided. The current version is 4.1.0 Quote Link to comment Share on other sites More sharing options...
coachablekarma Posted February 21, 2017 Author Share Posted February 21, 2017 I'm using the 4.1.0 boilerplate Quote Link to comment Share on other sites More sharing options...
Parasyte Posted February 21, 2017 Share Posted February 21, 2017 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. Quote Link to comment Share on other sites More sharing options...
coachablekarma Posted February 21, 2017 Author Share Posted February 21, 2017 I will take a look, and let you know Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.