【发布时间】:2018-05-11 20:01:22
【问题描述】:
我试图实现一个 wator 模拟,如果鲨鱼吃鱼,我想随机生成鲨鱼,程序编译但我得到“不支持设置垂直同步”。
在 Ubuntu 16.04 上工作。在我从事其他工作之前,我遇到了同样的错误,但显示的窗口不是这样。有什么帮助吗?
编辑我已经修复了代码,我的循环中有太多{,但现在我收到“分段错误(核心转储)”错误我已将我的 png 更改为 8有点,但没有帮助。
#include <SFML/Graphics.hpp>
int main()
{
int n;
int x;
int y;
sf::RenderWindow window(sf::VideoMode(800, 800), "SFML works!");
// Set Frame Rate to 60fps
window.setFramerateLimit(60);
srand(time(0));
sf::Texture shark;
shark.loadFromFile("image.png");
std::vector<sf::Sprite> Fishes(n,sf::Sprite(shark));
for (int n = 0; n < Fishes.size(); n++){
Fishes[n].setOrigin(15, 15);
Fishes[n].getPosition();
Fishes[n].setPosition(x = rand() % 790 + 10, y = rand() % -10 - 50);
}
// run the program as long as the window is open
while (window.isOpen())
{
// check all the window's events that were triggered since the last iteration of the loop
sf::Event event;
while (window.pollEvent(event))
{
// "close requested" event: we close the window
if (event.type == sf::Event::Closed)
window.close();
}
Fishes[n].setPosition(x, y+=1);
Fishes[n].rotate(1);
// clear the window with black color
window.clear(sf::Color::Black);
// draw everything here...
// window.draw(...);
window.draw(Fishes[n]);
// end the current frame
window.display();
}
return 0;
}
【问题讨论】:
-
您的循环变量是
i,但您使用的是全局变量n。您在渲染中缺少一个完整的循环,只需再次使用此n。我想说你应该首先弄清楚你的逻辑(一个好主意可能是将全局变量限制为没有,这样你就可以看到你的范围)。垂直同步可能只是一个警告,一旦你修复它,你的程序应该没问题。 -
我搞定了...我有一个
{太多了 -
我已经用新的错误更新了我的答案