Biggerplay Posted February 26, 2014 Share Posted February 26, 2014 Where can I put a global game var in the basic template and then be able to access it from any of the game states? So for example I want to have gold var, which is the players gold amount, and I need that to increase each time a new level runs, but I can't quite figure out where to put it, and then access it. I've tried create a this.gold = 0, and a var gold = 0, in boot.js, in MainMenu.js etc, but I can't access it from HUD.js which is where I need to access it from. sbonavida 1 Link to comment Share on other sites More sharing options...
Biggerplay Posted February 26, 2014 Author Share Posted February 26, 2014 Figured it out, declaring it in the main function of boot.js wasn't' enough I had to define it as well in the boot.js create function as well. Link to comment Share on other sites More sharing options...
r00 Posted February 26, 2014 Share Posted February 26, 2014 If you need to save variables between multiple states, you can store them as your main global var's properties. I think the main global variable is called BasicGame in the basic template. For example:BasicGame.gold = 0;Saving the value to this.gold inside a state wouldn't work, as all the state values are flushed on state change. sbonavida and Dread Knight 2 Link to comment Share on other sites More sharing options...
rich Posted February 26, 2014 Share Posted February 26, 2014 Look at the 'Full Screen Mobile' template Boot.js, the top part:BasicGame = { /* Here we've just got some global level vars that persist regardless of State swaps */ score: 0, /* If the music in your game needs to play through-out a few State swaps, then you could reference it here */ music: null, /* Your game can check BasicGame.orientated in internal loops to know if it should pause or not */ orientated: false}; Disease, jerome, Heppell08 and 3 others 6 Link to comment Share on other sites More sharing options...
Recommended Posts