Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Problems with clickable sprites  (Read 1082 times)

0 Members and 1 Guest are viewing this topic.

StrengthSauce

  • Newbie
  • *
  • Posts: 3
    • View Profile
Problems with clickable sprites
« 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.
        }

Arcade

  • Full Member
  • ***
  • Posts: 230
    • View Profile
Re: Problems with clickable sprites
« Reply #1 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