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

Author Topic: Why does my code register two mouse clicks per click?  (Read 2396 times)

0 Members and 1 Guest are viewing this topic.

murkinmouse

  • Newbie
  • *
  • Posts: 2
    • View Profile
Why does my code register two mouse clicks per click?
« on: March 21, 2012, 12:37:13 pm »
Hi,
When I run the following code & click the screen any number of times I get one undefined mouse click to start with then for each click I get a duplicate one.
 
Why is this?


 
Code: [Select]

/* main.cpp */
#include <SFML/System.hpp>
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <iostream>
#include <string>
using namespace std;

int main( void )
{
     // set gameWindow parameters
    sf::RenderWindow gameWindow( sf::VideoMode( 1024, 600, 32 ), "Adam's Fantasy", sf::Style::Fullscreen );

     // fill gameWindow with black background
    gameWindow.Clear();

     // include buttonOne.jpg
    sf::Image Image;
    Image.LoadFromFile( "buttonOne.jpg" );
    sf::Sprite buttonOne;
    buttonOne.SetImage( Image );
    gameWindow.Draw( buttonOne );

     // include buttonTwo.jpg
    Image.LoadFromFile( "buttonTwo.jpg" );
    sf::Sprite buttonTwo;
    buttonTwo.SetImage( Image );
    buttonTwo.SetPosition( 512.f, 300.f );
    gameWindow.Draw( buttonTwo );

     // display the game window
    gameWindow.Display();

     // begin interaction loop
    while ( gameWindow.IsOpened() )
    {
        sf::Event Event;
        /* while ( gameWindow.GetEvent( Event ) )
        { */
             // Press escape key to close the game
            if ( ( Event.Type == sf::Event::KeyPressed ) && ( Event.Key.Code == sf::Key::Escape ) )
                gameWindow.Close();

             // Left click
            if ( Event.MouseButton.Button == sf::Mouse::Left )
            {
                cout << "The left mouse button was clicked at " << Event.MouseButton.X << ", " << Event.MouseButton.Y << endl;
                    if ( Event.MouseButton.X >= 512 && Event.MouseButton.X <= 822 )
                    {
                        if ( Event.MouseButton.Y >= 300 && Event.MouseButton.Y <= 460 )
                            cout << "You clicked the picture" << endl;
                    } // end if
            } // end if
        /* } // while events are recieved */
    } // while gameWindow.IsOpened
} // end main
//////////////////////////////////////////////////////////////////////////////////////////////////////////


Thanks

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Why does my code register two mouse clicks per click?
« Reply #1 on: March 21, 2012, 01:00:15 pm »
It's because you handle events the wrong way.  :wink:
There are two mouse events:
- sf::Event::MouseButtonPressed
- sf::Event::MouseButtonReleased

If you only check for Even.MouseButton.Button you only check for you don't specify for which Event you want to check and thus you get two mouse clicks fired.

You'd have to do it like you did with the keyboard event.
Code: [Select]
if(Event.Type == sf::Event::MouseButtonReleased && Event.MouseButton.Button == sf::Mouse::Left)
{
 // Do what you want
}
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Why does my code register two mouse clicks per click?
« Reply #2 on: March 21, 2012, 01:47:28 pm »
Yeah, basically, this:
Code: [Select]
if ( Event.MouseButton.Button == sf::Mouse::Left )
... means "if the button of the mouse-button event is the left one", but you don't even know if a mouse-button event was triggered!

The first thing to do, always, is to check what kind of event was triggered. Then, according to the type, you can use the corresponding event members to know more about it.
Laurent Gomila - SFML developer

murkinmouse

  • Newbie
  • *
  • Posts: 2
    • View Profile
Why does my code register two mouse clicks per click?
« Reply #3 on: March 21, 2012, 01:58:59 pm »
thank you! :)

 

anything