2

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.

RON2015
  • 443
  • 3
  • 16
  • I thought to try changing main to this but I had errors, maybe I need to try that again. I will do that and post again here. – RON2015 Dec 16 '18 at 06:20
  • Use arrow functions instead, and avoid the unnecessary `main` variable, just use `this` – CertainPerformance Dec 16 '18 at 06:20
  • I just tried and failed. I moved game_loop into it's own file as a prototype, and replaced all 'main' with 'this', it fails at main.game_loop.init(); in the Main function. Will you please show me a basic example of the structure? – RON2015 Dec 16 '18 at 06:23
  • Are you sure you're importing and exporting everything properly? I don't see any cross-file syntax here – CertainPerformance Dec 16 '18 at 06:24
  • I'm sure it's imported properly, and actually it is firing the game_loop.init() function. I put a console log as the first thing in the function and it fires. It's actually failing if the prototype refers to itself. for example I was using main.game_loop etc... this.game_loop within the prototype doesn't seem to work. Does that make sense? – RON2015 Dec 16 '18 at 06:30
  • See the canonical - like I said, use arrow functions instead – CertainPerformance Dec 16 '18 at 06:33
  • I feel like an idiot, I don't understand what you mean. – RON2015 Dec 16 '18 at 06:33
  • Just see the canonical – CertainPerformance Dec 16 '18 at 06:35
  • thank you. but I have looked into the other post and I just can't understand how to apply it to my code – RON2015 Dec 16 '18 at 06:54
  • Actually, works just fine for me without any of that https://jsfiddle.net/avL5dpf9/ – CertainPerformance Dec 16 '18 at 07:18
  • That is what is so frustrating about this. It works, and they are making me split it into different files. – RON2015 Dec 16 '18 at 07:24
  • For example, "var GameLoop = require('gameloop.js'); this.game_loop = new GameLoop();", and then in gameloop.js: "module.exports = { init: function() { }, on_frame: function() { // functions reference 'main' variable };" – Jim Dec 17 '18 at 15:45

0 Answers0