Jump to content

Phaser Context


Recommended Posts

Hi all,


I have a game with something like the following:

var GameState = function(game) {};GameState.prototype.preload = function() {  ...  this.floor = this.game.add.sprite(0,this.game.height-50,rlrbdr);}Shift = function(hour) {  this.ypos = function() {    return game.height - GameState.prototype.floor.height;   }}var game = new Phaser.Game(1136, 640, Phaser.AUTO);game.state.add('game', GameState, true);

I am trying to get to my floor sprite within my ypos function and I was under the impression that I would be able to access it with GameState.prototype.floor. However it is not there.


I feel this is probably my lack of understanding of JavaScript and the this keyword, but how am I meant to access my sprite outside the GameState?

Link to comment
Share on other sites

The instance of the state will be available on the StateManager object (game.state) in its states property, so you can access the state's properties (of which your floor object is one when added to 'this' within the state) there: 


Though this is a rather unclean way of doing it - ideally Shift should either be a function on the state itself, or should be passed the object it needs to act upon. It's hard to describe this or determine the right way from the code you've provided but looking at Phaser's own source and the examples should give you a good understanding of how it's structured; which is by no means the only way to do it, but certainly a good way.

Shift = function(hour, floor) {  this.ypos = function() {    return floor.game.height - floor.height;  }}
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.

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.


  • Recently Browsing   0 members

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