2016-06-16 21 views
1

我正在用C++編寫一個小型的SFML遊戲。我想要做的就是用閃亮兩種不同顏色的介紹性文字開始遊戲,比如「Arturo Girona Presents」。我通過使用在while(window.isOpen())循環內部增加的計數器來實現它的工作,但後來我嘗試使用sf :: Clock來查看它是否使閃爍的外觀不那麼隨意。但是,由於某些原因,當我沒有明確地告訴它時,時鐘在每個循環後都保持重置,所以它不會從文本屏幕轉換。時鐘如何自行復位以及如何修復? 這裏是我的while循環:爲什麼我的sf :: SFML中的時鐘自己重置?

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; 
} 
+0

'cout << timer << endl;'是否顯示相同的數字?你似乎沒有改變這個循環中的任何地方 – doctorlove

回答

3

時鐘不重,你根本沒讀過的新值。 ;-)

在主循環內移動int timer = clock.getElapsedTime().asMilliseconds();

此外,您不希望while (timer < 5000)循環,尤其是break作爲最後一條語句。

+0

謝謝!移動這條線解決了我的問題,但我必須保持休息;聲明,否則程序將停止響應,當我運行它。 –

+1

@ArturoGirona:隨着那裏的休息,你的'while'實際上就是'if'。它根本不循環。這是你的意圖嗎? –

相關問題