korgoth Posted October 4, 2014 Report Share Posted October 4, 2014 I am wondering what is the best way to access state's properties from an extended sprite?Method 1 ( pass this.game to the Player class like in phaser examples then use this.game.state.states.Game.property ) MyGame.Game = function (game) {};MyGame.Game.prototype = { create: function () { this.property = 123; // pass this.game as in the examples this.player = new Player(this.game); }, update: function() { this.property += 1; }, }Player = function (_game) { Phaser.Sprite.call(this, _game, 0, 0, 'player'); this.game = _game; console.log(this.game.state.states.Game.property);};Player.prototype = Object.create(Phaser.Sprite.prototype);Player.prototype.constructor = Player;Method 2 ( pass the state itslef to the Player class ): MyGame.Game = function (game) {};MyGame.Game.prototype = { create: function () { this.property = 123; // only pass this ( the state itslef ) and not this.game this.player = new Player(this); }, update: function() { this.property += 1; }, }Player = function (parentState) { Phaser.Sprite.call(this, parentState.game, 0, 0, 'player'); this.parentState = parentState; console.log(this.parentState.property);};Player.prototype = Object.create(Phaser.Sprite.prototype);Player.prototype.constructor = Player;Method 3 ( with parameters ): MyGame.Game = function (game) {};MyGame.Game.prototype = { create: function () { this.property = 123; // pass this.game as in the examples this.player = new Player(this.game,this.property); }, update: function() { this.property += 1; }, }Player = function (_game,property) { Phaser.Sprite.call(this, _game, 0, 0, 'player'); this.game = _game; this.property = property; console.log(this.property);};Player.prototype = Object.create(Phaser.Sprite.prototype);Player.prototype.constructor = Player;Anything wrong with method 2? Thank You. Quote Link to comment Share on other sites More sharing options...
xerver Posted October 4, 2014 Report Share Posted October 4, 2014 > Anything wrong with method 2? Nope. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.