BobDylan Posted September 20, 2014 Share Posted September 20, 2014 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 ofvar 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 More sharing options...
chg Posted September 20, 2014 Share Posted September 20, 2014 can't you do:var playersprite = new Phaser.Sprite(game);playersprite.x = 100;playersprite.y = 100;playersprite.key = 'dude'; Link to comment Share on other sites More sharing options...
lewster32 Posted September 20, 2014 Share Posted September 20, 2014 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 More sharing options...
BobDylan Posted September 20, 2014 Author Share Posted September 20, 2014 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 More sharing options...
Recommended Posts