C++ 我的RenderWindow变为空白并停止工作
事实上,我正在尝试制作一个子弹射击代码。我使用了向量,并将精灵添加到其中,以及在不同向量中的位置。但是当我运行程序时,我的窗口停止工作。这是代码。我希望它不会重复C++ 我的RenderWindow变为空白并停止工作,c++,sfml,C++,Sfml,事实上,我正在尝试制作一个子弹射击代码。我使用了向量,并将精灵添加到其中,以及在不同向量中的位置。但是当我运行程序时,我的窗口停止工作。这是代码。我希望它不会重复 #include <SFML/Graphics.hpp> #include <iostream> #import "bulletcode.h"; #include <vector> using namespace std; using namespace sf; int main() { vecto
#include <SFML/Graphics.hpp>
#include <iostream>
#import "bulletcode.h";
#include <vector>
using namespace std;
using namespace sf;
int main()
{
vector<Sprite> bullets;
vector<float> xp;
vector<float> yp;
sf::RenderWindow window(sf::VideoMode(900, 600), "SFML works!");
sf::CircleShape shape(75.f);
shape.setFillColor(sf::Color::Green);
Texture bullet;
bullet.loadFromFile("bullet.png");
shape.setPosition(400,100);
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
if(Keyboard::isKeyPressed(Keyboard::Space)){
Sprite bulletsp;
bulletsp.setTexture(bullet);
bulletsp.setScale(0.8,0.8);
bullets.push_back(bulletsp);
xp.push_back(shape.getPosition().x);
yp.push_back(shape.getPosition().y);
}
for(int i=0;i<=bullets.size()-1;i=i){
yp[i]=yp[i]+0.2;
i++;
bullets[i].setPosition(xp[i],yp[i]);
window.draw(bullets[i]);
}
window.clear();
window.draw(shape);
window.display();
}
return 0;
}
#包括
#包括
#导入“bulletcode.h”;
#包括
使用名称空间std;
使用名称空间sf;
int main()
{
矢量子弹;
向量xp;
载体yp;
sf::RenderWindow窗口(sf::VideoMode(900600),“SFML工作!”;
sf:圆形(75.f);
setFillColor(sf::Color::Green);
纹理子弹;
bullet.loadFromFile(“bullet.png”);
形状、设置位置(400100);
while(window.isOpen())
{
sf::事件;
while(window.pollEvent(事件))
{
如果(event.type==sf::event::Closed)
window.close();
}
如果(键盘::isKeyPressed(键盘::空格)){
雪碧弹;
bulletsp.setTexture(bullet);
bulletsp.设置刻度(0.8,0.8);
子弹。推回(子弹);
xp.push_back(shape.getPosition().x);
yp.推回(shape.getPosition().y);
}
对于(inti=0;i您在循环中增加i。因此,在上一次迭代中,您将访问超出数组末端的部分
您可能希望将循环声明中的i=i
替换为i++
,而不是在循环体中增加i。由于for循环,您的程序无法工作:
for(int i=0;i<=bullets.size()-1;i=i)
for(int i=0;i#导入“bulletcode.h”
?如果读的是#包括“bulletcode.h”
?for(int i=0;我请编辑你的问题,而不是添加注释。标签下面有一个链接。#包含“bulletcode.h”;i=i)
没有意义,只需写)
并确保在您真正想要的位置(通常在整个循环体之后)递增i
,即i=i
现在的位置。然后您还有其他问题要调试。;)再快速一看,您可能永远看不到项目符号,因为您在绘制项目符号后正在清除窗口。