//SceneManager update Example

//SceneManager run method, which has this interesting requestUpdate method.
SceneManager.run = function(sceneClass) {
    try {
        this.initialize();
        this.goto(sceneClass);
        this.requestUpdate();
    } catch (e) {
        this.catchException(e);
    }
};

//requestUpdate method, which binds a new version of SceneManager's update function, and passes it to requestAnimationFrame.
SceneManager.requestUpdate = function() {
    if (!this._stopped) {
        requestAnimationFrame(this.update.bind(this));
    }
};

//The update method, which is being bound and being used, and repeated every frame.
SceneManager.update = function() {
    try {
        this.tickStart();
        this.updateMain();
        this.tickEnd();
    } catch (e) {
        this.catchException(e);
    }
};

//This is called by update, which then interesting enough calls requestUpdate  to start a feedback loop
SceneManager.updateMain = function() {

/* Lots of Code for updating things */
  
  //RequestUpdate is called again to bind our update again, and execute for another frame.
    this.requestUpdate();
};

/AutoRunner

function AutoRunner() {
  this._boundFunction = null; 
}

//takes a function and stores it in a variable
AutoRunner.bindFunctionToCall = function(fnc) {
  this._boundFunction = fnc;
}

//Executes the function stored in a variable
AutoRunner.executeBoundFunction = function() {
  this._boundFunction()
}

//Example of binding a function with specified parameters
function speak(text){
  console.log(text);
}

AutoRunner.bindFunctionToCall(speak.bind(this, "I'll Just say Hello at a later time"));
AutoRunner.executeBoundFunction();


function MyTimer() {
  this.initialize.apply(this, arguments)
}

MyTimer.prototype.initialize = function() {
  //Calls update, which will call requestUpdate later on -- entry point for our constant updates
  this.update();
}

MyTimer.prototype.update = function() {
  //Timer code to handle timers each frame
  //
  
  //calls requestUpdate -- , running our update method again, creating a feedback loop
  this.requestUpdate():
}

MyTimer.prototype.requestUpdate = function() {
  //Binds our new timer's update, allowing it's update to be called every frame with the specific parameters that we want -- in this case no parameters.
  requestAnimationFrame(this.update.bind(this));
}

%d bloggers like this: