NistorCristian

Inheritance Phaser.Sprite.prototype graphics

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

Share this post


Link to post
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);

 

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.