我有这个简单的代码:
// You can use 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);
我正在调用这样的函数:
window.onload = function () { Game.Init(); };
javascript错误说:Argument 1 of Window.requestAnimationFrame is not an object.
答案 0 :(得分:1)
由于Game.Init
是构造函数,您应该使用new
关键字正确初始化对象:
new Game.Init();
否则,上下文将是全局对象Window
,this.animate
是undefined
。为了帮助您检测出这样的问题,我建议使用use strict
模式,这可能会在最早的步骤中出现错误。