I am learning p5.js and I have come upon a problem that I cannot yet solve.
So I'm making this little game in which you have to shoot the enemies and dodge their skills. This is how to looks:
On the right side we have the player, in the middle the bullet ( that is handled with an array ) and on the left side the enemies ( an array ). Both enemies and the bullets have their own object. This is the code to spawn the enemies and the bullets:
For bullets
function shoot() {
x = p.x + 6.5;
y = p.y + 12.5;
bullets.push(new Bullet(x, y));
}
for enemies:
function spawnEnemies() {
let x = 650;
let y = 30;
for (let i = 0; i < 14; i++) {
enemies.push(new Enemy(x, y));
y += 40
}
}
What I am trying to do, is to detect the collision when the bullet reaches the targets. This is how I tried it:
// check hit
for (let i = 0; i < bullets.length; i++){
for (let j = 0; j < enemies.length; j++){
let d = dist(bullets[i].x, bullets[i].y, enemies[j].x, enemies[j].y)
if (d < 1){
console.log('hit');
}
}
}
The check hit
code is inside of p5's draw
function, therefore it is executed continuously. Can anyone help me out?
Thank you in advance!