Sign in to follow this  
Le Twitch

How to call a variable in another state?

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.

Share this post


Link to post
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?

Share this post


Link to post
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?

Share this post


Link to post
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...
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.