Jump to content

Inheritance Phaser.Sprite.prototype graphics


NistorCristian
 Share

Recommended Posts

Hello,

How can I do something like this:

Phaser.Sprite.call(this, game, x, y , graphics);

Where graphics is:

graphics = game.add.graphics(0, 0);
graphics.beginFill(0xFF33ff);
graphics.drawPolygon(polygonStructure);
graphics.endFill();
game.add.sprite(x, y);

This doesn't help me because I have to make something like this:

MonsterBunny = function (game, x, y, rotateSpeed) {

    Phaser.Sprite.call(this, game, x, y, 'bunny');

    this.rotateSpeed = rotateSpeed;

};

MonsterBunny.prototype = Object.create(Phaser.Sprite.prototype);
MonsterBunny.prototype.constructor = MonsterBunny;

/**
 * Automatically called by World.update
 */
MonsterBunny.prototype.update = function() {

    this.angle += this.rotateSpeed;

};

To be able to do:

var wabbit = new MonsterBunny(game, 200, 300, 1);

Thank you

Link to comment
Share on other sites

If I understand you correctly you want to create sprite based on a custom texture that you create in memory (i.e. bitmap data)?

With your graphics object you can use the generateTexture() method to get a texture that you can initialize your sprite with.

graphics = game.add.graphics(0, 0);
graphics.beginFill(0xFF33ff);
graphics.drawPolygon(polygonStructure);
graphics.endFill();

sprite = game.add.sprite(400, 300, graphics.generateTexture());
graphics.destroy();

Another way of doing it is to create a BitmapData object instead of using graphics, draw on it, and then use that as your texture for the sprite (this code is from the example at http://phaser.io/examples/v2/sprites/sprite-from-bitmapdata):

// create a new bitmap data object
var bmd = game.add.bitmapData(128,128);

// draw to the canvas context like normal
bmd.ctx.beginPath();
bmd.ctx.rect(0,0,128,128);
bmd.ctx.fillStyle = '#ff0000';
bmd.ctx.fill();

// use the bitmap data as the texture for the sprite
var sprite = game.add.sprite(200, 200, bmd);

 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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