sinanqd10 Posted November 13, 2014 Share Posted November 13, 2014 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 More sharing options...
lewster32 Posted November 13, 2014 Share Posted November 13, 2014 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. Titus and harrywatson 2 Link to comment Share on other sites More sharing options...
sinanqd10 Posted November 13, 2014 Author Share Posted November 13, 2014 The way I wanted is that when the game state is finished then it switch to the end game state which store the score from the game state that counted as total score. Please show me fiddler example bro. Thanks for answered my question. Link to comment Share on other sites More sharing options...
lewster32 Posted November 13, 2014 Share Posted November 13, 2014 Here you go, a more comprehensive working example with a suggestion of a way to structure your code: http://jsfiddle.net/lewster32/woy3reug/ sinanqd10 1 Link to comment Share on other sites More sharing options...
sinanqd10 Posted November 14, 2014 Author Share Posted November 14, 2014 Thanks lewster32. It really helped me a lot Link to comment Share on other sites More sharing options...
sinanqd10 Posted November 14, 2014 Author Share Posted November 14, 2014 this is my structure codeHow 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 More sharing options...
Ham Posted November 14, 2014 Share Posted November 14, 2014 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);andvar 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! sinanqd10 1 Link to comment Share on other sites More sharing options...
lewster32 Posted November 14, 2014 Share Posted November 14, 2014 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 More sharing options...
sinanqd10 Posted November 17, 2014 Author Share Posted November 17, 2014 Lewster32, You are right. My silly mistakes. Thanks you for helping bro lewster32 1 Link to comment Share on other sites More sharing options...
Recommended Posts