Jump to content

What is the best way to access state's properties from an extended sprite?


korgoth
 Share

Recommended Posts

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.

Link to comment
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

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