Cannot reference method on Javascript object -


i'm writing version of pong in javascript. have game object , i'm using prototype property define methods on it. i'm getting following error: "undefined not function". being thrown in game.prototype.step function this.update undefined in there. here's code game object:

(function(root) {   var pong = root.pong = (root.pong || {});    var game = pong.game = function() {     this.canvas = document.getelementbyid('canvas');     this.canvas.width = 800;     this.canvas.height = 400;     this.context = canvas.getcontext('2d');     this.maxstartspeed = 10;     this.keysdown = {};     this.player2 = new pong.player({'player': 2});     this.player1 = new pong.player({'player': 1});     this.ball = new pong.ball(400, 200);   }    game.prototype.update = function() {     this.player1.update();     this.player2.update();     this.ball.update(player1.paddle, player2.paddle);   };    game.prototype.render = function() {     this.context.fillstyle = "#bdc3c7";     this.context.fillrect(0, 0, width, height);     this.player1.render();     this.player2.render();     this.ball.render();   };     game.prototype.animate =  function(callback) {      window.settimeout(callback, 1000/60)    };    game.prototype.step = function() {     this.update();     this.animate(this.step);   };    window.addeventlistener("keydown", function (event) {     game.keysdown[event.keycode] = true;   });    window.addeventlistener("keyup", function (event) {     delete game.keysdown[event.keycode];   });    window.onload = function() {     document.getelementbyid('canvas-container').appendchild(canvas);     game = new game();     game.animate(game.step);   };    })(this); 

the settimeout going change scope. maintain proper scope, need use bind

change

this.animate(this.step); 

to

this.animate(this.step.bind(this)); 

you need same thing other animate calls.


Comments

Popular posts from this blog

windows - Single EXE to Install Python Standalone Executable for Easy Distribution -

c# - Access objects in UserControl from MainWindow in WPF -

javascript - How to name a jQuery function to make a browser's back button work? -