Jump to content

switch to new state and store the score previous state


sinanqd10
 Share

Recommended Posts

I want to store the score in game state to end game state. Example: I am in the playing game state and store the score, then when the game is finished. I switch to end game state. How can I send the score to the end game state? can anyone give me a simple example?

Link to comment
Share on other sites

Just store it outside of the state, for instance:

var score = 0;var state1 = {  create: function() {    console.log(score);  }}var state2 = {  create: function() {    console.log(score);  }}

How you structure your code is up to you, and the above is almost certainly not the way you'd want to do it, just a very simplistic example.

Link to comment
Share on other sites

this is my structure code

How can I pass the global score variable value to endState ?

(function (){var score = 0;var count = 0;var totalScore;var gameState = {    text: function() {     score = game.add.text(game.world.centerX+190,40,"0",{font: "14pt house-a-rama-kingpin",fill: "#ffffff"});   }   button: function() {     score.setText(count+=5);   }}var endState = {    create: function() {         totalScore = game.add.text(game.world.centerX,game.world.centerY + 80,'0\n',totalStyle);         totalScore.setText(score);   }}})();
Link to comment
Share on other sites

the code you wrote should be working fine, what problem are you getting with it? 

 

the method mentioned by lewster works great, but if you ever need to pass variables to a state without making them global you can pass them on the game.state.start function.

Example:

game.state.start('state', true, false, param1,param2,param3);

in your case it will be:
 

game.state.start('End', true, false, score);

and

var endState = {   var Score;    init: function(parameter1){//Note: the parameters passed on game.state start are sent to the init function(if it exists)       Score = parameter1;    }    create: function() {         totalScore = game.add.text(game.world.centerX,game.world.centerY + 80,'0\n',totalStyle);         totalScore.setText(Score);   }}

Goodluck!

Link to comment
Share on other sites

sinanqd10, the idea is correct in your sample, but you appear to be getting your variables muddled up - you're setting 'score' initially to 0, then you're setting it to a Text object and using 'count' to set the value, and then in the endState you're trying to set another text object with the previous 'score' text object, when in fact you should probably be using 'count':

(function (){// you can separate multiple variable declarations/assignments with a commavar scoreText, totalScoreText, score = 0;var gameState = {    text: function() {     scoreText = game.add.text(game.world.centerX+190,40,"0",{font: "14pt house-a-rama-kingpin",fill: "#ffffff"});   }   button: function() {     scoreText.setText(score+=5);   }}var endState = {    create: function() {         totalScoreText = game.add.text(game.world.centerX,game.world.centerY + 80,'0\n',totalStyle);         totalScoreText.setText(score);   }}})();
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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