Jump to content

Extending a Group


NORD
 Share

Recommended Posts

Hi!

How do I make multiple levels of extending of Phaser.Group? I need: BasicObject extending Phaser.Group, Hero extending BasicObject. When i do this, i have a error:

TypeError: a.setStageReference is not a function phaser.min.js:3

TypeError: this.children[a].preUpdate is not a function phaser.min.js:7
Thank you!
function create(){   hero = new Hero(game);      hero.init(game.world.randomX, game.world.randomY, 0);}//====================================================================================//Hero.prototype = Object.create(BasicObject.prototype);function Hero(game){   BasicObject.call(this, game);   console.log("Hero Created!");}Hero.prototype.init = function(posX, posY, rot){   this.sprite = new Phaser.Sprite(game, 'hero', 0, 0, 3);   BasicObject.prototype.init.call(this, posX, posY, rot);}//====================================================================================//BasicObject.prototype = Object.create(Phaser.Group.prototype);function BasicObject(game){   console.log("BasicObject Created!");   Phaser.Group.call(this, game);   this.container = null;   this.sprite = null;   this.timer = 100;}BasicObject.prototype.init = function(posX, posY, rot){   if(this.sprite != null) this.add(this.sprite);   this.x = posX;   this.y = posY;   this.angle = rot;}

 

Link to comment
Share on other sites

  • 1 year later...

Try this

var BasicObject = function(game){   Phaser.Group.call(this, game);   this.container = null;   this.sprite = null;   this.timer = 100;   console.log("BasicObject Created!");}BasicObject.prototype = Object.create(Phaser.Group.prototype);BasicObject.prototype.constructor = BasicObject;BasicObject.prototype.init = function(posX, posY, rot){   if(this.sprite != null) this.add(this.sprite);   this.x = posX;   this.y = posY;   this.angle = rot;}	//====================================================================================//var Hero = function(game){   BasicObject.call(this, game);      this.init(game.world.randomX, game.world.randomY, 0);   console.log("Hero Created!");}Hero.prototype = Object.create(BasicObject.prototype);Hero.prototype.constructor = Hero;Hero.prototype.init = function(posX, posY, rot){   this.sprite = new Phaser.Sprite(game, 'hero', 0, 0, 3);   BasicObject.prototype.init.call(this, posX, posY, rot);}//====================================================================================//function create(){   var hero = new Hero(game);   }
Link to comment
Share on other sites

My 2 cents: You can do this much better in TypeScript. I'm rather new to JS so I don't really know the 'myriad ways of writing it', as the forum says there is, and I was stunned trying to write OOP stuff. Moving to TS really makes everything much smoother!

 

I concur. Not that an OOP approach isn't possible in pure JS, but it's much, much cleaner in TS.

 

Just look at that example: http://www.typescriptlang.org/Playground#src=class%20GenericObject%20extends%20Phaser.Group%20%7B%0A%09%0A%7D

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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