Reputation: 189
I'm using SFML to write game and I got small problem with my button. Here is function I use to check if button is clicked:
bool InputManager::isSpriteClicked(sf::Sprite object, sf::Mouse::Button button, sf::RenderWindow &window) {
if (sf::Mouse::isButtonPressed(button)) {
sf::IntRect rect(object.getPosition().x, object.getPosition().y,
object.getGlobalBounds().width, object.getGlobalBounds().height);
if (rect.contains(sf::Mouse::getPosition(window))) {
return true;
}
}
return false;
}
It works almost fine, yet sometimes once I press this button, action is trigerred twice, like I double click it, even tho I didnt even release it yet. I tried to involve sf::Event::MouseButtonReleased
but It actually wasnt helping too. What I want to achieve is of course just 1 action per 1 button press/release/whatever.
Here is example of my GameLoop
if its needed
void GameState::handleUserInput() {
sf::Event evnt;
while (this->m_data->window.pollEvent(evnt)) {
if (this->m_data->input.isSpriteClicked(this->m_rollButton, sf::Mouse::Left, this->m_data->window)) {
m_gameEngine.startTurn(m_gameStatusBox);
}
}
void GameState::update(sf::Time dt) {
m_gameEngine.getActivePlayer().move(dt);
}
void GameState::draw() {
this->m_data->window.display();
}
Upvotes: 1
Views: 1538
Reputation: 1993
You're mixing two different approaches to manage user input
Input by events: used to manage those things that happen once and you want to be notified
Real time input: basically, poll repeatedly for input status (in this case, the mouse)
The general rule:
if (sf::Mouse::isButtonPressed(sf::Mouse::Left))
// WHILE the left mouse button is being pressed, do something
if (event.type == sf::Event::MouseButtonPressed)
// WHEN the left mouse button has been pressed, do something
If you want to learn more, i recommend you to read this chapter (or better, the whole book) about SFML game dev.
Upvotes: 0
Reputation:
Short answer:
if (event.type == sf::Event::MouseButtonPressed)
Long story By the way, it's second link in google....
Upvotes: 0