Jump to content

switch to new state and store the score previous state


Recommended Posts

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.


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

in your case it will be:

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


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);   }}


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

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.

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.


  • Recently Browsing   0 members

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