bexphones Posted October 8, 2017 Share Posted October 8, 2017 hi, i would put a prototype outside a state. this code works well. var the_game = { create: function(game){ _mechant = function(posx,posy,image){ this.image=image this.posx=posx this.posy=posy Phaser.Sprite.call(this,game,this.posx,this.posy,this.image) game.add.existing(this) } _mechant.prototype=Object.create(Phaser.Sprite.prototype) this.m=new _mechant(100,100,'green_circle') }, }; But when i put the prototype outside the state (to reuse them) i have got this error : ReferenceError: game is not defined _mechant = function(posx,posy,image){ this.image=image this.posx=posx this.posy=posy Phaser.Sprite.call(this,game,this.posx,this.posy,this.image) game.add.existing(this) } _mechant.prototype=Object.create(Phaser.Sprite.prototype) var the_game = { create: function(game){ this.m=new _mechant(100,100,'green_circle') }, }; What must i do to bind the game to the prototype ? Link to comment Share on other sites More sharing options...
Théo Sabattié Posted October 8, 2017 Share Posted October 8, 2017 game is the parameter of the function create. You can add game as parameter of the mechant constructor. Or define a global variable game and assign its value on create function (ugly) Link to comment Share on other sites More sharing options...
samme Posted October 8, 2017 Share Posted October 8, 2017 _mechant = function (game, posx, posy, image) { this.image = image; this.posx = posx; this.posy = posy; Phaser.Sprite.call(this, game, this.posx, this.posy, this.image); game.add.existing(this); }; _mechant.prototype = Object.create(Phaser.Sprite.prototype); var the_game = { create: function (game) { this.m = new _mechant(game, 100, 100, 'green_circle'); } }; Link to comment Share on other sites More sharing options...
bexphones Posted October 9, 2017 Author Share Posted October 9, 2017 hi, thanks both. it works and it works also without game in the 'create:function()' like Théo suggested _mechant = function (game, posx, posy, image) { this.image = image; this.posx = posx; this.posy = posy; Phaser.Sprite.call(this, game, this.posx, this.posy, this.image); game.add.existing(this); }; _mechant.prototype = Object.create(Phaser.Sprite.prototype); var the_game = { create: function () { this.m = new _mechant(game, 100, 100, 'green_circle'); } }; Link to comment Share on other sites More sharing options...
Recommended Posts