0

So I'm making a simple html5 Canvas game for educational purposes. I have an array which holds all of the separate prototypes of the same enemy object. When the enemy goes off the screen or perhaps is killed by player, I want to be able to despawn it from within the object by deleting it altogether from the array. I'll probably create new instances of the enemy by pushing a new object to the array with some pseudo-random properties.

I apologize for not providing code, if you'd like to see anything specific just ask.

4

1 回答 1

1

您可以使用Array.prototype.splice,它需要两个参数index, howMany从数组中删除元素,从指定的开始index

这是一个基本的例子

var players = [1,2,3,4,5,6]
players.splice (3,1); //remove 1 element beginning at index 3
console.log (players) // [1,2,3,5,6]

如您所见,玩家 4 (在 player 中的索引 3 处)已被删除

于 2013-08-19T09:39:53.797 回答