Jump to content

How to call a variable in another state?


Le Twitch
 Share

Recommended Posts

Main.Game = function (game) {    this.score = 0;};Main.Game.prototype = {    create: function() {    }};
Main.ScoreMenu = function (game) {};Main.ScoreMenu.prototype = {    create: function() {        console.log(Main.Game.score);    }};

How do I call a variable in another state? The above method is obviously wrong since it doesn't work.

Link to comment
Share on other sites

Is ScoreMenu a state itself? Do you call it via game.state.start()? Or just access it as a property of the object Main?

 

If Main.game is the current active state, you can access the contents of it with  game.state.getCurrentState(); . This returns the active state and you can access every value and function in there..

 

According to the code above, you got  Main.game but you try to access score from Main.Game which doesn't exist in the code you gave us. Did you try Main.game.score?

Link to comment
Share on other sites

Is ScoreMenu a state itself? Do you call it via game.state.start()? Or just access it as a property of the object Main?

 

If Main.game is the current active state, you can access the contents of it with  game.state.getCurrentState(); . This returns the active state and you can access every value and function in there..

 

According to the code above, you got  Main.game but you try to access score from Main.Game which doesn't exist in the code you gave us. Did you try Main.game.scor

 

Yes ScoreMenu is a state itself and I have to call it with game.state.start()

 

Accessing variables in an active state isn't the problem. I want to have global variables that can be accessed from any state.

 

Main.game was a typo, it should be Main.Game

 

 

Look at the templates provided with phaser. In the boot state there is a Basicgame.score variable defined that can be accessed throughout the game's states.

 

Which template is this?

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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