使用 sf::Time 进行循环

Making a loop with sf::Time

我可以使用 sf::time class 进行循环吗,因为当我尝试这样做时我的程序崩溃了。

while(time.asSeconds() <= 10.0f)

这是我在 sf::Time class 中使用的 while 循环。

编辑:

sf::Clock clock; // Initializing clock starts counting time.
while(someCondition)
{
    sf::Time time = clock.getElapsedTime();
    //It's going to execute do something() until time is higher than 10.0f
    while(time.asSeconds() <= 10.0f)
    {
        //we need this to ensure that we won't fall into the infinite loop
        time = clock.getElapsedTime();
        do something()
        //Optional: clock.restart(); If you want your code to be executed every x seconds.
    }
}

首先你必须创建 sf::Clock 变量,然后从时钟获取时间到 sf::Time 变量。