Search the Community
Showing results for tags 'extended object'.
-
How do extended object's prototype functions fit in with the current state's functions? For example: Creature = function(game, x, y){ Phaser.Sprite.call(this, game, x, y, 'mob'); game.add.existing(this); this.enableBody = true; } }; Creature.prototype = Object.create(Phaser.Sprite.prototype); Creature.prototype.constructor = Creature; and var world = { create: function(){ // Test dummy dummy = new Creature(this, 200, 200); dummy.body.static = true; } }; work, but Creature = function(game, x, y){ Phaser.Sprite.call(this, game, x, y, 'mob'); } }; Creature.prototype = Object.create(Phaser.Sprite.prototype); Creature.prototype.constructor = Creature; Creature.prototype.create = function(){ game.add.existing(this); this.enableBody = true; }; and var world = { create: function(){ // Test dummy dummy = new Creature(this, 200, 200); dummy.body.static = true; } }; return "Uncaught TypeError: Cannot set property 'static' of null" From what I read in the extending sprites examples, the prototype functions are supposed to be automatically called in the given state. Am I missing something about scope or is there some timing involved? I was wondering if i could cut down on code if i did something like // Creature constructor Creature = function(game, x, y, alignment){ if(alignment === 'good'){ Phaser.Sprite.call(this, game, x, y, 'gMob'); }else if(alignment === 'neutral'){ Phaser.Sprite.call(this, game, x, y, 'nMob'); }else{ Phaser.Sprite.call(this, game, x, y, 'bMob'); } }; Creature.prototype = Object.create(Phaser.Sprite.prototype); Creature.prototype.constructor = Creature; Creature.prototype.create = function = { game.add.existing(this); game.physics.p2.enable(this); this.enableBody = true; his.body.setRectangle(20, 30); this.body.fixedRotation = true; }; instead of // Creature constructor Creature = function(game, x, y, alignment){ if(alignment === 'good'){ Phaser.Sprite.call(this, game, x, y, 'gMob'); game.add.existing(this); game.physics.p2.enable(this); this.enableBody = true; this.body.setRectangle(20, 30); this.body.fixedRotation = true; }else if(alignment === 'neutral'){ Phaser.Sprite.call(this, game, x, y, 'nMob'); game.add.existing(this); game.physics.p2.enable(this); this.enableBody = true; this.body.setRectangle(20, 30); this.body.fixedRotation = true; }else{ Phaser.Sprite.call(this, game, x, y, 'bMob'); game.add.existing(this); game.physics.p2.enable(this); this.enableBody = true; this.body.setRectangle(20, 30); this.body.fixedRotation = true; } }; Creature.prototype = Object.create(Phaser.Sprite.prototype); Creature.prototype.constructor = Creature; Thanks in advance!