-1
\$\begingroup\$

I am making a canvas game where you shoot at an endless amount of enemies. My problem is that the I am not sure how to make the objects from the bullets array collide with those of the enemy array. Here is what I have:

enemies.forEach(hitCheck)

Then:

var hitCheck = function(object){
  var a = bullets.length

  if (bullets[a].x < object.x + object.width  && bullets[a].x + bullets[a].width  > object.x &&
        bullets[a].y < object.y + object.height && bullets[a].y + bullets[a].height > object.y) {object.x += 400}
}  
\$\endgroup\$
0
\$\begingroup\$

I think the problem here is that you're not iterating over the bullets array.

Instead of just setting a to bullets.length, use a for loop inside hitCheck:

for (var a = 0; a < bullets.length; a++) {
    if (bullets[a].x < object.x + object.width
                  && bullets[a].x + bullets[a].width  > object.x
                  && bullets[a].y < object.y + object.height 
                   && bullets[a].y + bullets[a].height > object.y) {
         object.x += 400
    }
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.