如何找出这个不可能的错误?

时间:2017-05-08 06:32:58

标签: javascript debugging

不幸的是,我无法提供减少代码示例,因为在主游戏更新循环运行11次以上后会发生错误,但我希望项目很简单。

这是项目的链接: https://github.com/marko-avlijas/udacity-frogger

Project是udacity游戏项目,它附带3个文件,但我只修改了app.js,以便这个bug必须在哪里。

问题始于engine.js函数UpdateEntities,但我还没有改变。

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)

在debuggger中,我看到this设置为window,而不是enemywindow未定义area,因此我收到了该错误。

我无法解决这个问题。请帮忙。

也欢迎提供有关如何调试此功能的提示。

我设置了一个计数器,看看它updateEntities()被调用了多少次,当我收到错误时它是11。但是当我11次将if语句放到console.log时,错误发生在第13次。当我第13次停止时,没有错误,稍后会发生。

1 个答案:

答案 0 :(得分:1)

问题出在.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;//<====
};