I have a work project that has become quite large, and I am now required to separate each object of the code into it's own file. The following code example is how it is structured currently, all in one giant file.
function Main() {
var main = this;
this.init = function() {
// code
main.keyboard.init();
main.game_loop.init();
main.physics.init();
}
this.keyboard = {
init: function() { },
keys: { UP:38, DOWN:40, LEFT:37, RIGHT:39 }
input: function(e) {
// functions reference 'main' variable
}
}
this.game_loop = {
init: function() { },
on_frame: function() {
// functions reference 'main' variable
}
}
this.physics = {
init: function() {
// functions reference 'main' variable
}
}
}
$(document).ready(function(){
var m = new Main();
m.init();
});
I have tried separating the objects into their own file's and changing them, for example from:
this.keyboard = { }
to
Main.prototype.keyboard = { }
However I then no longer have access to the 'main' variable. Thank you so much for anyone who puts any thought into this.