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

Author Topic: Mouse Button Event not triggering  (Read 3006 times)

0 Members and 1 Guest are viewing this topic.

Adriankusiak

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Mouse Button Event not triggering
« on: January 04, 2013, 04:27:48 pm »
Hello, I'm writing a small 2D mini-game in which I'm developing a menu. I wrote events to be checked when the mouse is over a button so that specific actions can be triggered but for some reason the mouse event doesn't seem to get triggered at all. When I click over a button nothing happens and I've been checking my code and referencing Google and different tutorials as well as these forums for hours but I can't seem to figure out the problem. I'm using SFML 2.0 and visual studio 11. Any help is much appreciated, thanks.

Here is my code:
        int nCurrentScene=1;
    while (window.isOpen())
    {
                sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
        }

                //Buttons

                sf::Vector2i localPosition = sf::Mouse::getPosition(window);
                sf::FloatRect sStartBnRec = sStartBn.getGlobalBounds();
                sf::FloatRect sExitBnRec = sExitBn.getGlobalBounds();
                sf::Event eMouseClk;
                if(sStartBnRec.contains(localPosition.x,localPosition.y))
                {
                        sStartBn.setTexture(tStartBn_hl);
                        while(window.pollEvent(eMouseClk))
                        {
                                if (eMouseClk.type == sf::Event::MouseButtonPressed)
                                {
                       
                                        if(eMouseClk.mouseButton.button == sf::Mouse::Left)
                                        {
                                                nCurrentScene=2;
                                        }
                                }
                        }
                }
                                else
                {
                        sStartBn.setTexture(tStartBn);
                }

                if(sExitBnRec.contains(localPosition.x,localPosition.y))
                {
                        sExitBn.setTexture(tExitBn_hl);
                        while(window.pollEvent(eMouseClk))
                        {
                                if (eMouseClk.type == sf::Event::MouseButtonPressed)
                                {
                                        if(eMouseClk.mouseButton.button == sf::Mouse::Left)
                                        {
                                                EXIT_SUCCESS;
                                        }
                                }
                        }
                }
                else
                {
                        sExitBn.setTexture(tExitBn);
                }

        window.clear();
                switch (nCurrentScene)
                {
                case 1:
                        window.draw(sMenuBg);
                        window.draw(sExitBn);
                        window.draw(sStartBn);
                        break;
                case 2:
                        window.draw(sLvloneBg);
               
                        break;
                }
       
        window.display();
    }

    return 0;
}
« Last Edit: January 04, 2013, 04:34:33 pm by Adriankusiak »

iride

  • Jr. Member
  • **
  • Posts: 88
    • View Profile
    • Email
Re: Mouse Button Event not triggering
« Reply #1 on: January 04, 2013, 04:30:55 pm »
Poll the event only once, not every time you need to. So only one sf::Event object is needed.

You can change this part :
if(sStartBnRec.contains(localPosition.x,localPosition.y))
        {
            sStartBn.setTexture(tStartBn_hl);
            while(window.pollEvent(eMouseClk))
            {
                if (eMouseClk.type == sf::Event::MouseButtonPressed)
                {
           
                    if(eMouseClk.mouseButton.button == sf::Mouse::Left)
                    {
                        nCurrentScene=2;
                    }
                }
            }
        }
 

to
if(sStartBnRec.contains(localPosition.x,localPosition.y))
{
   sStartBn.setTexture(tStartBn_hl);
   if (event.type == sf::Event::MouseButtonPressed && event.mouseButton.button == sf::Mouse::Right)
          nCurrentScene=2;
}
« Last Edit: January 04, 2013, 04:40:37 pm by iride »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Mouse Button Event not triggering
« Reply #2 on: January 04, 2013, 04:40:18 pm »
Your logic is flawed.

If you have multiple event loops, each loop will miss events that are processed by the others.

What you must do is:
while (poll event)
{
    if (event is mouse button pressed and button is left)
    {
        if (mouse is in start button)
            -> start button clicked
        else if (mouse is in exit button)
            -> exit button clicked
    }
}
Laurent Gomila - SFML developer

Adriankusiak

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Re: Mouse Button Event not triggering
« Reply #3 on: January 04, 2013, 05:00:09 pm »
Thanks everyone both your answers worked, I thought it would be more efficient to implement Laurent's answer to keep it structured and it works great so thanks very much.