vendredi 27 octobre 2017

erase() last item of std::vector crash

I'm trying to create a game which includes oil particles getting blown up by fire, and I want my game to check if any fire particle is touching any oil particle. When it is touching, it calls a function and deletes the oil particle. All is fine and well until a fire particle touches the latest oil particle, then my game crashes and ti says that the vector erase iterator is outside of the range. I have put my code below.

    for (int a = 0; a < effVisMap[2].size(); ++a) {
        for (int i = 0; i < effVisMap[1].size(); ) {
            if (effVisMap[2][a].getGlobalBounds().intersects(effVisMap[1][i].getGlobalBounds())) {
                fireBurst(mollyExplosion * 1.2, effPhyMap[1][i].effx, effPhyMap[1][i].effy, fire);
                    effVisMap[2].erase(effVisMap[2].begin() + a);
                    effElaMap[2].erase(effElaMap[2].begin() + a);
                    effCloMap[2].erase(effCloMap[2].begin() + a);
                    effPhyMap[2].erase(effPhyMap[2].begin() + a);
            }
            else {
                ++i;
            }
        }
    }

Thanks a lot!

Aucun commentaire:

Enregistrer un commentaire