Closed dsyenket closed 6 years ago
My guess is that you're performing the splice inside the update enemies loop? If you splice during that loop, you'll miss updating one of the enemies in the list. That might be causing the flicker.
enemies = [0,1,2]
for (var i = 0; i < enemies.length; i++) {
console.log(enemies[i]);
if (enemies[i] === 0) enemies.splice(i,1);
}
// => output will be "0" "2"
Any idea on how to make the enemy disappear when the player hits the enemy but will not cause it to flicker?
If that's the cause of the flicker, you can manually increment i
when you don't splice instead of letting the loop do it.
for (var i = 0; i < enemies.length; ) {
console.log(enemies[i]);
if (enemies[i] === 0) enemies.splice(i,1);
else i++;
}
This is the function that was used to update enemies position and draw. function updateEnemies() { // animate enemies for (var i = 0; i < enemies.length; i++) { enemies[i].update(); enemies[i].draw();
// player ran into enemy
if (player.minDist(enemies[i]) <= player.width - platformWidth/2) {
//gameOver();
if (enemies[i] === 0)
{
enemies.splice(i, 1);
}
else
{
i++;
}
}
}
// remove enemies that have gone off screen if (enemies[0] && enemies[0].x < -platformWidth) { enemies.splice(0, 1); } }
It's not removing the enemy as it should.
remove the if (enemies[i] === 0)
and just leave the enemies.splice(i,1)
.
Thank you very much, it works.
Hi, I changed the updateEnemies function a little bit by removing the gameOver() function if the player collides with enemy and replace it with splice function. However I noticed when the enemy that collides with the player gets removed, the other enemies nearby will have the flicker effect. any idea how to solve this? Thanks.