为什么我的sf :: clock在sfml中单独重置

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

本文关键字:单独重 sfml clock 我的 sf 为什么      更新时间:2023-10-16

我正在从事C 的小SFML游戏。我要做的是使用介绍性文字开始游戏,该文字闪烁两种不同的颜色,例如" Arturo Girona Presents"。我通过使用一个内部内部(window.isopen())循环的计数器来使它起作用,但是后来我尝试使用sf :: clock来查看它是否使闪烁看起来不那么随机。但是,由于某种原因,当我也没有明确告诉它时,每个循环后时钟都在每个循环后不断重置,因此它不会从文本屏幕过渡。时钟如何单独重置,我该如何修复?这是我的时循环:

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