我试图运行下面的代码,但在gameLoop函数中出现错误:JavaScript运行时错误:对象不支持属性或方法'更新'。对象不支持方法
我是一个开始的JavaScript程序员。你能发现这个代码有什么问题吗?
function Core(context) {
this.context = context;
this.fps = 500;
this.sprite = new Sprite();
}
Core.prototype.run = function() {
setInterval(this.gameLoop, this.fps); // <<<< PROBLEM
}
Core.prototype.gameLoop = function() {
this.update();
this.draw();
}
Core.prototype.update = function() {
this.sprite.x += 50;
this.sprite.y += 50;
}
Core.prototype.draw = function() {
this.context.clearRect(0, 0, 300, 300);
this.context.fillRect(this.sprite.x, this.sprite.y, 50, 50);
this.context.fillText('x: ' + this.sprite.x + ' y: ' + this.sprite.y, 10, 250);
}
固定码:http://pastebin.com/xuNTQSrP - 'this'在'Core.prototype.gameLoop'没有解决的。预期的,因为'setInterval'。 – 2012-03-25 11:03:06
@Milosz:没有必要,只有当'gameLoop'被**调用**时才会被定义,而不是定义它时。 – 2012-03-25 11:03:19