javascript - 有没有办法让这段代码知道 g 已定义?
问题描述
请帮忙,我不知道为什么这会引发错误。我已经尝试了我能想到的一切,为什么 this.runLoop() 被定义而突然 this.processInput() 没有,我试图更好地理解类,我终于认为我明白了,我猜不是
class Game {
constructor(teamSize){
this.cpu1 = new Array(teamSize-1);
this.cpu2 = new Array(teamSize);
this.run = -1;
this.init();
}
init(){
}
shutdown(){};
processInput = function(elapsed) {
console.log(elapsed);
}
updateGame(elapsed){}
generateOutput(elapsed){
}
runLoop(timestamp){
let last;
if(last === undefined){
last = timestamp;
}
let elapsed = timestamp - last;
this.processInput(elapsed);
this.updateGame(elapsed);
this.generateOutput(elapsed);
last = timestamp;
if(this.run == 1){
window.requestAnimationFrame(this.runLoop);
}
};
start(){
this.run *= -1;
if(this.run == 1){
window.requestAnimationFrame(this.runLoop);
}
}
};
var g = new Game(5);
g.start();
错误是否与 window.requestAnimationFrame() 有关系,它会成为“this”所指的对象吗?
解决方案
推荐阅读
- elasticsearch - 如何删除搜索发现中的重复项
- python - 读取大文件后,'pwd' 命令需要花费大量时间(例如:40M)
- php - 导航中的工作计数
- google-analytics - 如何避免单个标签容器将数据发送到同一帐户的不同属性
- macos - Zerotier - MacOS 上的 PORT_ERROR
- powercli - 如何创建桌面池地平线 powercli
- c# - 如何在 ASP.NET Core 中使用密码在运行时获取连接字符串
- android - Recyclerview 在 NestedScrollview 内时自动启动视频
- php - 使用 Laravel API 端点在重定向时获取经过身份验证的用户
- c++ - 在 getline 上检查 eof() 即 while(!getline().eof()) 和简单地检查 while(getline()) 有什么区别