collideCounter = 0;
for (iter2 = projArray.begin(); iter2 != projArray.end(); iter2++)
{
collideCounter2 = 0;
for (iter3 = projArray.begin(); iter3 != projArray.end(); iter3++)
{
assert(collideCounter2 < enemy.enemyArray.size());
if (projArray[collideCounter].rect.getGlobalBounds().intersects(enemy.enemyArray[collideCounter2].enemySprite.getGlobalBounds()))
{
std::cout << "COLLIDE" << std::endl;
}
collideCounter2++;
}
collideCounter++;
}
Try adding this assert against the size to see if it fires. collideCounter must be within bounds, but you should use the iterator to get projArray[collideCounter] and remove the collideCounter variable.
How will the assert help? Look at the arrays. The first for loop is looping through the "projArray" vector. The second for loop is looping through... oops... the "projArray" vector again! He is then comparing "projArray" with "enemyArray", but using the length of "projArray" as a conditional for "enemyArray". Let's not forget that he is using iterators in the for loops without actually using them in his code. Look at my initial response. I eliminated the counters entirely by using the iterators for their intended purpose. The counters are redundant.
It is also recommended that you use ++iter, not iter++
You do have a point here, but even that is moot if he were to follow SmkViper's advice and use "for (const auto& item : myArray)". Using that, you get:
//Enemy projectile collide
for (const auto& iter2 : projArray){
for (const auto& iter3 : enemy.enemyArray){
if (*iter2.rect.getGlobalBounds().intersects(*iter3.enemySprite.getGlobalBounds()))
{
std::cout << "COLLIDE" << std::endl;
}
}
}