Jump to content

Adding sprite to stage


oliversb
 Share

Recommended Posts

How do I create a sprite using the 'new' keyword. I have a function acting as a class which uses Prototype to inherit from Phaser.Sprite. How do I take advantage of this to append it to the list of children on the stage?

 

Thanks

Link to comment
Share on other sites

So, if you have an extended sprite:

var MySprite = function(game, x, y, asset, frame) {  Phaser.Sprite.call(this, game, x, y, asset, frame);}MySprite.prototype = Object.create(Phaser.Sprite.prototype);MySprite.prototype.constructor = MySprite;

And you want to add it to the game, simple do:

create: function() {  var mySprite = new MySprite(this.game, x, y, 'assetKey', 0);  this.game.add.existing(mySprite);};
Link to comment
Share on other sites

 

So, if you have an extended sprite:

var MySprite = function(game, x, y, asset, frame) {  Phaser.Sprite.call(this, game, x, y, asset, frame);}MySprite.prototype = Object.create(Phaser.Sprite.prototype);MySprite.prototype.constructor = MySprite;

And you want to add it to the game, simple do:

create: function() {  var mySprite = new MySprite(this.game, x, y, 'assetKey', 0);  this.game.add.existing(mySprite);};

Thanks a ton. Very clear, to the point and this is exactly what I was looking for.

Link to comment
Share on other sites

 

So, if you have an extended sprite:

var MySprite = function(game, x, y, asset, frame) {  Phaser.Sprite.call(this, game, x, y, asset, frame);}MySprite.prototype = Object.create(Phaser.Sprite.prototype);MySprite.prototype.constructor = MySprite;

And you want to add it to the game, simple do:

create: function() {  var mySprite = new MySprite(this.game, x, y, 'assetKey', 0);  this.game.add.existing(mySprite);};

If you don't mind me asking? Is it possible to create a sprite without a key. In the case that I want to create a sprite which does not particularly start with a sprite? Thanks

Link to comment
Share on other sites

You can create a Sprite without a key, yes. It will be invisible, as expected.

 

To give it a key later on, you can use the loadTexture(key, frame) function.

var sprite = new Phaser.Sprite(game, 0, 0);sprite.loadTexture(key, frame);

This goes for Phaser.Image objects as well.

Link to comment
Share on other sites

You can create a Sprite without a key, yes. It will be invisible, as expected.

 

To give it a key later on, you can use the loadTexture(key, frame) function.

var sprite = new Phaser.Sprite(game, 0, 0);sprite.loadTexture(key, frame);

This goes for Phaser.Image objects as well.

Thanks. This is great help.

Link to comment
Share on other sites

You can create a Sprite without a key, yes. It will be invisible, as expected.

 

To give it a key later on, you can use the loadTexture(key, frame) function.

var sprite = new Phaser.Sprite(game, 0, 0);sprite.loadTexture(key, frame);

This goes for Phaser.Image objects as well.

Sorry, but I seem to be getting this error in Firefox:

TypeError: this.onTextureUpdate is not a function

 

Thanks

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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