AramCP Posted December 30, 2016 Share Posted December 30, 2016 Hi guys i have a game where you collect money, and then a menu, where i want all your money to be shown. I have a var called money, and a var called totalmoney. And i want to store all the money that i gain playing in that variable. For example, i play the game and i collect 20 money. And then this money has to be shown on the menu screen, well thats easy, in the menu screen you create a text saying: game.add.text(128, 0, money+'$') and the money will be shown, but what if i want to play again? I cant do the same thing because the money gained will overwrite the actual money, so guys any idea about what can i do? Link to comment Share on other sites More sharing options...
PhasedEvolution Posted December 30, 2016 Share Posted December 30, 2016 I see. You should store the money in the web storage. That way it will be saved for your next session (if you don't delete the data of course). Example: // That game session money should be stored on your global var money so just do: var money (the one that contains your temp money) if(localStorage.money == undefined) { // for the first save ever localStorage.setItem("global_money", money); } else {localStorage.global_money+= money} Then just load the value of global_money when you need: var global_money = localStorage.global_money; Link to comment Share on other sites More sharing options...
AramCP Posted December 30, 2016 Author Share Posted December 30, 2016 16 minutes ago, PhasedEvolution said: I see. You should store the money in the web storage. That way it will be saved for your next session (if you don't delete the data of course). Example: // That game session money should be stored on your global var money so just do: var money (the one that contains your temp money) if(localStorage.money == undefined) { // for the first save ever localStorage.setItem("global_money", money); } else {localStorage.global_money+= money} Then just load the value of global_money when you need: var global_money = localStorage.global_money; For the moment i only want to store it for one session, im not very experienced programmer and i dont really know how to implement that code you put above in my code :/, i would like to but i dont even understand what the code does, i mean i know what you want to do, but i've never used something like that before, and i preffer to dont use things that i dont know for what they're. But i apreciate the help, this will be usefull for me in the future. Link to comment Share on other sites More sharing options...
PhasedEvolution Posted December 30, 2016 Share Posted December 30, 2016 I don't think I understood the problem... Which one of these do you want: .You play the game. You score 20. Then a menu appears saying "Money: 20$". You start the game again and you play and you score 15 this time. Then a menu appears saying "Money: 15$". Or you play the game and you score 15. A menu saying "Money: 15$" appears. Then you start the game again and you play and you score 10. Then a menu appears saying "Money: 25$" (which is 15+10). The money adds to the previous game instances. Is it any of these? Link to comment Share on other sites More sharing options...
AramCP Posted December 30, 2016 Author Share Posted December 30, 2016 1 hour ago, PhasedEvolution said: I don't think I understood the problem... Which one of these do you want: .You play the game. You score 20. Then a menu appears saying "Money: 20$". You start the game again and you play and you score 15 this time. Then a menu appears saying "Money: 15$". Or you play the game and you score 15. A menu saying "Money: 15$" appears. Then you start the game again and you play and you score 10. Then a menu appears saying "Money: 25$" (which is 15+10). The money adds to the previous game instances. Is it any of these? The second one is exactly what i want, but just for one session Link to comment Share on other sites More sharing options...
PhasedEvolution Posted December 30, 2016 Share Posted December 30, 2016 If you don't leave your game, like if you don't close your browser window, you just have to set a outer global var. Declare your var money right in the beggining of everything like "var money = 0;" and then just play with it as you want. When you init your game money is 0 but as you play and play and score you can add more money to it like money += money; Then you just access it. It has a global scope. Anyway If you want to store money value "forever" (even if you close your browser window and reboot etc) you must add it to browser localStorage. AramCP 1 Link to comment Share on other sites More sharing options...
AramCP Posted December 30, 2016 Author Share Posted December 30, 2016 2 hours ago, PhasedEvolution said: If you don't leave your game, like if you don't close your browser window, you just have to set a outer global var. Declare your var money right in the beggining of everything like "var money = 0;" and then just play with it as you want. When you init your game money is 0 but as you play and play and score you can add more money to it like money += money; Then you just access it. It has a global scope. Anyway If you want to store money value "forever" (even if you close your browser window and reboot etc) you must add it to browser localStorage. Thanks man it works now, btw i will check how works that browser storage. Link to comment Share on other sites More sharing options...
PhasedEvolution Posted December 31, 2016 Share Posted December 31, 2016 1 hour ago, AramCP said: Thanks man it works now, btw i will check how works that browser storage. No problem Link to comment Share on other sites More sharing options...
Recommended Posts