javascript OOP concept issue -
javascript OOP concept issue -
this question has reply here:
how access right `this` / context within callback? 4 answersi have simple code :
// can utilize either pixi.webglrenderer or pixi.canvasrenderer var renderer; var stage; var bunnytexture; var bunny; var game= {}; game.init = function() { }; game.init.prototype={ init:function(){ renderer= new pixi.webglrenderer(800, 600); document.body.appendchild(renderer.view); stage= new pixi.stage; bunnytexture= pixi.texture.fromimage("img/ninja.png"); bunny= new pixi.sprite(bunnytexture); bunny.position.x = 400; bunny.position.y = 300; stage.addchild(bunny); }, animate:function(){ bunny.rotation += 0.01; renderer.render(stage); requestanimationframe(this.animate); } } requestanimationframe(game.init.animate);
i calling function :
window.onload = function () { game.init(); };
a javascript error saying : argument 1 of window.requestanimationframe not object.
since game.init
constructor function should initialize object new
keyword:
new game.init();
otherwise context global object window
, this.animate
undefined
. help observe such problems recommend use strict
mode, have failed error on steps.
javascript
Comments
Post a Comment