Is the sleep there to ensure that the button is held rather than just clicked?
Yes that is exactly why the sleep is there. Basically if the left pressed event is triggered then I want to know if the user is still holding the left button. If I don't have that sleep in there then it takes left clicks as dragging attempts and moves the sprite when I just trying to left-click on the sprite.
I don't think I fully understand the timing method that you are describing. Are you able to clarify how the timer would work?
I did make an attempt as below, but it always seemed to be saying the user was dragging, even during a left-click.
So in my class I defined a gameClock
sf::Clock gameClock;
Then at the beginning of the event loop, I have this to check if the left button is pressed and if the gameclock elapsed time is > 100 milliseconds. If not then restart the game clock
if (sf::Mouse::isButtonPressed(sf::Mouse::Left) && gameClock.getElapsedTime().asMilliseconds() >= 100)
{
cout << "gameClock.getElapsedTime().asMilliseconds() = " << gameClock.getElapsedTime().asMilliseconds() << "\n";
cout << "setting isdragging to true\n";
isDragging = true;
}
else
{
gameClock.restart();
}
Now I am not entirely sure if this what you meant as to having a timer?
Then in my events I have more or less the same thing, except no sleep or isButton pressed check anymore.
if (event.type == sf::Event::MouseButtonPressed)
{
//if left button pressed
if (event.mouseButton.button == sf::Mouse::Left)
{
cout << "left pressed \n";
if (isDragging)
{
//Here I check if mouse was within the bounds of the sprite and if so then do the dragging updates.
}
else if (!isDragging)
{
//Do the stuff I want to do when user is not trying to do a drag and drop
}
}
}