c++ - 为什么 SFML 中的 sf::Clock 会自行重置?

标签 c++ sfml

我正在用 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/

相关文章:

c++ - SFML2 应用程序找不到共享对象

c++ - 让 Sprite 跟随鼠标,使用 View 时出现奇怪的故障

c++ - boost c++ 中的 XML RSI(KUKA 机器人格式)解析库?

c++ - 没有得到 cout 的输出

c# - 如何下载整个网页

c++ - 回答对二进制数组的查询

c++ - sf::音乐播放但不是 sf::声音?为什么?

c++ - 乒乓 Racket 未按预期运行

c++ - 多个 Cmake 文件和 SFML "fatal error: SFML/Graphics.hpp: No such file or directory"

c++ - c库链接到什么点