Jump to content

Phaser States properties missing


luckylooke
 Share

Recommended Posts

Mobile game template from photostorm github:
//  When a State is added to Phaser it automatically has the following properties set on it, even if they already exist:
 
        this.game;      //  a reference to the currently running game
        this.add;       //  used to add sprites, text, groups, etc
        this.camera;    //  a reference to the game camera
        etc..

 

 

I have problem with this, because those properties are not available from when state is added, but after state is started (game.state.start('stateName');)

var game = new Phaser.Game(800, 600, Phaser.CANVAS);var BasicGame = {};BasicGame.gameState = function () {    alert(this.game); // undefined};BasicGame.gameState.prototype = {    create: function () {    }};game.state.add('First', BasicGame.gameState);game.state.start('First'); 

See the fiddle: http://jsfiddle.net/luckylooke/5KHRd/6/

Link to comment
Share on other sites

You still have access to 'game' which is set globally in this case. You shouldn't be referencing game or any of its components in the state constructor, only in its methods such as init, preload, create and so on where game is passed as the first parameter (and where you can then attach it to the state so further custom methods can access via setting this.game. I think possibly the documentation is a little misleading as to where you gain access to the game object within a state.

Link to comment
Share on other sites

Usually I get this errors because, "this" isn't what I expect it to be. 'this' isn't the function, but the context ("owner") of the function. In your case "this" isn't the game state, but "BasicGame", try the following and you might see:

BasicGame.gameState = function (args) {    console.log(this,args);};

EDIT: However, that's just the "why" of the error (encapsulating the gamestate in BasicGame changed the owner to the unexpected), not how to work around it. Encapsulating the various states in it's own container certainly has it's charm.

Link to comment
Share on other sites

You still have access to 'game' which is set globally in this case. You shouldn't be referencing game or any of its components in the state constructor, only in its methods such as init, preload, create and so on where game is passed as the first parameter (and where you can then attach it to the state so further custom methods can access via setting this.game. I think possibly the documentation is a little misleading as to where you gain access to the game object within a state.

That is exactly my walk-around, as I have game object accessible globally ;) Thanks for answer anyway :)

 

Usually I get this errors because, "this" isn't what I expect it to be. 'this' isn't the function, but the context ("owner") of the function. In your case "this" isn't the game state, but "BasicGame", try the following and you might see:

BasicGame.gameState = function (args) {    console.log(this,args);};

EDIT: However, that's just the "why" of the error (encapsulating the gamestate in BasicGame changed the owner to the unexpected), not how to work around it. Encapsulating the various states in it's own container certainly has it's charm.

But after starting the state, there are declared properties in this as mentioned in note. Never mind, I have accessing game properties from global game object and I dont rely on mentioned injection anymore ;) Thanks for answer anyway :)

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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