当我在游戏运行时尝试创建射弹时,会弹出两条Microsoft Visual C ++运行时库消息
Debug Assertion Failed! Program: C:\WINDOWS\SYSTEM32\MSVCP120D.dll
File: c:\program files (x86)\microsoft visual studio
12.0\vc\include\vector Line:1201 Expression: vector subscript out of range
和
Debug Assertion Failed!
Program: C:\SFML-2.3.2\SFML\Debug\SFML.exe
File: c:\program files (x86)\microsoft visual studio 12.0\vc\include\vector
Line:1202
Expression: "Standard C++ Libraries Out of Range" && 0
然后是Microsoft Visual Studio Express 2013的消息:
SFML.exe中0x5B89A893(msvcr120d.dll)的未处理异常: 无效参数传递给认为无效的函数 参数致命。
只要按空格键创建弹丸(绿色方块),就会发生这种情况。绿色方块出现在屏幕上,然后立即弹出消息。此时使用当前代码的抛射物应该表现为绿色方形画笔,但会弹出此消息。这是我的源代码:
int main ( )
{
int counter1 = 0;
int counter2 = 0;
int counter3 = 0;
//create the main window
sf::RenderWindow window(sf::VideoMode(640, 480), "SFML Demo");
sf::Event event;
sf::Vector2i source(1, 0);
//load texture
sf::Texture texturePlayer;
if (!texturePlayer.loadFromFile("images/player.png"))
return EXIT_FAILURE;
//class object
class player Player1;
Player1.sprite.setTexture(texturePlayer);
//Projectile vector array
vector<projectile>::const_iterator iter;
vector<projectile> projectileArray;
//projectile object
class projectile projectile1;
while ( window.isOpen() )
{
while (window.pollEvent(event))
{
if ( event.type == sf::Event::EventType::Closed )
window.close();
}
window.clear();
//fire missile (space bar)
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Space))
{
projectile1.rect.setPosition(Player1.rect.getPosition());
projectileArray.push_back(projectile1);
}
//Draw projectile
for (iter = projectileArray.begin(); iter != projectileArray.end(); iter++)
{
window.draw(projectileArray[counter1].rect);
counter1++;
}
//update player with rectangle collision detection
Player1.update();
Player1.updateMovement();
//window.draw(Player1.rect);
window.draw(Player1.sprite);
window.display();
}
}
class projectile:公共实体//从实体继承 { 公共:
float movementSpeed = 10;
int attackDamage = 5;
projectile(); //constructor
//void update();
//void updateMovement();
};
#endif
projectile::projectile() {
rect.setSize(sf::Vector2f(10.0, 10.0));
rect.setFillColor(sf::Color::Green);
rect.setPosition(0, 0);
//sprite.setTextureRect(sf::IntRect(0, 0, 32, 32)); }
我该如何解决这个问题?任何建议/帮助非常感谢。提前致谢
答案 0 :(得分:0)
在周期Draw projectile
中您必须使用或迭代器来访问projectileArray
中的元素
for (iter = projectileArray.begin(); iter != projectileArray.end(); iter++)
{
window.draw(iter->rect);
}
或在周期之前初始化counter1=0;
否则在主while
counter1
的第二次迭代中将不会指向向量projectileArray
的开头。