:-( Why does my code register two mouse clicks per click? :-(

This is a discussion on :-( Why does my code register two mouse clicks per click? :-( within the Game Programming forums, part of the General Programming Boards category; Howdy ya'll, When I run the following code & click the screen any number of times I get one undefined ...

  1. #1
    Registered User JM1082's Avatar
    Join Date
    Mar 2011
    Posts
    51

    :-( Why does my code register two mouse clicks per click? :-(

    Howdy ya'll,

    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:
    /* 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
    //////////////////////////////////////////////////////////////////////////////////////////////////////////

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,333
    Does your Event.MouseButton also tell you whether it was a "press" or "release".

    If you do get down/up events for mouse clicks, you need to add this distinction to your code.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User JM1082's Avatar
    Join Date
    Mar 2011
    Posts
    51
    Thanks for your input but after checking YouTube I realized that the double clickings are solved by using a switch statement rather than an if statement.
    I'm still struggling to stop the "out of bounds" coordinates when the mouse if moved outside of the renderWindow area in case you can help with that.

  4. #4
    C++ Enthusiast M.Richard Tober's Avatar
    Join Date
    May 2011
    Location
    Georgia
    Posts
    56

    I'm plotting to steal Salem's brain...

    Quote Originally Posted by JM1082 View Post
    Thanks for your input but after checking YouTube I realized that the double clickings are solved by using a switch statement rather than an if statement.
    I'm still struggling to stop the "out of bounds" coordinates when the mouse if moved outside of the renderWindow area in case you can help with that.
    Salem's right, JM. Your loop will trigger when any event is detected - and though the check for the "Key Press & Escape Key" is correct, the second check is incomplete. You're not checking for a mouse event -- you simply check the value of the field as though a mouse event fired.

    Compare how KeyPressed, KeyReleased checks IF a key press event occurred, and is followed in short-circuit logic by a check of the event structure to determine if the key is the ESC key.

    Similarly, MouseButtonPressed, MouseButtonReleased catch mouse events - then can be followed by your check of the Event structure for the Event.MouseButton.Button field to see if it equals sf::Mouse::Left, sf::Mouse::Right, sf::Mouse::Middle, etc.

    Reference:
    SFML - Simple and Fast Multimedia Library

    Leaving before I ramble.
    Eventually, I decided that thinking was not getting me very far and it was time to try building.
    Rob Pike, "The Text Editor sam"

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,333
    I'm pretty sure you must be doing something else.

    Because just switching if to switch won't do a damn thing.
    Code:
    if ( var == const )
    
    // and
    
    switch ( var ) {
      case const:
    }
    Are functionally equivalent.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to add mouse simulation clicks
    By whatnext in forum C Programming
    Replies: 6
    Last Post: 09-30-2011, 01:18 PM
  2. mouse clicks
    By chris1985 in forum C Programming
    Replies: 1
    Last Post: 01-14-2005, 03:19 PM
  3. Key Presses and Mouse Clicks
    By Anonymous in forum Windows Programming
    Replies: 3
    Last Post: 04-22-2002, 06:40 PM
  4. Mouse Clicks
    By Unregistered in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 11-07-2001, 08:12 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21