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

put a prototype outside a state


bexphones
 Share

Recommended Posts

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

_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

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

 Share

  • Recently Browsing   0 members

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