mraak

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:

 

game.add.sprite()

 

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

Share this post


Link to post
Share on other sites

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 function

I hope it helps you.

Share this post


Link to post
Share on other sites

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?

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.