Ninjadoodle Posted April 15, 2015 Share Posted April 15, 2015 Hi guys Could somebody please let me know the correct way of setting a variable to use between scenes? Let's say I have ...game.createScene('Stage01', { init: function() { score = 1234; ... then I goto Stage02 game.createScene('Stage02', {init: function() { var stage2score = 0; stage2score = score + stage2score; Is this the correct way of going about it? I always hear that using globals is bad Thanks heaps for any help! Quote Link to comment Share on other sites More sharing options...
enpu Posted April 15, 2015 Share Posted April 15, 2015 just put the variable inside game, like game.score Quote Link to comment Share on other sites More sharing options...
Ninjadoodle Posted April 15, 2015 Author Share Posted April 15, 2015 Hi enpu Thanks for the help! Just for my understanding, what is the difference between using ... score = 1234; vs game.score = 1234; Quote Link to comment Share on other sites More sharing options...
enpu Posted April 15, 2015 Share Posted April 15, 2015 score = 1234 puts the variable into the global namespace (window.score), while game.score puts it inside game object (window.game.score). Quote Link to comment Share on other sites More sharing options...
Ninjadoodle Posted April 15, 2015 Author Share Posted April 15, 2015 Cool! Thank you for letting me know Quote Link to comment Share on other sites More sharing options...
enpu Posted April 15, 2015 Share Posted April 15, 2015 No problem! And just make sure you don't overwrite any Panda classes or Core methods/properties:http://www.pandajs.net/docs/classes/game.Core.html Quote Link to comment Share on other sites More sharing options...
hamdirizal Posted April 17, 2015 Share Posted April 17, 2015 I usually put global variable inside game.config but I'm not sure if it is good practice. Quote Link to comment Share on other sites More sharing options...
enpu Posted April 17, 2015 Share Posted April 17, 2015 That is good, even safer than using just game. Quote Link to comment Share on other sites More sharing options...
Ninjadoodle Posted April 18, 2015 Author Share Posted April 18, 2015 Hi @hamdirizal or @enpu Could you guys please show me how you set your variable in the config file and how to access it inside your game? I would really appreciate it Thank you! Quote Link to comment Share on other sites More sharing options...
enpu Posted April 18, 2015 Share Posted April 18, 2015 pandaConfig = { score: 0};That is same as game.config.score Ninjadoodle 1 Quote Link to comment Share on other sites More sharing options...
Ninjadoodle Posted April 18, 2015 Author Share Posted April 18, 2015 Ahhh ok, thank you very much 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.