So I have stumbled across some example game code that runs on Phaser's html 5 game engine. Below is the example code
var game = new Phaser.Game(800, 600, Phaser.AUTO, '', { preload: preload, create: create, update: update });
function preload() {
game.load.image('someKey','someUrl');
}
function create() {
}
function update() {
}
I'm trying to figure out how phaser is able to accomplish passing in functions (preload,create, and update) to the game object by referring to the function declarations and adding images, for example, to the object via the variable name 'game'. The example provided above is what I have generally seen around the web (or at least something similar).
Below, I have started working on my own game engine (for fun) in order to imitate phaser and understand their though process. Unfortunately, I can't successfully accomplish the same effect as seen above.
My Engine.js (Phaser copycat file)
function Game(holderId,canvasOptions,functionDeclarations){
this.test="testing";
functionDeclarations.init();//this does not work :( because game in init() is undefined.
}
My Main.js
var game= new Game("canvas-holder",{width:1500,height:600, unit:"px"},{init:init,core:core,update:update});
function init(){
console.log(game.test);//game is undefined. Seems like a scope issue
}
I sense this is a scoping issue. I tried looking through phaser's source code, but it contains 97k lines, and ctrl+f was of no help.