SFML停止或删除时钟

时间:2018-01-05 23:12:03

标签: c++ sfml

是否可以在SFML中停止或删除时钟?我正在学习SFML,我做矩形变大,直到4秒后再重置。但是现在我正在考虑做类似的事情,但是我想要在4秒内留下矩形尺寸而不是矩形尺寸,然后我想在下一个矩形旁边画一个矩形。但要做到这一点,我需要以某种方式停止创建时钟或冻结它或第一个矩形将变得更大。有可能吗?

sf::RenderWindow window(sf::VideoMode(1800, 800), " window ");
sf::Clock timer;
while (window.isOpen())
{
    sf::Event event;
    while (window.pollEvent(event))
    {
        if (event.type == sf::Event::Closed)
            window.close();

    } //while
    window.clear();

    sf::RectangleShape shape(sf::Vector2f(timer.getElapsedTime().asSeconds()*50,100));


    if (timer.getElapsedTime().asSeconds() > 4)
    {
        timer.restart();

    }



    shape.setPosition(sf::Vector2f(100, 400));

    shape.setFillColor(sf::Color(150,150,150));
    window.draw(shape);
    window.display();
}

1 个答案:

答案 0 :(得分:1)

一种方法可以像这样

sf::RenderWindow window(sf::VideoMode(1800, 800), " window ");
sf::Clock timer;
sf::RectangleShape shape(sf::Vector2f(timer.getElapsedTime().asSeconds()*50,100));
while (window.isOpen())
{
    sf::Event event;
    while (window.pollEvent(event))
    {
        if (event.type == sf::Event::Closed)
            window.close();

    } //while
    window.clear();

    if (shape.getSize().x < 200)
        shape.setSize(sf::Vector2f(std::min(timer.getElapsedTime().asSeconds()*50, 200.f), 100));

    shape.setPosition(sf::Vector2f(100, 400));

    shape.setFillColor(sf::Color(150,150,150));
    window.draw(shape);
    window.display();
}