我正在用 C++ 开发一个小型 SFML 游戏。我想做的是用闪烁两种不同颜色的介绍性文字开始游戏,例如“Arturo Girona Presents”。我通过使用在 while (window.isOpen()) 循环内递增的计数器使其工作,但随后我尝试使用 sf::Clock 来查看它是否使闪烁看起来不那么随机。然而,由于某种原因,当我也没有明确告诉它时,时钟会在每次循环后不断重置,因此它不会从文本屏幕转换。时钟如何自行重置?如何修复? 这是我的 while 循环:
int count = 0;
sf::Clock clock;
int timer = clock.getElapsedTime().asMilliseconds();
while (window.isOpen())
{
count++;
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
window.clear();
while (timer < 5000)
{
if (timer < 200 && introText1.getColor() == sf::Color::Red)
{
introText1.setColor(sf::Color::White);
window.draw(introText1);
window.display();
}
if (timer < 200 && introText1.getColor() == sf::Color::White)
{
introText1.setColor(sf::Color::Red);
window.draw(introText1);
window.display();
}
break;
}
if (timer == 5000) {
window.clear();
window.draw(door);
window.draw(doorWindow1);
window.display();
}
cout << timer << endl;
if (timer > 0)
cout << "Time is moving" << endl;
}
最佳答案
时钟没有被重置,您根本就没有读取到新值。 ;-)
移动int timer = clock.getElapsedTime().asMilliseconds();
在主循环内部。
此外,您不需要 while (timer < 5000)
循环尤其是 break
正如最后一句话。
关于c++ - 为什么 SFML 中的 sf::Clock 会自行重置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37862083/