C++ SFML游戏中的向量下标超出范围
我正在做一个游戏,作为我游戏开发课程的一个项目。它使用矢量在屏幕顶部生成敌人,并将其击溃,同时创建由玩家和老板共同发射的“子弹”。在添加了boss和子弹代码之后,以前的工作代码现在抛出了一个向量下标超出范围的错误。此错误总是在击中敌人后立即抛出,并且在引入boss之前不会发生C++ SFML游戏中的向量下标超出范围,c++,sfml,C++,Sfml,我正在做一个游戏,作为我游戏开发课程的一个项目。它使用矢量在屏幕顶部生成敌人,并将其击溃,同时创建由玩家和老板共同发射的“子弹”。在添加了boss和子弹代码之后,以前的工作代码现在抛出了一个向量下标超出范围的错误。此错误总是在击中敌人后立即抛出,并且在引入boss之前不会发生 bullets[i].shape.move(bullets[i].currVelocity); //Bullet Removal if (bulle
bullets[i].shape.move(bullets[i].currVelocity);
//Bullet Removal
if (bullets[i].shape.getPosition().x < 0 || bullets[i].shape.getPosition().x > window.getSize().x
|| bullets[i].shape.getPosition().y < 0 || bullets[i].shape.getPosition().y > window.getSize().y) {
bullets.erase(bullets.begin() + i);
}
else {
//Collision Code
for (size_t k = 0; k < enemies.size(); k++) {
if (bullets[i].shape.getGlobalBounds().intersects(enemies[k].getGlobalBounds())) {
bullets.erase(bullets.begin() + i);
enemies.erase(enemies.begin() + k);
score = score + 1;
fiveScore = fiveScore + 1;
std::cout << "Score: " << score << std::endl;
hitSound.play();
if (fiveScore == 5) {
fiveScore = 0;
//scoreUpSound.play();
}
if (score >= 100 && bossSpawned == false) {
bossSpawned = true;
boss.setPosition(250.0f, 50.0f);
}
break;
}
}
if (bullets[i].shape.getGlobalBounds().intersects(boss.getGlobalBounds())) {
bullets.erase(bullets.begin() + i);
hitSound.play();
bossHP = bossHP - 1;
if (bossHP <= 0) {
bossSpawned = false;
boss.setPosition(3214.0f, 4322.0f);
text.setString("You have completed your quest." "\n" "The warlock is slain and the realm is" "\n" "safe, yet as life returns to the kingdom" "\n" "you are dissatisfied. Your brother and" "\n" "lover are both deceased and" "\n" "you feel only death will heal your pain.");
text.setPosition(25.0f, 150.0f);
window.clear();
window.draw(text);
window.display();
Sleep(9000);
return 0;
}
break;
}
}
}
项目符号[i].shape.move(项目符号[i].currVelocity);
//子弹清除
如果(项目符号[i].shape.getPosition().x<0 | |项目符号[i].shape.getPosition().x>window.getSize().x
||项目符号[i].shape.getPosition().y<0 | |项目符号[i].shape.getPosition().y>window.getSize().y){
项目符号.erase(项目符号.begin()+i);
}
否则{
//冲突代码
对于(大小k=0;k<0.size();k++){
如果(项目符号[i].shape.getGlobalBounds().intersects(敌人[k].getGlobalBounds())){
项目符号.erase(项目符号.begin()+i);
敌人。擦除(敌人。开始()+k);
分数=分数+1;
五芯=五芯+1;
std::cout在向量上循环时正在从向量中删除。这会改变向量的大小,这通常是错误的来源
查看一种替代方法。这与崩溃无关,但如果使用bossbullets.erase(bossbullets.begin()+z);
则会错过下一个元素。例如,当z=4
删除第5个元素时,for循环会将z
变量增加到5,因此新的(删除后)第5个元素将不被检查
b2.shape.setPosition(bossCenter);
b2.currVelocity = bossAimDirNorm * b2.maxSpeed;
bossbullets.push_back(BossBullet(b2));
}
for (size_t z = 0; z < bossbullets.size(); z++) {
bossbullets[z].shape.move(bossbullets[z].currVelocity);
//BossBullet Removal
if (bossbullets[z].shape.getPosition().x < 0 || bossbullets[z].shape.getPosition().x > window.getSize().x
|| bossbullets[z].shape.getPosition().y < 0 || bossbullets[z].shape.getPosition().y > window.getSize().y) {
bossbullets.erase(bossbullets.begin() + z);
}
else {
//Collision Code
if (bossbullets[z].shape.getGlobalBounds().intersects(player.getGlobalBounds())) {
bossbullets.erase(bossbullets.begin() + z);
playerDead = true;
text.setString("You have failed your quest.");
text.setPosition(150.0f, 150.0f);
window.clear();
window.draw(text);
window.display();
Sleep(5000);
return 0;
}
}
}