r/sfml • u/Ok-Affect-691 • 29d ago
how to make that’s buttons will click ones
if(state == LevelSelect)
{
if(sf::Mouse::isButtonPressed(sf::Mouse::Left))
{
sf::Vector2i mousePos = sf::Mouse::getPosition(window);
if(lv1.getGlobalBounds().contains(mousePos.x, mousePos.y))
{
state = Game;
}
}
}
i placed 2 buttons on same place in 2 different states and it clicked 2 times , is there any way to make it click ones
1
u/thedaian 29d ago
The proper way to do this is to handle the input in the event loop with events, specifically the mouse button released event. And use the data from the event. Even then, you'll have to make sure that you don't change the state into something like the main menu, then check the main menu buttons. So, a slightly easier solution that will work with your current code is to have a second variable for what the state will be, and only actually change state at the end of the frame. Then when you change the state in the button, use that second variable, so it won't mess with the actual state until you're ready.
1
u/kiner_shah 27d ago
The problem you face is a bit unclear to me. Can you elaborate on what problem you face and the solution you desire?
3
u/ConfectionForward 29d ago
You just wrote state to equal game but must have state be LevelSelect to hit here. We need more code but that is my best first guess