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

Author Topic: Trouble detecting sprite's global position  (Read 1858 times)

0 Members and 1 Guest are viewing this topic.

The Terminator

  • Full Member
  • ***
  • Posts: 224
  • Windows and Mac C++ Developer
    • View Profile
Trouble detecting sprite's global position
« on: November 05, 2012, 11:32:09 pm »
Hi there, I'm having some difficulties trying to detect if the mouse is hovered over and clicking on the sprite. I want it to change sprites once it's hovered over, then change states once it is clicked on. The only thing I'm having trouble on is the mouse. Here's my code that compiles but doesn't seem to work:

void IntroState::update() {
    sf::Event event;

        while(m_game.screen.pollEvent(event)) {
                switch(event.type) {
            case sf::Event::Closed:
                m_game.quit();
                break;

            case sf::Event::KeyPressed:
                switch (event.key.code)
                {
                    case sf::Keyboard::Escape:
                    m_game.quit();

                    break;
                }

                break;

            break;

            case sf::Event::MouseButtonPressed:
                if (s_playButton.getGlobalBounds().contains(sf::Mouse::getPosition().x, sf::Mouse::getPosition().y))
                {
                    m_game.screen.draw(s_playButtonActive); //replace current button with a highlighted one since it is being hovered over

                    if (sf::Mouse::isButtonPressed(sf::Mouse::Left)) // if the playbutton is being hovered over AND the left mouse button is being clicked
                    {
                        //do stuff
                    }
                }

                break;
                }
        }
}

I appreciate any help. Thanks!
Current Projects:
Technoport

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Trouble detecting sprite's global position
« Reply #1 on: November 05, 2012, 11:36:02 pm »
1. get position is not relative to a window, get position(window ) is relative to window you pass to it
2. you must also convert these to sf vector 2f or they may be wrong if you changed views
Back to C++ gamedev with SFML in May 2023

The Terminator

  • Full Member
  • ***
  • Posts: 224
  • Windows and Mac C++ Developer
    • View Profile
Re: Trouble detecting sprite's global position
« Reply #2 on: November 05, 2012, 11:54:51 pm »
Ok, now changing the states work. I still can't get changing the sprites to work though.
Current Projects:
Technoport

The Terminator

  • Full Member
  • ***
  • Posts: 224
  • Windows and Mac C++ Developer
    • View Profile
Re: Trouble detecting sprite's global position
« Reply #3 on: November 06, 2012, 12:01:36 am »
Nevermind! Got it to work using sf::Event::MouseMoved. Thanks for the help though FRex!
Current Projects:
Technoport