oliversb Posted May 5, 2014 Share Posted May 5, 2014 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 More sharing options...
codevinsky Posted May 5, 2014 Share Posted May 5, 2014 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);}; lukaMis and oliversb 2 Link to comment Share on other sites More sharing options...
oliversb Posted May 5, 2014 Author Share Posted May 5, 2014 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 More sharing options...
oliversb Posted May 5, 2014 Author Share Posted May 5, 2014 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 More sharing options...
XekeDeath Posted May 6, 2014 Share Posted May 6, 2014 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. oliversb 1 Link to comment Share on other sites More sharing options...
oliversb Posted May 6, 2014 Author Share Posted May 6, 2014 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 More sharing options...
oliversb Posted May 6, 2014 Author Share Posted May 6, 2014 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 More sharing options...
codevinsky Posted May 6, 2014 Share Posted May 6, 2014 Sorry, but I seem to be getting this error in Firefox:TypeError: this.onTextureUpdate is not a function ThanksThe correct code is:var sprite = game.add.sprite(0,0); Link to comment Share on other sites More sharing options...
oliversb Posted May 7, 2014 Author Share Posted May 7, 2014 How do I do it with inheritance? Link to comment Share on other sites More sharing options...
oliversb Posted May 8, 2014 Author Share Posted May 8, 2014 The correct code is:var sprite = game.add.sprite(0,0); Link to comment Share on other sites More sharing options...
Recommended Posts