I'm making a very basic space invaders game, but having some trouble with the collisionDetection. enemy are the ones I need to hit, they are inside "fiende". "missile" is the div for the missiles.
I haven't tried much, since I'm very new to js, hence I don't have enough knowledge to improve on it.
EDIT: After some testing, I've found out that the collisionDetection breaks when the animations is running.
function collisionDetection() {
for (var enemy = 0; enemy < fiende.length; enemy++) {
for (var missile = 0; missile < missiles.length; missile++) {
if (
missiles[missile].left >= fiende[enemy].left &&
missiles[missile].left <= (fiende[enemy].left + 50) &&
missiles[missile].top <= (fiende[enemy].top + 50) &&
missiles[missile].top >= fiende[enemy].top
) {
fiende.splice(enemy, 1);
missiles.splice(missile, 1);
}
}
}
}
HTML
<div id="background">
<div id="missiles"></div>
<div id="fiende"></div>
</div>
CSS
div.missile1 {
width: 10px;
height: 28px;
background-image: url('missile1.png');
position: absolute;
}
div.enemy {
width: 50px;
height: 50px;
background-image: url('enemy.png');
position: absolute;
}
Some of the animation, it's basically the same after that.
@keyframes bevegelse {
0% {
left: -230px;
top: 0%;
}
5% {
left: 250px;
top: 0%;
}
10% {
left: 250px;
top: 40px;
}
15% {
left: -230px;
top: 40px;
}
When a missile hits the enemy, it destroys some other enemies, which the missile didn't hit. Sometimes it doesn't even destroy the randoms ones.
friendeandmissilesvariables or pass them as arguments tofunction collisionDetection"fiende" and "missile" are div(s) in html.. Then you are doingfiende.length. The JavaScript object for div doesn't have a length property.