I’m quite new to the concept of module patterns. I managed to implement the core features of my HTML5 game, but I can’t figure out a good way to make certain variables available to every function of that game.
Here is a short pseudo example which should make it more clear:
var Game = {};
Game.player = (function() {
//...
return {
update : update,
draw : draw
};
});
Game.main = (function() {
var player = new Game.player();
// needed by player
var gravity = 1.0,
loop,
canvas,
ctx,
key_inputs;
//...
return {
init : init,
pause : pause,
play : play
};
});
var game = new Game.main();
game.init();
If code scoped to your
Playerfunction needs to use some variables declared in yourGamefunction, you have three options:Playerif they change inGame)getterfunction inGamethat returns the current state of the variables to thePlayerfunction.You’ll also need to pass the instance of the
Gameto thePlayerso it knows whichGameto grab it’s data from.Here’s an example of #3 (the one I would try):