The if(Window.IsOpened()) is just there to start the sprite moving without requiring any input.
The sprite will also move without the if statement...
Could you elaborate on what you mean by
If you have the decleration of the clock next to the time request you'll create a new clock object each iteration and the elapsedtime between creation and request will be just a few milli or micro seconds.
Next to the following explenation it also means that you should go again back to a C++ book and learn a bit more about the language...
A variable or class instance is kept alive only within a specific scope, this can either be the global scope or the local scope. A scope can be created easily with the { }-brackets and everything you declare inside or instanciate inside won't be accessable outside and will be destroyed at the end of the scope (= }).
{
int a = 0;
}
std::cout << a std::endl;
That code will give you an error because
a does not exist in the scope outside of the curlybrackets.
The same is true for a if, for loop or while loop:
while(window.isOpen())
{
sf::Clock clock;
}
Every iteration you'll end up at the closing curlybracket and the clock instance will be deleted. Thus in the next itereation a new clock object will be created and initialized with 0.
As I already said before the time distance between one and the other call is mostly just a few microseconds, thus the following code will always print out a number smaller than one second.
while(window.isOpen())
{
sf::Clock clock;
float dt = clock.getElapsedTime().asSeconds();
std::cout << dt << "s" << std::endl;
}
The solution is the declare the clock outside of the loop and reset it every itereation.
sf::Clock clock;
while(window.isOpen())
{
float dt = clock.restart().asSeconds();
std::cout << dt << "s" << std::endl;
}