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

Author Topic: Why does 'mouseButton.button' returns mouse's x position?  (Read 1841 times)

0 Members and 1 Guest are viewing this topic.

yj1214

  • Newbie
  • *
  • Posts: 14
    • View Profile
Why does 'mouseButton.button' returns mouse's x position?
« on: July 30, 2015, 09:26:45 am »
What's the difference between using 'sf::Mouse::isButtonPresessed' and 'sf::Event::mouseButton.button'?

mouseButton function somehow returns the value of the x coordinate of the mouse. Of course it also returns weather I clicked my mouse or not. But why does it return the x coordinate of my mouse?

Finally, which of the above function is recommended?
« Last Edit: July 30, 2015, 10:18:49 pm by yj1214 »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Why does 'mouseButton' returns mouse's x position?
« Reply #1 on: July 30, 2015, 09:31:52 am »
Quote
What's the difference between using 'sf::Mouse::isButtonPresessed' and 'sf::Event::mouseButton'?
sf::Mouse gives you the current state of the mouse at any time.
sf::Event::MouseButtonPressed notifies you when a button is clicked.
The first is a state, the second is an event. If you don't understand the difference, you can read the corresponding doc and tutorials, this is explained with more details.

Quote
But why does it return the x coordinate of my mouse?
Because when you click, you're usually interested to know where you clicked.
Laurent Gomila - SFML developer

Hapax

  • Hero Member
  • *****
  • Posts: 3364
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Why does 'mouseButton' returns mouse's x position?
« Reply #2 on: July 30, 2015, 01:15:05 pm »
SFML has its own tutorial on these:

Finally, which of the above function is recommend?
Both have their advantages and disadvantages that depend on what you actually want to use them for.
Events are most useful for single instances of controls e.g. mouse clicks, mousewheel scrolled etc. whereas real-time control states are more useful for game controls, for example.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

yj1214

  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: Why does 'mouseButton' returns mouse's x position?
« Reply #3 on: July 30, 2015, 10:18:07 pm »
Thanks for the answers...but I still don't get this one thing.

mouseButton.button function constantly returns the value of my mouse's x coordinate whenever I move my mouse. But why only X coordinate? why not return Y coordinate as well?


Also, how does mouseButton.x and mouseButton.y work?

Code: [Select]
std::cout << mouseButton.x << std::endl;

This prints out Y coordinate of my mouse constantly...when I click my mouse, then it returns my X coordinate...if I need to check where I clicked my mouse, shouldn't the Y coordinate disturbs my X coordinate? how do you distinguish which is my X coordinate?
« Last Edit: July 30, 2015, 10:35:57 pm by yj1214 »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Why does 'mouseButton.button' returns mouse's x position?
« Reply #4 on: July 30, 2015, 10:49:48 pm »
You obviously uses events incorrectly. Have you read the documentation first?
Laurent Gomila - SFML developer

Arcade

  • Full Member
  • ***
  • Posts: 230
    • View Profile
Re: Why does 'mouseButton.button' returns mouse's x position?
« Reply #5 on: July 30, 2015, 10:57:44 pm »
Make sure you understand this paragraph of the tutorial. You may also need to look into the concept of unions if you are unfamiliar with them.

Quote
Before dealing with events, it is important to understand what the sf::Event type is, and how to correctly use it. sf::Event is a union, which means that only one of its members is valid at a time (remember your C++ lesson: all the members of a union share the same memory space). The valid member is the one that matches the event type, for example event.key for a KeyPressed event. Trying to read any other member will result in an undefined behavior (most likely: random or invalid values). It it important to never try to use an event member that doesn't match its type.