每隔几秒钟移动一次对象

时间:2019-01-09 14:48:48

标签: c++ sfml

我的问题是,如何每隔几秒钟移动一个对象,而当我按下键盘时又可以独立移动另一个对象呢。

例如:

while (window.isOpen())
{
sf::Event event;
    while (window.pollEvent(event))
    {
        switch (event.type)
        {
        case  sf::Event::Closed: 
            window.close(); 
            break;

        case sf::Event::KeyPressed: 
            m_mo[0]->move(0.f, 50.f); //move when keyboard is pressed
            break;
        }
    }
    m_mo[1]->move(0.f, 50.f); //move every 10 seconds

    window.clear();
    window.draw(m_mo[0]);
    window.draw(m-mo[1]);
    window.display();
    }

任何有时间给我举个例子的人!

1 个答案:

答案 0 :(得分:0)

SFML 为此提供了两个类,TimeClock。这些可以像以下一样使用。

sf::Clock clock;
sf::Time time;

time = clock.restart();
// wait some time
sf::Time ellapsed = clock.restart() - time;
sf::Int32 ellapsedMiliseconds = ellapsed.asMiliseconds();

时间here的更多信息

相关问题