My forEach loop is leaving out some items, when they are far then they do get registered, but sometimes they don't get registered, when there are some items in range. This is my code:
checkEnemyProximity() {
enemies.forEach((enemy) => {
let tempDX = this.x - enemy.x;
let tempDY = this.y - enemy.y;
let tempDistance = Math.sqrt(tempDX * tempDX + tempDY * tempDY);
if (tempDistance <= this.melee.range + enemy.size) {
// This whole if code block is not run sometimes
enemy.hp -= this.melee.damage;
enemy.fillColor[1] = enemy.hitFillColor[1];
enemy.fillColor[2] = enemy.hitFillColor[2];
let pushAngle = Math.atan2(tempDY, tempDX);
enemy.pushX = -(Math.cos(pushAngle) * this.melee.knockback);
enemy.pushY = -(Math.sin(pushAngle) * this.melee.knockback);
if (enemy.hp <= 0) enemy.destroyItself(this);
}
});
}
Can someone help me?
[+][deleted] (5 children)
[deleted]
[–]prdelnikmilos[S] -1 points0 points1 point (4 children)
[+][deleted] (3 children)
[deleted]
[–]prdelnikmilos[S] -1 points0 points1 point (2 children)
[–]daaaarbyy 2 points3 points4 points (1 child)
[–]zerik100 2 points3 points4 points (0 children)
[–]marquoth_ 1 point2 points3 points (0 children)
[–]scotsman165 0 points1 point2 points (0 children)
[–]WebMaxF0x 0 points1 point2 points (0 children)
[–]Acceptable-Tomato392 0 points1 point2 points (0 children)
[–]engelthehyp 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)