mraak Posted April 16, 2014 Share Posted April 16, 2014 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: game.add.sprite() When I tried to do a prototypal inheritance from Phaser.Sprite, it didn't work out. Link to comment Share on other sites More sharing options...
plicatibu Posted April 17, 2014 Share Posted April 17, 2014 Hi. 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 functionI hope it helps you. Link to comment Share on other sites More sharing options...
mraak Posted April 19, 2014 Author Share Posted April 19, 2014 Ok, I missed this part: Phaser.Sprite.call(this, game, x, y, v[0]); and this: game.add.existing(this); Couldn't and still can't find it in docs. What's the meaning of 5th param in Sprite.call? Link to comment Share on other sites More sharing options...
adamyall Posted April 19, 2014 Share Posted April 19, 2014 ".call" is used in JavaScript to call a function with a different scope than the scope in which it is called.In other words, Phaser.Sprite.call(this,game,x,y,v[0]) is equivalent to Phaser.Sprite(game,x,y,v[0]). Link to comment Share on other sites More sharing options...
mraak Posted April 19, 2014 Author Share Posted April 19, 2014 Got it, so it's basically just calling the super constructor. Link to comment Share on other sites More sharing options...
Recommended Posts