AmAuron Posted May 30, 2015 Share Posted May 30, 2015 Hi there, i was searching the forum for a method to send information trough the states, and found this post that was very helpfull.http://www.html5gamedevs.com/topic/14708-controls-through-multiple-levels/?hl=%2Bobject+%2Bjson#entry84031 My question is....Is there a way to create an object in another .js file and use it instead of passing the information with the function state.start? Thank you for your time,and sorry if i didn't explained myself well...English isn't my native language. Link to comment Share on other sites More sharing options...
mwatt Posted May 31, 2015 Share Posted May 31, 2015 Yes there is. Any JavaScript file that is included above the JavaScript file that you want to work in, can be a source of variables - objects, arrays, functions, etc. So you could define an object in an included js file above the one you want to use it from and then you can access it (assuming there is not some reason why it takes too long to load before you need it - but this would not normally be the case). Any variable defined in the file that is not within the confines of another object or function is globally available. This is convenient, but can also lead to undesirable consequences if you aren't careful with your code (you can overwrite the variable value accidentally or another included js file might do that). The easiest way to avoid that is to make just one global object available to all of your code (give it a name that is not likely to be duplicated by any third party library) and then make all of the variables (or even functions) that you will wish to use in your program be members of this over-arching single global variable. You can also take the path of having a global "Boot" object, "Preload" object, "Game" object, etc., and just stick all of your other loose variables in an "master" object like I am suggesting. It should be noted that using objects defined outside of a function and not passed in, is not the necessarily an optimal programming methodology. It tightly couples your code together. But it will work. For a small project, no big deal, IMO. Link to comment Share on other sites More sharing options...
AmAuron Posted May 31, 2015 Author Share Posted May 31, 2015 Thank you very much Link to comment Share on other sites More sharing options...
Recommended Posts