Greetings! I am diving into JavaScript and trying my hand at creating a game. The concept is simple - a ship shooting lasers at alien spacecrafts. However, I've encountered a problem with the destroy function that has left me scratching my head. Surprisingly, the alien ships seem to survive multiple laser hits until the final blow strikes them down. I need some guidance on how to activate the destroy function as soon as any laser touches an alien craft. Below, you'll find the relevant section of code (I believe) along with a link to the jsfiddle for reference. http://jsfiddle.net/235mX/
function drawLaser(){
laserList.forEach(function(Laser){
Laser.y = Laser.y - 1;
Ly = Laser.y;
Lx = Laser.x;
ctx.beginPath();
ctx.fillStyle = Laser.color;
ctx.arc(Laser.x, Laser.y, 10, 2 * Math.PI, false);
ctx.fill();
ctx.closePath();
ctx.beginPath();
ctx.fillStyle = 'white';
ctx.arc(Laser.x, Laser.y + 10, 10, 2 * Math.PI, false);
ctx.fill();
ctx.closePath();
});
}
if(Lx >= Ax - 30 && Lx < Ax + 30 && Ly < Ay + 30 && Ly > Ay + 10){
destroyX = Lx;
destroyY = Ly;
enemy1 = 0;
destroy();
}
if(Lx >= Ax + 100 - 30 && Lx < Ax + 100 + 30 && Ly < Ay + 30 && Ly > Ay + 10){
destroyX = Lx;
destroyY = Ly;
enemy2 = 0;
destroy();
}
if(Lx >= Ax + 200 - 30 && Lx < Ax + 200 + 30 && Ly < Ay + 30 && Ly > Ay + 10){
destroyX = Lx;
destroyY = Ly;
enemy3 = 0;
destroy();
}
if(Lx >= Ax + 300 - 30 && Lx < Ax + 300 + 30 && Ly < Ay + 30 && Ly > Ay + 10){
destroyX = Lx;
destroyY = Ly;
enemy4 = 0;
destroy();
}