Hi,
I have made a class Button and inside I've placed a bool Button::isClicked(). With this code it worked fine:
bool Button::isClicked()
{
sf::Event event;
sf::Vector2f mousePosition = static_cast<sf::Vector2f>(sf::Mouse::getPosition());
if(this->getGlobalBounds().contains(mousePosition)
&& sf::Mouse::isButtonPressed(sf::Mouse::Left)) //if clicked
{
return true;
}
else
{
return false;
}
}
But I couldn't place other buttons on the same place on an "other window" (I did a thing like this:)
if(status == "MENU")
{
...
window.clear();
window.draw(...);
}
if(status == "GAME")
{
...
window.clear();
window.draw(...);
}
So I tried to do something like that:
bool Button::isClicked()
{
sf::Event event;
sf::Vector2f mousePosition = static_cast<sf::Vector2f>(sf::Mouse::getPosition());
if(this->getGlobalBounds().contains(mousePosition)
&& event.type == sf::Event::MouseButtonReleased
&& event.mouseButton.button == sf::Mouse::Left) //if clicked
{
return true;
}
else
{
return false;
}
}
but it doesn't work. The program runs fine (I have no errors) but I can't press the buttons. So I tried to put that code in the main.cpp for see if it is the fault of the event that could be written with a wrong syntax:
while(window.isOpen())
{
...
sf::Vector2f mousePosition = static_cast<sf::Vector2f>(sf::Mouse::getPosition());
if(button.getGlobalBounds().contains(mousePosition)
&& event.type == sf::Event::MouseButtonReleased
&& event.mouseButton.button == sf::Mouse::Left) //if clicked
{
//Do something
}
}
Instread of:
while(window.isOpen())
{
...
if(button.isClicked()) //if clicked
{
//Do something
}
}
And that works. Any ideas?