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

Author Topic: Mouse event problem....  (Read 4650 times)

0 Members and 1 Guest are viewing this topic.

Dennis

  • Newbie
  • *
  • Posts: 3
    • View Profile
Mouse event problem....
« on: February 19, 2017, 11:22:29 am »
I have the event loop inside the main.cpp and i have 2 classes  (a game class and a player class) i want the player to shoot if the user presses the mouse. but the only thing i could use was the sf::Mouse::isButtonPressed(sf::Mouse::Left) it works but if you press it just keeps repeating. so i was thinking about using events. but then i need to send it via main.cpp to game.cpp to the player.cpp. is there another way of doing this?

EDIT:
does someone know a good way of sending event data. because now i have a fucntion in the game.cpp thats named SetMouseState(bool state) that sets a value in the game.h called mouseState and i have the same function in the player.cpp that does the same thing. this is really annoying to do it for every single button.
« Last Edit: February 19, 2017, 06:45:01 pm by Dennis »

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Mouse event problem....
« Reply #1 on: February 19, 2017, 12:00:21 pm »
Sending something elsewhere if that other place needs it seems like a pretty good way to do things. ;)

You can send the event and let other places use it or you could create a (simple or complex) message system that does it instead.

Using isButtonPressed will return true as long as it is pressed. If you wish to use this to track only the first press, you can store a boolean that stores its previous state (whether it was pressed or not in the previous cycle). Then if they are identical, the key hasn't changed (mostly).
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

JayhawkZombie

  • Jr. Member
  • **
  • Posts: 76
    • View Profile
Re: Mouse event problem....
« Reply #2 on: February 20, 2017, 02:09:24 am »
The way we do it is that events are "consumed" by objects when they use them.  If it is consumed, it isn't sent any further down the line.

Each object has an EventHandler (just a small class), and if an event makes it all the way down to that object, its handler get notified.
Each SFML event has a corresponding function in that EventHandler class. ie:
bool OnMousePressed(sf::Mouse::Button which);
bool OnKeyPressed(sf::Keyboard::Key which);
bool OnKeyReleased(sf::Keyboard::Key which);
...//and so on
They return false if they didn't consume the event, and true if they did.

That might be a little complex for your needs.  But maybe the idea will help?