Soule Posted December 31, 2014 Share Posted December 31, 2014 Hi, I was wondering if there's any way to pass variables when you change states, or if it's possible to set global variables? I'm using codevinsky's generator (https://github.com/codevinsky/generator-phaser-official), if it makes any difference. Specifically, I want to tell my Game scene what difficulty level to use when the user picks it in the intro scene. Thanks Link to comment Share on other sites More sharing options...
WitheringTreant Posted January 3, 2015 Share Posted January 3, 2015 Hey, have you already looked at this? http://www.html5gamedevs.com/topic/4702-states-with-parameters/ Also, yes you can set global variables. Just set them outside any functions, including game functions like create, update etc. http://www.w3schools.com/js/js_scope.asp I use global variables in that purpose in my game. Link to comment Share on other sites More sharing options...
spencerTL Posted January 3, 2015 Share Posted January 3, 2015 there's a specific example of setting a game wide accessible variable in the Phaser mobile template that allows them to be accessible throughout the game but not pollute the global scope.https://github.com/photonstorm/phaser/blob/master/resources/Project%20Templates/Full%20Screen%20Mobile/src/Boot.jsLook towards the top of the code. illscode 1 Link to comment Share on other sites More sharing options...
valueerror Posted January 3, 2015 Share Posted January 3, 2015 why is the "basicgame" object in that example not in global scope? Link to comment Share on other sites More sharing options...
spencerTL Posted January 3, 2015 Share Posted January 3, 2015 Sorry - I've been too simplistic. Basicgame is global but what I meant by not polluting the global scope is that Basicgame.score is less likely to conflict with anything than if you just had a globally scoped variable called score which could feasibly be used by another script on the page. valueerror 1 Link to comment Share on other sites More sharing options...
Finwillwin Posted November 20, 2017 Share Posted November 20, 2017 In the index.html put the variable as <html lang="en"> <head> <meta charset="UTF-8" /> <title>Your Game</title> <script type="text/javascript" src="code/phaser.min.js"></script> <script type="text/javascript" src="code/Level1.js"></script> </head> <body> <script type="text/javascript"> var game = new Phaser.Game(1100, 550, Phaser.AUTO); var game.variable = 0930211 </script> </body> </html> Link to comment Share on other sites More sharing options...
Recommended Posts