NORD

Extending a Group

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;}

 

Share this post


Link to post
Share on other sites

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);   }

Share this post


Link to post
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!

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

  • Recently Browsing   0 members

    No registered users viewing this page.