Rob*_*yes 2 javascript prototype runtime-error node.js
这似乎是这个网站上一个受欢迎的问题,但以前的答案并没有解决这个问题的实例.
我在node.js服务器上有一个游戏引擎的开头,但是当我设置它时,我在loop方法中出现以下错误:Object #<Timer> has no method update.
我以为我正在设置原型以获得更新方法 GameEngine.prototype.update = function(){ ... };
任何帮助解决这个问题将非常感激.谢谢.
这是整个代码:
function GameEngine(){
this.fps = 1000/60;
this.deltaTime = 0;
this.lastUpdateTime = 0;
this.entities = [];
}
GameEngine.prototype.update = function(){
for(var x in this.entities){
this.entities[x].update();
}
}
GameEngine.prototype.loop = function(){
var now = Date.now();
this.deltaTime = now - this.lastUpdateTime;
this.update();
this.lastUpdateTime = now;
}
GameEngine.prototype.start = function(){
setInterval(this.loop, this.fps);
}
GameEngine.prototype.addEntity = function(entity){
this.entities.push(entity);
}
var game = new GameEngine();
game.start();
Run Code Online (Sandbox Code Playgroud)
这似乎是这个网站上的一个热门问题
是.
但以前的答案并没有解决这个问题的实例.
真?你找到了哪些?
this当函数由超时/事件监听器/等执行时,"method"()的上下文将丢失.
GameEngine.prototype.start = function(){
var that = this;
setInterval(function(){
that.loop();
}, this.fps);
}
Run Code Online (Sandbox Code Playgroud)
要么
GameEngine.prototype.start = function(){
setInterval(this.loop.bind(this), this.fps);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1424 次 |
| 最近记录: |