Jump to content

How can I extend a Phaser sprite with class.js?


Recommended Posts

I'm using "Simple Inheritance": http://ejohn.org/blog/simple-javascript-inheritance/

Maybe I shouldn't be, and if not I'd like to here some better options. :) How can I extend a Phaser sprite using this? It does not implement the extend function, which makes interoping with it tricky. 

I would guess I can do something along the lines of over-riding the Phaser.Sprite.Prototype and constructor, and then trying to 'extend' it after. However, I'm not sure if that's going to have zero side-effects or even work. 

Link to comment
Share on other sites

I've tried that, but i need to support the extend method: Currently, I have:

Entity.prototype =  Object.create(Phaser.Sprite.prototype);Entity.prototype.constructor = Entity;Phaser.Sprite.prototype.extend = Class.extend;var Entity = Phaser.Sprite.extend({  init: function(game, x, y, key, frame) {    this.game = game;    this.id = null; // This is the network ID of the object on the server    // Call the phaser sprite chain all the way down    this.Sprite(game, x, y, key, frame);  },

However, I'm being told that 'extend' is not a function on Phaser.Sprite. Which of course, seems to be the case. I need to support this as everywhere elses uses this type of inheritance and it'd look a bit goofy for my entities to break the chain.. :)

Link to comment
Share on other sites

Ah ok.  :)  "Simple inheritance" looks pretty neat. reminds me of java.
I think, to use Class.extend you need to started out with something like this:

Phaser.Sprite = Class.extend({init:function(){//all the stuff}}); 

to then be able to do something like this:

var Entity = Phaser.Sprite.extend({init:function(){this._super();//other stuff}}); 

(I think  :P )

if you want to use Class.extend, i would do something like:

Entity = Class.extend({ init: function(game){  Phaser.Sprite.call(this,game,x,y,key,frame); }});

then you can create your entities like this:

Car = Entity.extend({ init: function(game){  this._super(this,game,x,y,key,frame); }});

( i haven't tested this and i feel like i missed something)

Anways i was checking the Phaser source and saw an extend method in the Utils.js here

Link to comment
Share on other sites


  • Recently Browsing   0 members

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