Howdy! :)
I personally prefer making a button class.
Like so.
bool released;
class Button
{
Sprite sprite;
bool clickedleft=false;
bool clickedright=false;
bool active=false;
void update(RenderWindow &window)
{
if(active)
{
if(Mouse::isButtonPressed(Mouse::Left))
{
if(released)
{
clickedleft=true;
}
released=false;
}
if(Mouse::isButtonPressed(Mouse::Right))
{
if(released)
{
clickedright=true;
}
released=false;
}
window.draw(sprite);
}
}
};
int main()
{
Texture texture;
texture.loadFromFile("button.png");
Button btn[10];
btn[0].sprite.setTexture(texture);
btn[0].sprite.setPosition(300,300);
btn[0].active=true;
RenderWindow window(VideoMode(960,540),"");
while(window.isOpen())
{
Event e;
while(window.pollEvent(e))
{
if(e.type==Event::Closed)
window.close();
}
if(!Mouse::isButtonPressed(Mouse::Left)&&!Mouse::isButtonPressed(Mouse::Right))
released=true;
if(btn[0].clickedleft)
{
btn[0].active=false;
cout<<"clickedLeft";
}
window.clear();
for(int a=0;a<sizeof(btn);a++)
{
if(btn.active)
btn.update(window);
}
window.display();
}
}
This limits the checking for a click to one time and makes the button invisible when clicked.