Javascript 从函数中删除对象

Javascript removing an object from a function

我正在创建一个游戏,其中有一个对象数组,其中一个数组包含所有导弹对象。这个数组需要在导弹撞到墙壁(或玩家)时经常从中移除导弹,但我似乎无法找到一种方法来从数组中移除对象而不会得到混乱的代码。最好我想要一种从其中一个对象函数中删除对象的方法,例如 missiles.splice(this,1).

该段代码示例如下

Missiles.prototype.Draw = function () {
    if (this.x >= -200) {
        ctx.beginPath();
        ctx.fillStyle = "Black";
        ctx.rect(this.x,this.y,this.width,this.height);
        ctx.stroke();
        ctx.fill();
    } else {
        missileCount.splice(this,1);
        console.log('removed a missile')
    }
};

但是,当我使用它时,屏幕上的随机导弹消失了

我有一个包含我的代码的 pastebin(使用以下 link 它可能会有所帮助) http://pastebin.com/f5S8zhJg

游戏地图无限大,挺有意思的。

missileCount 是一个数组。

splice 需要 2 个整数。

this 是导弹的实例。

无需费力地浏览您的代码,我认为您想要:

missleCount.splice(missleCount.indexOf(this),1);