momenimum Posted July 25, 2018 Share Posted July 25, 2018 Hi I have a very basic question. If I want variables to remain scoped to its module what is the best way to do it? So the below example I want every class created inside the module to be able to see "score". Right now I use game.score, I can also use global scope with window. But is there a better way? game.module( 'game.main' ) .body(function() { game.addAsset('screen.png'); game.score = 0; //I want everything in the module to see this game.hiscore = 0; //I want everything in the module to see this game.createClass('PreLoader', 'Loader', { ... //This class sees game.score game.createScene('Title', { ... //This class sees game.score game.createScene('Main', { .. //This class sees game.score Wolfsbane 1 Quote Link to comment Share on other sites More sharing options...
enpu Posted July 25, 2018 Share Posted July 25, 2018 You can just do it like this: game.module( 'game.main' ) .body(function() { var score = 0; game.createScene('Main', { init: function() { console.log(score); // 0 } }); }); Because the "score" variable is defined with var keyword inside the module, it won't be visible to any other modules, but will be visible to all classes inside that module. It's not good idea to create variables into root of game object, because you might overwrite something important. Quote Link to comment Share on other sites More sharing options...
momenimum Posted July 25, 2018 Author Share Posted July 25, 2018 Thanks for the advice! Quote Link to comment Share on other sites More sharing options...
Wolfsbane Posted July 26, 2018 Share Posted July 26, 2018 Useful topic. What's the advice for playing music globally? I set it up a global variable like this, and start playing it inside the Main scene(I don't think I it will play if I do it outside the scene), but when I switch scenes, the music stops. Quote Link to comment Share on other sites More sharing options...
momenimum Posted July 26, 2018 Author Share Posted July 26, 2018 @Wolfsbane If you add this to your config, audio should play thru switching scenes: game.config = { name: 'Test', audio: { stopOnSceneChange: false //Add this }, ....... Wolfsbane 1 Quote Link to comment Share on other sites More sharing options...
Wolfsbane Posted July 26, 2018 Share Posted July 26, 2018 Ah ha, nice! Cheers mate. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.