I have two problems that are loosely related and I didn't want to create two separate threads into separate sub-forums:
The first is that I have a number of buttons, when the left mouse button is clicked while the cursor is inside a button's hitbox, the scene is changed depending on which button is clicked:
for (int j = init; j < limit; j++) {
collision = buttons[j].button_collision();
if (sf::Mouse::isButtonPressed(sf::Mouse::Button::Left) == true) {
//Button logic goes here.
}
}
The problem is, when the mouse button is pressed, and the scene changes, in the fraction of a second that I'm still holding the mouse button down and the cursor happens to be on another button in the new scene, that button will be clicked and the scene will change again quickly even if that's not what I want.
So I tried changing the code to the following:
for (int j = init; j < limit; j++) {
collision = buttons[j].button_collision();
if (e.type == sf::Event::MouseButtonReleased) {
if (e.mouseButton.button == sf::Mouse::Left) {
//Button logic goes here.
}
}
}
Now, the scene only changes when the button is clicked
then released, but, for fraction of a second, if the cursor happens to be hovering on a button when the scene changes, that button will also be "clicked" even though the mouse button is released when that happens.
It's weird, the code does something completely different than the previous one, but I still end up with what is essentially the same problem.
Now that leaves with the second problem, which is a simple one:
for (int j = init; j < limit; j++) {
collision = buttons[j].button_collision();
if (collision == true) {
m_hover.play(); //'m_hover' is a blip sfx that is supposed to play when the cursor hovers on a button.
}
if (sf::Mouse::isButtonPressed(sf::Mouse::Button::Left) == true) {
//Button logic goes here.
}
}
But since this code is included in the game loop, the sound effect ends up playing every single frame.
So my two problems are essentially the same: Executing an instruction only on the first frame in which
collision=true, and not executing it again until
collision=false.
How do I register a single mouse press
(Or a click preferably if possible, meaning a press then a release)?
And how do I get a sound effect to play only the first time it's condition is met, until it returns to be false again?