The Problem
I have created a constructor called Game
. Then, I tried to expand it's functionality by adding two new functions (update
and render
) using prototype.
However, I'd like my update
function to be able to call both itself and then call render
.
My Code
var Game = function(){
};
Game.prototype.update = function(){
requestAnimationFrame(this.render);
requestAnimationFrame(this.update);
};
Game.prototype.render = function(){
console.log('Rendering');
};
var game = new Game();
game.update();
What I have also Tried
requestAnimationFrame(render);
requestAnimationFrame(update);
And..
requestAnimationFrame(Game.render);
requestAnimationFrame(Game.update);
And...
requestAnimationFrame(Parent.render);
requestAnimationFrame(Parent.update);
But due to some (glaringly obvious) gaps in my javascript knowledge, I can't make this work. It appears as if this
and parent
are both referring to window
- I'm guessing that's because of how the functions were created.
This is the error I receive;
Game.js:6 Uncaught TypeError: Failed to execute
'requestAnimationFrame' on 'Window': The callback provided as
parameter 1 is not a function.
Questions I've already found
I've already found the following questions on SO, but they don't appear to be all that helpful to this particular problem.
Javascript multiple prototype functions - how to call one from another
calling function from prototype function
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…