SFML community forums

Help => Window => Topic started by: faizaanceg on May 16, 2014, 01:57:09 pm

Title: SFML Ping Pong
Post by: faizaanceg on May 16, 2014, 01:57:09 pm
Hey everyone,

So me and my friend are learning SFML (it's fun :p). We have just started with the basics and so we decided to make a simple Ping Pong game to test our skills. We have effectively used most simple features and now we decided to use the sf::Mouse events. Thats where the problem starts.

if(Mouse::isButtonPressed(Mouse::Left))
        {
                Vector2i  position =sf::Mouse::getPosition(*window);
               
                if(Up.contains(position))
                        playerYvel = -(SLIDER_VEL);                    
                else if(Down.contains(position))
                        playerYvel = (SLIDER_VEL);
                else
                        playerYvel=0;  
               
                 
        }              
        else
                playerYvel = 0;

The commented region is where the problem is, the Down button doesn't work at all. It doesn't get the input at all. Is it because of nested if ?
Title: Re: SFML Ping Pong
Post by: Geheim on May 16, 2014, 02:18:58 pm
Welcome to the forums!

If your first button contains the position, then your "else if" has no effect.
However we can't help any further, because we don't know how you implemented these buttons.
You could either give us some more information, or give a complete and minimal example. (http://en.sfml-dev.org/forums/index.php?topic=5559.msg36368#msg36368)

The commented region is where the problem is, [...]
There is no commented region btw.

Edit: This should also not be under "SFML Projects", better under "General" or the corresponding sfml-module :)
Title: Re: SFML Ping Pong
Post by: faizaanceg on May 16, 2014, 03:23:14 pm
Hmm, sorry for the incomplete details.

Here is the GitHub link : https://github.com/faizaanceg/Ping-pong

In the handle_input function,(ping.h) is the part I posted before.

Title: Re: SFML Ping Pong
Post by: Geheim on May 16, 2014, 03:46:19 pm
If you are using events, you have to get them first. You can read a detailed information about how to use events in the tutorial (http://www.sfml-dev.org/tutorials/2.1/window-events.php).

What you use in your code is "real-time input", but as far as I can see, the buttons do not overlap, so it is not a problem with the if-statements.

Your error is a pretty annoying one to find: (ping.h, lines 74-77)
Down.left = 730;
Down.top = 190;
Down.width = 20;
Down.width = 20;
What height does this button have? ;)

Besides, header files should only contain declarations!
Title: Re: SFML Ping Pong
Post by: faizaanceg on May 16, 2014, 04:10:27 pm
WOW.Oh My God ! I wanna hang myself -_- That was the error. (Man...) Thanks very much for that and really sorry for such a stupid mistake :/
Title: Re: SFML Ping Pong
Post by: jannugimes on May 16, 2014, 06:15:50 pm
No [problem man
WOW.Oh My God ! I wanna hang myself -_- That was the error. (Man...) Thanks very much for that and really sorry for such a stupid mistake :/
No problem man.