Javascript 如何找出这个不可能的错误?
不幸的是,我不能提供reduce代码示例,因为错误发生在主游戏更新循环运行了11次以上之后,但我希望项目足够简单 这是指向项目的链接: 这个项目是udacity游戏项目,它有3个文件,但我只修改了Javascript 如何找出这个不可能的错误?,javascript,debugging,Javascript,Debugging,不幸的是,我不能提供reduce代码示例,因为错误发生在主游戏更新循环运行了11次以上之后,但我希望项目足够简单 这是指向项目的链接: 这个项目是udacity游戏项目,它有3个文件,但我只修改了app.js,所以这就是bug的所在 问题开始于我没有更改的函数UpdateEntities中的engine.js function updateEntities(dt) { allEnemies.forEach(function(enemy) { // this is the
app.js
,所以这就是bug的所在
问题开始于我没有更改的函数UpdateEntities
中的engine.js
function updateEntities(dt) {
allEnemies.forEach(function(enemy) {
// this is the line which calls problem function
enemy.update(dt);
});
player.update();
}
然后在app.js
中调用:
Enemy.prototype.update = function(dt) {
// this is the line which is causing error
this.area.updateOuterLeft(this.area.outer.left + dt*this.speed);
if (this.area.outer.left > settings.canvas.width)
this.area.updateOuterLeft = -settings.canvas.width*2.5;
};
不知怎的,这在前11个月很好用?运行updateEntities()
的次数,但在此之后,我会出现此错误
我得到这个错误:
Uncaught TypeError: this.area.updateOuterLeft is not a function
at Enemy.update (file:///data/komp_tuts/udacity/oo_js/frontend-nanodegree-arcade-game-master/js/app.js:123:15)
at file:///data/komp_tuts/udacity/oo_js/frontend-nanodegree-arcade-game-master/js/engine.js:100:19
at Array.forEach (native)
at updateEntities (file:///data/komp_tuts/udacity/oo_js/frontend-nanodegree-arcade-game-master/js/engine.js:99:20)
at update (file:///data/komp_tuts/udacity/oo_js/frontend-nanodegree-arcade-game-master/js/engine.js:83:9)
at main (file:///data/komp_tuts/udacity/oo_js/frontend-nanodegree-arcade-game-master/js/engine.js:49:9)
在Debugger中,我看到这个
被设置为窗口
,而不是敌人
。
窗口
没有定义区域
,因此我得到了该错误
我想不出来。请帮忙
还欢迎提供有关如何调试此功能的提示
我设置了一个计数器,以查看它被调用了多少次
updateEntities()
,当我得到错误时,它是11次。但当我在11次将if语句放入console.log时,错误发生在第13次。当我在第13次停止时,没有出现错误,而且发生的时间稍晚。问题出现在.update
函数中,您将现有函数替换为this.area.updateOuterLeft=-settings.canvas.width*2.5代码>
现在,.updateOuterLeft
变成了浮点值而不是函数
Enemy.prototype.update = function(dt) {
// this is the line which is causing error
this.area.updateOuterLeft(this.area.outer.left + dt*this.speed);
if (this.area.outer.left > settings.canvas.width)
this.area.updateOuterLeft = -settings.canvas.width*2.5;//<====
};
敌方.prototype.update=功能(dt){
//这是导致错误的行
this.area.updateOuterLeft(this.area.outer.left+dt*this.speed);
if(this.area.outer.left>settings.canvas.width)
this.area.updateOuterLeft=-settings.canvas.width*2.5;//你是怎么这么快弄明白的?仅仅通过查看代码或一些高级chrome调试器功夫?这就像在代码中成为侦探,而你也是一个杀人犯……;)我遵循了下面的步骤1)在你的代码中,你已经调用了.updateOuterLeft
函数,并且有时中断2)这意味着有人正在更改.updateOuterLeft
.3)错误本身说This.area.updateOuterLeft不是一个函数
.4)现在开始查看哪一行是罪魁祸首