Jump to content
This forum will be closing down. Please move to the respective dedicated project forums.

Possible to create a sprite first, and then assign its properties?


BobDylan
 Share

Recommended Posts

I'd like to be able to do the following :

var playersprite = Phaser.Sprite.create(game);playersprite.x = 100;playersprite.y = 100;playersprite.key = 'dude';

instead of

var playersprite = Phaser.Sprite.create(game, 100, 100, 'dude');

So is there a way to just create the sprite first, and then afterwards assign its properties ?

 

(The reason for wanting this is a bit complex, but has to do with extending Phaser.Sprite to a different class)

Link to comment
Share on other sites

Yes, but by default it will be placed at 0, 0. It has to have a position as this is a fundamental property of a Sprite as opposed to just a texture. The only required parameter a Sprite needs is the first, the reference to the Game object. Could you explain a bit more about what you're trying to achieve?

Link to comment
Share on other sites

I'm trying to extend Phaser.Sprite to my own sprite class, to add some extra features.

I don't like the prototype way of extending objects, so I implemented my own extend-method (similar to John Resig's Simple Javascript inheritance).

 

I want to create a sprite, using an object that defines its properties (such as color, width, height, texture, etc.)

To do this, I want to create a bitmapData object inside a method of my new class.

The problem is that methods only exist after creating a object.  But the create function of Phaser.Sprite already expects the bitmapData to be ready!

 

I found a workaround by first creating the object with only the game parameter:

var playersprite = Phaser.Sprite.create(game);

and then calling a newly defined method initProperties(props) :

var bmd = this.game.add.bitmapData(props.width, props.height);bmd.ctx.beginPath();bmd.ctx.rect(0,0,props.width,props.height);bmd.ctx.fillStyle = props.color;bmd.ctx.fill();this.loadTexture(bmd);

Have no idea if this makes sense, but it works.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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