Jump to content

How to subclass Sprite?


Recommended Posts

How to subclass Phaser.Sprite and add custom stuff to it? The problem I see is that Sprite has to be explicitly added like this:




When I tried to do a prototypal inheritance from Phaser.Sprite, it didn't work out. 

Link to comment
Share on other sites



Based in the template provided in Phaser I created the following class (only one function showed but you get the idea)

var PlicaMultiTile = function (game, x, y, tiles) {    this.tiles = tiles;    var v = tiles.values();    Phaser.Sprite.call(this, game, x, y, v[0]);    this.inputEnabled=false;    game.add.existing(this);};PlicaMultiTile.prototype = Object.create(Phaser.Sprite.prototype);PlicaMultiTile.prototype.constructor = PlicaMultiTile;PlicaMultiTile.prototype.showTile = function(tile) {    if (this.tiles.hasItem(tile)) {        this.loadTexture( this.tiles.getItem(tile) );    }};

And I instantiate it like this:

// t is a hash table but it's a implementation dependent detail.var Btn = new PlicaMultiTile(this.game, 0, 0, t);

In case I want to move it latter on (say to position x = 100, y = 200):

Btn.x = 100;
Btn.y = 200;



In case I want it to respond to user input, I do:

Btn.inputEnabled = true;Btn.events.onInputUp.add(this.onControleClicked, this); // onControleClicked is the callback function

I hope it helps you.

Link to comment
Share on other sites


  • Recently Browsing   0 members

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