Jump to content

Extended object function troubles


d0nut
 Share

Recommended Posts

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!

Link to comment
Share on other sites

It's returning that because when I try to set dummy.body.static to true it doesn't see that dummy.body exists.

For some reason, the things in the Creature.prototype.create function aren't being executed, I just don't understand why.

Link to comment
Share on other sites

 

Your problem is that "dummy.body" is null when you are trying to access the property "static".

 

You are a valiant warrior for attempting to use prototype's in an OOP fashion. I would recommend just using regular classes with Es6 or TypeScript. Or you could use some component pattern from a  framework like Angular or React.

Link to comment
Share on other sites

Thanks, @samme.

But, where can I find info like that.  I've dug through the Phaser-CE API and can't find anything saying what extended objects do or do not have.

I actually just found what I needed.

I was looking in the gameObjectFactory docs not the sprites.  Thanks again for the info samme.

One last thing.  If you don't mind, could you give me an example of implementing Creature#create the way you say?

I'm still very new at Phaser and even worse at OOP, lol.

Thanks everybody for your replies.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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