wamballa Posted December 17, 2017 Share Posted December 17, 2017 Hi I've created a game which is pretty cool ( for me ) but I'm stuck now on the simplest of things. How do I run global variables across the different level states. the variables I want to use are; highscore, score, lives Where in the code do i declare these variables? I've tried a load of things but can't seem to crack it. https://github.com/wamballa/games/tree/master/arcade/category/jetpac3 Any help would be appreciatd and let me know if I need to explain more. Thanks Link to comment Share on other sites More sharing options...
spencerTL Posted December 17, 2017 Share Posted December 17, 2017 Look at the templates included with Phaser. One of them, I forget which, shows one way to do this. It is in the boot state and essentially involves declaring Basicgame.highscore or whatever. That variable is global. It doesn't have to be declared in boot but that makes it available right from the offset so is a good place. Link to comment Share on other sites More sharing options...
TomC Posted December 17, 2017 Share Posted December 17, 2017 class Game extends Phaser.Game { constructor() { this.global = { highscore: 25 }; } // Then access in state via this.game.global.highscore; Fenopiù 1 Link to comment Share on other sites More sharing options...
wamballa Posted December 17, 2017 Author Share Posted December 17, 2017 Wow. EUREKA! That is works great. I did need to put super(); before this.global...... and now it works perfectly. Thanks!!!!!!!!!!!!! Link to comment Share on other sites More sharing options...
TomC Posted December 17, 2017 Share Posted December 17, 2017 ^ No dramas, keep up the good work with the game. Link to comment Share on other sites More sharing options...
Recommended Posts