SFML community forums
Help => General => Topic started by: dove96 on March 28, 2016, 08:10:27 am
-
Hello everyone, i am trying to display a window for 3 seconds,after that it closes. yet why does the window displays on random amount of time. it does not display the way i wanted to. Can anyone please enlighten me of my rather stup!D error. it does display the window but not exactly 3 seconds. sometimes it jumps to 3.xx to greater than 3 seconds. can somebody enlighten me with this please?
sf::Clock clock;
sf::Time limit = sf::milliseconds(3000);
sf:: Time elapsed;
...loop...
elapsed=clock.getElapsedTime();
while(window5.pollEvent(event))
{
//std::cout<<elapsed.asMilliseconds()<<std::endl;
if(elapsed.asMilliseconds()>limit.asMilliseconds())
{
window5.close();
}
}
window5.display();
...end loop...
-
You get the elapsed time once, so when the loop starts both the limit and elapsed time have been set and all you do is compare two fixed values. On top of that you call getElapsedTime before the loop, so whatever time has past since the creation of the clock object and that call, will be returned.
What you want to do is call restart() before the loop and then simply check against getElapsedTime() during the iteration, the elapsed sf::Time object is not necessarily needed.
Also please make use of the [code=cpp][/code] tags.
-
hi exploiter, i get a little bit confused when you say:
"you get the elapsed time once","all you do is compare two fixed values" and " you call getElapsedTime before the loop"
are you telling me what i should do, or what my mistake are??
-
Don't put your code inside the event loop if it don't want it to be triggered only when there are events.
-
oww. there. got it. thanks both.