SFML community forums

Help => General => Topic started by: StrengthSauce on May 04, 2015, 11:35:13 pm

Title: Problems with clickable sprites
Post by: StrengthSauce on May 04, 2015, 11:35:13 pm
Hello, I want to make a sprite clickable, but when I use the code below, I never have to lift the mouse to navigate. Should I use sf::Event::mouseButtonReleased instead, and can you give an example?

sf::FloatRect bounds = sprite.getGlobalBounds();
        sf::Vector2f mouse = Win.mapPixelToCoords(sf::Mouse::getPosition(Win));
        if (bounds.contains(mouse) && sf::Mouse::isButtonPressed(sf::Mouse::Left))
        {
                // Stuff happens.
        }
Title: Re: Problems with clickable sprites
Post by: Arcade on May 05, 2015, 12:30:07 am
sf::Mouse::isButtonPressed() will return true every time it is called as long as the mouse is pressed. It sounds like the behavior you actually want is to be notified only one time when the mouse gets pressed. For this you should use events instead of sf::Mouse::isButtonPressed().

See the Events tutorial for more information:
http://www.sfml-dev.org/tutorials/2.2/window-events.php