0

我正在调试代码,并看到在调用 erase() 时正确填充了实体和 _deferredKill 数组。问题是参数“item”没有定义,即使我看到参数“gGameEngine._deferredKill[i]”正在被传递。为什么它不接受我传递的被杀死的实体?我正在使用John Resig 的简单 javascript 继承

//In GameEngine.js
GameEngineClass = Class.extend({

    entities: [],
    _deferredKill: [],

    .........

    for (var k = 0; k < gGameEngine._deferredKill.length; k++){
        gGameEngine.entities.erase(gGameEngine._deferredKill[i]);
    }
}
gGameEngine = new GameEngineClass();

//in core.js, GameEngineClass is extended from this.  
Array.prototype.erase = function(item) {
for (var i = this.length; i--; i) {
    if (this[i] === item) this.splice(i, 1);
}
    return this;
};
4

1 回答 1

1

You're using the variable i when accessing the array index. This variable does not exist and therefore the value of item passed as an argument is consequently also undefined.

You should replace gGameEngine._deferredKill[i] with gGameEngine._deferredKill[k] to match your loop.

于 2013-10-16T22:52:30.190 回答