I am creating a game that randomly displays circles onto a canvas. The circles objects are added to an array and whenever the player collides with one of them I want to remove that object. Here is my code currently for the collision -
for(var i = 0; i < currentGame.items.length; i++)
{
if (player1.x < currentGame.items[i].x + currentGame.items[i].radius*2 && player1.x + currentGame.items[i].radius*2 > currentGame.items[i].x &&
player1.y < currentGame.items[i].y + currentGame.items[i].radius*2 && player1.y + player1.car.height > currentGame.items[i].y) {
currentGame.score++;
position = currentGame.items.indexOf(i);
currentGame.items.splice(position, 1);
}
}
This code works fine when the player hits the last circle that has been added to the array/canvas. However, if the player hits circles that are in the middle of the array then all the subsequant items of the array will also be removed (not the previous ones). The players score will be increased by however many circles get deleted. This suggests that when one of the circles is removed the items are shifted down and take the place of the one just deleted, including taking it's position coordinates so then the player collides with all of them and they are all then deleted.
I don't know how to fix this, or if I am using splice incorrectly.
Here is my code for adding to the array -
function createItem(){
item1 = new itemSmall();
item1.x = Math.floor(Math.random()*((currentGame.windowWidth - 40)-40+1)+40);
item1.y = Math.floor(Math.random()*((currentGame.windowHeight - 40)-40+1)+40);
item1.fillStyle = "rgb(200,80,200)";
item1.lineWidth = 4; //Stroke Thickness
item1.strokeStyle = "rgb(255,215,0)";
currentGame.items.push(item1);
}
items is stored here (I removed everything else from this object for clarity) -
function gameValues(){
this.items = [];
}
currentGame = new gameValues();