Le Twitch Posted October 12, 2014 Share Posted October 12, 2014 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 More sharing options...
j0hnskot Posted October 12, 2014 Share Posted October 12, 2014 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 More sharing options...
spencerTL Posted October 12, 2014 Share Posted October 12, 2014 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. Link to comment Share on other sites More sharing options...
Le Twitch Posted October 12, 2014 Author Share Posted October 12, 2014 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 More sharing options...
SignalSin Posted October 13, 2014 Share Posted October 13, 2014 Which template is this? The full-screen mobile template. You'll see in the templates boot.js that you can declare the variable when you create BasicGame, so it persists across all states. Also, if you want a variable to be global, don't define it within a function using the 'this' keyword. Link to comment Share on other sites More sharing options...
Recommended Posts