为什么我在 SFML 中的 sf::Clock 会自行重置?

Why is my sf::Clock in SFML resetting by itself?

我正在用 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 作为最后一个语句。