Why do my button class items share the same lambda functions? Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) The Ask Question Wizard is Live! Data science time! April 2019 and salary with experience Should we burninate the [wrap] tag?How to call a parent class function from derived class function?Meaning of 'const' last in a function declaration of a class?Why do we need virtual functions in C++?Can lambda functions be templated?Storing values in buffer, within class function methodIdentify objects in boost::shared_ptr<boost::thread>How do I use a template type of an outer class as a field in an inner class in C++?Why is enum class preferred over plain enum?Why Choose Struct Over Class?Static vs class functions/variables in Swift classes?
Should I call the interviewer directly, if HR aren't responding?
How do I determine if the rules for a long jump or high jump are applicable for Monks?
How do I keep my slimes from escaping their pens?
Letter Boxed validator
What would be the ideal power source for a cybernetic eye?
When is phishing education going too far?
Antler Helmet: Can it work?
Models of set theory where not every set can be linearly ordered
Storing hydrofluoric acid before the invention of plastics
Single word antonym of "flightless"
What are the pros and cons of Aerospike nosecones?
If Jon Snow became King of the Seven Kingdoms what would his regnal number be?
I am not a queen, who am I?
Can Pao de Queijo, and similar foods, be kosher for Passover?
What causes the vertical darker bands in my photo?
If a contract sometimes uses the wrong name, is it still valid?
ListPlot join points by nearest neighbor rather than order
What are the motives behind Cersei's orders given to Bronn?
Why is black pepper both grey and black?
How to recreate this effect in Photoshop?
How does cp -a work
List *all* the tuples!
How to bypass password on Windows XP account?
How to deal with a team lead who never gives me credit?
Why do my button class items share the same lambda functions?
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)
The Ask Question Wizard is Live!
Data science time! April 2019 and salary with experience
Should we burninate the [wrap] tag?How to call a parent class function from derived class function?Meaning of 'const' last in a function declaration of a class?Why do we need virtual functions in C++?Can lambda functions be templated?Storing values in buffer, within class function methodIdentify objects in boost::shared_ptr<boost::thread>How do I use a template type of an outer class as a field in an inner class in C++?Why is enum class preferred over plain enum?Why Choose Struct Over Class?Static vs class functions/variables in Swift classes?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I am trying to make my own reusable button class in SFML. This would allow me to create a button and add a callback function to it in order to make the creation of buttons much easier.
Here is my hpp file:
#ifndef Button_hpp
#define Button_hpp
#include <stdio.h>
#include <SFML/Graphics.hpp>
#include "View.hpp"
#include "State.hpp"
#include "Window.hpp"
namespace kge
class Button: public View
private:
sf::RectangleShape* _buttonOutline;
sf::RenderWindow* _window;
sf::Clock _clock;
std::string _textString;
sf::Text* _text;
public:
Button(Window*, std::string);
~Button();
virtual void update(float td);
std::function<void(void)> callback;
void setPosition(float x, float y);
;
#endif /* Button_hpp */
And here is where I generate the buttons:
_restartButton = new kge::Button(_window, "Restart");
_restartButton->setPosition(getCenterOfScreen().x-((11*fontSize)/2), 300);
_restartButton->callback = ([this]()
State::instance().currentView = new GameView(this->_window);
this->_window->setView(State::instance().currentView);
);
_exitButton = new kge::Button(_window, "Quit");
_exitButton->setPosition(getCenterOfScreen().x-((11*fontSize)/2), 500);
_exitButton->callback = ([this]()
this->_window->close();
);
Finally, I tell the button to update and do it's checks in my window update, button->update(td)
All my buttons seem to all do the action of the last set callback. In this case, my restart button executes my exit code.
Why is this happening and how would I fix it?
Edit
Here is my generation code:
#ifndef GameOver_hpp
#define GameOver_hpp
#include <stdio.h>
#include "View.hpp"
#include "Button.hpp"
#include "GameView.hpp"
#include "State.hpp"
namespace kge
class View;
;
class GameOver: public kge::View
private:
sf::Text* _text;
kge::Button* _restartButton;
kge::Button* _exitButton;
void restartFunction(void)
public:
GameOver(kge::Window* screen) : View(screen)
int fontSize = 50;
_text = new sf::Text();
_text->setFont(kge::AssetManager::mainBundle().getFontNamed("mainfont"));
_text->setString("Game Over");
_text->setCharacterSize(fontSize);
_text->setPosition(getCenterOfScreen().x-((9*fontSize)/2), 100);
_text->setFillColor(sf::Color(255,0,0));
_restartButton = new kge::Button(_window, "Restart");
_restartButton->setPosition(getCenterOfScreen().x-((11*fontSize)/2), 300);
_exitButton = new kge::Button(_window, "Quit");
_exitButton->setPosition(getCenterOfScreen().x-((11*fontSize)/2), 500);
_restartButton->callback = ([this]()
// State::instance().currentView = new GameView(this->_window);
// this->_window->setView(State::instance().currentView);
puts("Restart");
);
_exitButton->callback = ([this]()
// this->_window->close();
puts("Restart");
);
this->addItemToView(_text);
this->addItemToView(_restartButton);
this->addItemToView(_exitButton);
void update(float td)
_restartButton->update(td);
_exitButton->update(td);
~GameOver()
delete _text;
delete _restartButton;
;
#endif /* GameOver_hpp */
Note, kge::View is just a custom sf::Drawable class (how I create my own "views")
Edit 2
Button update function:
void Button::update(float td)
if(_clock.getElapsedTime().asMilliseconds() < 400) return;
if(!sf::Mouse::isButtonPressed(sf::Mouse::Left)) return;
if(mouseIsIn(*_buttonOutline, _window))
callback();
_clock.restart();
Please note: _clock is an sf::Clock that is stored privately in the button class.
c++ class sfml
|
show 8 more comments
I am trying to make my own reusable button class in SFML. This would allow me to create a button and add a callback function to it in order to make the creation of buttons much easier.
Here is my hpp file:
#ifndef Button_hpp
#define Button_hpp
#include <stdio.h>
#include <SFML/Graphics.hpp>
#include "View.hpp"
#include "State.hpp"
#include "Window.hpp"
namespace kge
class Button: public View
private:
sf::RectangleShape* _buttonOutline;
sf::RenderWindow* _window;
sf::Clock _clock;
std::string _textString;
sf::Text* _text;
public:
Button(Window*, std::string);
~Button();
virtual void update(float td);
std::function<void(void)> callback;
void setPosition(float x, float y);
;
#endif /* Button_hpp */
And here is where I generate the buttons:
_restartButton = new kge::Button(_window, "Restart");
_restartButton->setPosition(getCenterOfScreen().x-((11*fontSize)/2), 300);
_restartButton->callback = ([this]()
State::instance().currentView = new GameView(this->_window);
this->_window->setView(State::instance().currentView);
);
_exitButton = new kge::Button(_window, "Quit");
_exitButton->setPosition(getCenterOfScreen().x-((11*fontSize)/2), 500);
_exitButton->callback = ([this]()
this->_window->close();
);
Finally, I tell the button to update and do it's checks in my window update, button->update(td)
All my buttons seem to all do the action of the last set callback. In this case, my restart button executes my exit code.
Why is this happening and how would I fix it?
Edit
Here is my generation code:
#ifndef GameOver_hpp
#define GameOver_hpp
#include <stdio.h>
#include "View.hpp"
#include "Button.hpp"
#include "GameView.hpp"
#include "State.hpp"
namespace kge
class View;
;
class GameOver: public kge::View
private:
sf::Text* _text;
kge::Button* _restartButton;
kge::Button* _exitButton;
void restartFunction(void)
public:
GameOver(kge::Window* screen) : View(screen)
int fontSize = 50;
_text = new sf::Text();
_text->setFont(kge::AssetManager::mainBundle().getFontNamed("mainfont"));
_text->setString("Game Over");
_text->setCharacterSize(fontSize);
_text->setPosition(getCenterOfScreen().x-((9*fontSize)/2), 100);
_text->setFillColor(sf::Color(255,0,0));
_restartButton = new kge::Button(_window, "Restart");
_restartButton->setPosition(getCenterOfScreen().x-((11*fontSize)/2), 300);
_exitButton = new kge::Button(_window, "Quit");
_exitButton->setPosition(getCenterOfScreen().x-((11*fontSize)/2), 500);
_restartButton->callback = ([this]()
// State::instance().currentView = new GameView(this->_window);
// this->_window->setView(State::instance().currentView);
puts("Restart");
);
_exitButton->callback = ([this]()
// this->_window->close();
puts("Restart");
);
this->addItemToView(_text);
this->addItemToView(_restartButton);
this->addItemToView(_exitButton);
void update(float td)
_restartButton->update(td);
_exitButton->update(td);
~GameOver()
delete _text;
delete _restartButton;
;
#endif /* GameOver_hpp */
Note, kge::View is just a custom sf::Drawable class (how I create my own "views")
Edit 2
Button update function:
void Button::update(float td)
if(_clock.getElapsedTime().asMilliseconds() < 400) return;
if(!sf::Mouse::isButtonPressed(sf::Mouse::Left)) return;
if(mouseIsIn(*_buttonOutline, _window))
callback();
_clock.restart();
Please note: _clock is an sf::Clock that is stored privately in the button class.
c++ class sfml
Can you confirm this is happening by attaching a debugger and putting a breakpoint in both lamdbas? That may tell you why this would happen.
– Botje
Mar 22 at 8:28
@Botje I can confirm that by using break points, the IDE is showing it's on the correct callback, however it is always doing the exit event. If I remove my exit button, my restart button works just fine.
– iProgram
Mar 22 at 8:36
@Botje What is interesting is when I puts a message, that message shows correctly and if I remove the this->_window->close(); line, it works.
– iProgram
Mar 22 at 8:37
1
Could it be that all callbacks are executing? Show the code that should trigger the callbacks.
– Botje
Mar 22 at 8:40
1
It is not MCVE. How are you storing buttons? How are you iterating over them and call callbacks? Where is update definition? Where are buttons generated, you are capturingthis
so it happens from some member functions.. and so on.
– rafix07
Mar 22 at 8:40
|
show 8 more comments
I am trying to make my own reusable button class in SFML. This would allow me to create a button and add a callback function to it in order to make the creation of buttons much easier.
Here is my hpp file:
#ifndef Button_hpp
#define Button_hpp
#include <stdio.h>
#include <SFML/Graphics.hpp>
#include "View.hpp"
#include "State.hpp"
#include "Window.hpp"
namespace kge
class Button: public View
private:
sf::RectangleShape* _buttonOutline;
sf::RenderWindow* _window;
sf::Clock _clock;
std::string _textString;
sf::Text* _text;
public:
Button(Window*, std::string);
~Button();
virtual void update(float td);
std::function<void(void)> callback;
void setPosition(float x, float y);
;
#endif /* Button_hpp */
And here is where I generate the buttons:
_restartButton = new kge::Button(_window, "Restart");
_restartButton->setPosition(getCenterOfScreen().x-((11*fontSize)/2), 300);
_restartButton->callback = ([this]()
State::instance().currentView = new GameView(this->_window);
this->_window->setView(State::instance().currentView);
);
_exitButton = new kge::Button(_window, "Quit");
_exitButton->setPosition(getCenterOfScreen().x-((11*fontSize)/2), 500);
_exitButton->callback = ([this]()
this->_window->close();
);
Finally, I tell the button to update and do it's checks in my window update, button->update(td)
All my buttons seem to all do the action of the last set callback. In this case, my restart button executes my exit code.
Why is this happening and how would I fix it?
Edit
Here is my generation code:
#ifndef GameOver_hpp
#define GameOver_hpp
#include <stdio.h>
#include "View.hpp"
#include "Button.hpp"
#include "GameView.hpp"
#include "State.hpp"
namespace kge
class View;
;
class GameOver: public kge::View
private:
sf::Text* _text;
kge::Button* _restartButton;
kge::Button* _exitButton;
void restartFunction(void)
public:
GameOver(kge::Window* screen) : View(screen)
int fontSize = 50;
_text = new sf::Text();
_text->setFont(kge::AssetManager::mainBundle().getFontNamed("mainfont"));
_text->setString("Game Over");
_text->setCharacterSize(fontSize);
_text->setPosition(getCenterOfScreen().x-((9*fontSize)/2), 100);
_text->setFillColor(sf::Color(255,0,0));
_restartButton = new kge::Button(_window, "Restart");
_restartButton->setPosition(getCenterOfScreen().x-((11*fontSize)/2), 300);
_exitButton = new kge::Button(_window, "Quit");
_exitButton->setPosition(getCenterOfScreen().x-((11*fontSize)/2), 500);
_restartButton->callback = ([this]()
// State::instance().currentView = new GameView(this->_window);
// this->_window->setView(State::instance().currentView);
puts("Restart");
);
_exitButton->callback = ([this]()
// this->_window->close();
puts("Restart");
);
this->addItemToView(_text);
this->addItemToView(_restartButton);
this->addItemToView(_exitButton);
void update(float td)
_restartButton->update(td);
_exitButton->update(td);
~GameOver()
delete _text;
delete _restartButton;
;
#endif /* GameOver_hpp */
Note, kge::View is just a custom sf::Drawable class (how I create my own "views")
Edit 2
Button update function:
void Button::update(float td)
if(_clock.getElapsedTime().asMilliseconds() < 400) return;
if(!sf::Mouse::isButtonPressed(sf::Mouse::Left)) return;
if(mouseIsIn(*_buttonOutline, _window))
callback();
_clock.restart();
Please note: _clock is an sf::Clock that is stored privately in the button class.
c++ class sfml
I am trying to make my own reusable button class in SFML. This would allow me to create a button and add a callback function to it in order to make the creation of buttons much easier.
Here is my hpp file:
#ifndef Button_hpp
#define Button_hpp
#include <stdio.h>
#include <SFML/Graphics.hpp>
#include "View.hpp"
#include "State.hpp"
#include "Window.hpp"
namespace kge
class Button: public View
private:
sf::RectangleShape* _buttonOutline;
sf::RenderWindow* _window;
sf::Clock _clock;
std::string _textString;
sf::Text* _text;
public:
Button(Window*, std::string);
~Button();
virtual void update(float td);
std::function<void(void)> callback;
void setPosition(float x, float y);
;
#endif /* Button_hpp */
And here is where I generate the buttons:
_restartButton = new kge::Button(_window, "Restart");
_restartButton->setPosition(getCenterOfScreen().x-((11*fontSize)/2), 300);
_restartButton->callback = ([this]()
State::instance().currentView = new GameView(this->_window);
this->_window->setView(State::instance().currentView);
);
_exitButton = new kge::Button(_window, "Quit");
_exitButton->setPosition(getCenterOfScreen().x-((11*fontSize)/2), 500);
_exitButton->callback = ([this]()
this->_window->close();
);
Finally, I tell the button to update and do it's checks in my window update, button->update(td)
All my buttons seem to all do the action of the last set callback. In this case, my restart button executes my exit code.
Why is this happening and how would I fix it?
Edit
Here is my generation code:
#ifndef GameOver_hpp
#define GameOver_hpp
#include <stdio.h>
#include "View.hpp"
#include "Button.hpp"
#include "GameView.hpp"
#include "State.hpp"
namespace kge
class View;
;
class GameOver: public kge::View
private:
sf::Text* _text;
kge::Button* _restartButton;
kge::Button* _exitButton;
void restartFunction(void)
public:
GameOver(kge::Window* screen) : View(screen)
int fontSize = 50;
_text = new sf::Text();
_text->setFont(kge::AssetManager::mainBundle().getFontNamed("mainfont"));
_text->setString("Game Over");
_text->setCharacterSize(fontSize);
_text->setPosition(getCenterOfScreen().x-((9*fontSize)/2), 100);
_text->setFillColor(sf::Color(255,0,0));
_restartButton = new kge::Button(_window, "Restart");
_restartButton->setPosition(getCenterOfScreen().x-((11*fontSize)/2), 300);
_exitButton = new kge::Button(_window, "Quit");
_exitButton->setPosition(getCenterOfScreen().x-((11*fontSize)/2), 500);
_restartButton->callback = ([this]()
// State::instance().currentView = new GameView(this->_window);
// this->_window->setView(State::instance().currentView);
puts("Restart");
);
_exitButton->callback = ([this]()
// this->_window->close();
puts("Restart");
);
this->addItemToView(_text);
this->addItemToView(_restartButton);
this->addItemToView(_exitButton);
void update(float td)
_restartButton->update(td);
_exitButton->update(td);
~GameOver()
delete _text;
delete _restartButton;
;
#endif /* GameOver_hpp */
Note, kge::View is just a custom sf::Drawable class (how I create my own "views")
Edit 2
Button update function:
void Button::update(float td)
if(_clock.getElapsedTime().asMilliseconds() < 400) return;
if(!sf::Mouse::isButtonPressed(sf::Mouse::Left)) return;
if(mouseIsIn(*_buttonOutline, _window))
callback();
_clock.restart();
Please note: _clock is an sf::Clock that is stored privately in the button class.
c++ class sfml
c++ class sfml
edited Mar 22 at 8:49
iProgram
asked Mar 22 at 8:22
iProgramiProgram
1,85031439
1,85031439
Can you confirm this is happening by attaching a debugger and putting a breakpoint in both lamdbas? That may tell you why this would happen.
– Botje
Mar 22 at 8:28
@Botje I can confirm that by using break points, the IDE is showing it's on the correct callback, however it is always doing the exit event. If I remove my exit button, my restart button works just fine.
– iProgram
Mar 22 at 8:36
@Botje What is interesting is when I puts a message, that message shows correctly and if I remove the this->_window->close(); line, it works.
– iProgram
Mar 22 at 8:37
1
Could it be that all callbacks are executing? Show the code that should trigger the callbacks.
– Botje
Mar 22 at 8:40
1
It is not MCVE. How are you storing buttons? How are you iterating over them and call callbacks? Where is update definition? Where are buttons generated, you are capturingthis
so it happens from some member functions.. and so on.
– rafix07
Mar 22 at 8:40
|
show 8 more comments
Can you confirm this is happening by attaching a debugger and putting a breakpoint in both lamdbas? That may tell you why this would happen.
– Botje
Mar 22 at 8:28
@Botje I can confirm that by using break points, the IDE is showing it's on the correct callback, however it is always doing the exit event. If I remove my exit button, my restart button works just fine.
– iProgram
Mar 22 at 8:36
@Botje What is interesting is when I puts a message, that message shows correctly and if I remove the this->_window->close(); line, it works.
– iProgram
Mar 22 at 8:37
1
Could it be that all callbacks are executing? Show the code that should trigger the callbacks.
– Botje
Mar 22 at 8:40
1
It is not MCVE. How are you storing buttons? How are you iterating over them and call callbacks? Where is update definition? Where are buttons generated, you are capturingthis
so it happens from some member functions.. and so on.
– rafix07
Mar 22 at 8:40
Can you confirm this is happening by attaching a debugger and putting a breakpoint in both lamdbas? That may tell you why this would happen.
– Botje
Mar 22 at 8:28
Can you confirm this is happening by attaching a debugger and putting a breakpoint in both lamdbas? That may tell you why this would happen.
– Botje
Mar 22 at 8:28
@Botje I can confirm that by using break points, the IDE is showing it's on the correct callback, however it is always doing the exit event. If I remove my exit button, my restart button works just fine.
– iProgram
Mar 22 at 8:36
@Botje I can confirm that by using break points, the IDE is showing it's on the correct callback, however it is always doing the exit event. If I remove my exit button, my restart button works just fine.
– iProgram
Mar 22 at 8:36
@Botje What is interesting is when I puts a message, that message shows correctly and if I remove the this->_window->close(); line, it works.
– iProgram
Mar 22 at 8:37
@Botje What is interesting is when I puts a message, that message shows correctly and if I remove the this->_window->close(); line, it works.
– iProgram
Mar 22 at 8:37
1
1
Could it be that all callbacks are executing? Show the code that should trigger the callbacks.
– Botje
Mar 22 at 8:40
Could it be that all callbacks are executing? Show the code that should trigger the callbacks.
– Botje
Mar 22 at 8:40
1
1
It is not MCVE. How are you storing buttons? How are you iterating over them and call callbacks? Where is update definition? Where are buttons generated, you are capturing
this
so it happens from some member functions.. and so on.– rafix07
Mar 22 at 8:40
It is not MCVE. How are you storing buttons? How are you iterating over them and call callbacks? Where is update definition? Where are buttons generated, you are capturing
this
so it happens from some member functions.. and so on.– rafix07
Mar 22 at 8:40
|
show 8 more comments
1 Answer
1
active
oldest
votes
The issue was resolved in chat. To summarize, the mouseIsIn
function that @iProgram posted did not check collision correctly, leading to multiple buttons triggering at the same time.
The original function:
bool mouseIsIn(sf::RectangleShape shape, sf::RenderWindow* window)
sf::FloatRect shapeBounds = shape.getGlobalBounds();
sf::Vector2i mousePos = sf::Mouse::getPosition(*window);
sf::FloatRect mouseRect = sf::FloatRect(mousePos.x, mousePos.y, mousePos.x+shapeBounds.width, mousePos.y+shapeBounds.height);
return mouseRect.intersects(shapeBounds);
The fixed function:
bool mouseIsIn(sf::RectangleShape shape, sf::RenderWindow* window)
sf::FloatRect shapeBounds = shape.getGlobalBounds();
sf::Vector2i mousePos = sf::Mouse::getPosition(*window);
return shapeBounds.contains(mousePos.x, mousePos.y);
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55295508%2fwhy-do-my-button-class-items-share-the-same-lambda-functions%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The issue was resolved in chat. To summarize, the mouseIsIn
function that @iProgram posted did not check collision correctly, leading to multiple buttons triggering at the same time.
The original function:
bool mouseIsIn(sf::RectangleShape shape, sf::RenderWindow* window)
sf::FloatRect shapeBounds = shape.getGlobalBounds();
sf::Vector2i mousePos = sf::Mouse::getPosition(*window);
sf::FloatRect mouseRect = sf::FloatRect(mousePos.x, mousePos.y, mousePos.x+shapeBounds.width, mousePos.y+shapeBounds.height);
return mouseRect.intersects(shapeBounds);
The fixed function:
bool mouseIsIn(sf::RectangleShape shape, sf::RenderWindow* window)
sf::FloatRect shapeBounds = shape.getGlobalBounds();
sf::Vector2i mousePos = sf::Mouse::getPosition(*window);
return shapeBounds.contains(mousePos.x, mousePos.y);
add a comment |
The issue was resolved in chat. To summarize, the mouseIsIn
function that @iProgram posted did not check collision correctly, leading to multiple buttons triggering at the same time.
The original function:
bool mouseIsIn(sf::RectangleShape shape, sf::RenderWindow* window)
sf::FloatRect shapeBounds = shape.getGlobalBounds();
sf::Vector2i mousePos = sf::Mouse::getPosition(*window);
sf::FloatRect mouseRect = sf::FloatRect(mousePos.x, mousePos.y, mousePos.x+shapeBounds.width, mousePos.y+shapeBounds.height);
return mouseRect.intersects(shapeBounds);
The fixed function:
bool mouseIsIn(sf::RectangleShape shape, sf::RenderWindow* window)
sf::FloatRect shapeBounds = shape.getGlobalBounds();
sf::Vector2i mousePos = sf::Mouse::getPosition(*window);
return shapeBounds.contains(mousePos.x, mousePos.y);
add a comment |
The issue was resolved in chat. To summarize, the mouseIsIn
function that @iProgram posted did not check collision correctly, leading to multiple buttons triggering at the same time.
The original function:
bool mouseIsIn(sf::RectangleShape shape, sf::RenderWindow* window)
sf::FloatRect shapeBounds = shape.getGlobalBounds();
sf::Vector2i mousePos = sf::Mouse::getPosition(*window);
sf::FloatRect mouseRect = sf::FloatRect(mousePos.x, mousePos.y, mousePos.x+shapeBounds.width, mousePos.y+shapeBounds.height);
return mouseRect.intersects(shapeBounds);
The fixed function:
bool mouseIsIn(sf::RectangleShape shape, sf::RenderWindow* window)
sf::FloatRect shapeBounds = shape.getGlobalBounds();
sf::Vector2i mousePos = sf::Mouse::getPosition(*window);
return shapeBounds.contains(mousePos.x, mousePos.y);
The issue was resolved in chat. To summarize, the mouseIsIn
function that @iProgram posted did not check collision correctly, leading to multiple buttons triggering at the same time.
The original function:
bool mouseIsIn(sf::RectangleShape shape, sf::RenderWindow* window)
sf::FloatRect shapeBounds = shape.getGlobalBounds();
sf::Vector2i mousePos = sf::Mouse::getPosition(*window);
sf::FloatRect mouseRect = sf::FloatRect(mousePos.x, mousePos.y, mousePos.x+shapeBounds.width, mousePos.y+shapeBounds.height);
return mouseRect.intersects(shapeBounds);
The fixed function:
bool mouseIsIn(sf::RectangleShape shape, sf::RenderWindow* window)
sf::FloatRect shapeBounds = shape.getGlobalBounds();
sf::Vector2i mousePos = sf::Mouse::getPosition(*window);
return shapeBounds.contains(mousePos.x, mousePos.y);
answered Mar 22 at 9:20
BotjeBotje
2,559915
2,559915
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55295508%2fwhy-do-my-button-class-items-share-the-same-lambda-functions%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Can you confirm this is happening by attaching a debugger and putting a breakpoint in both lamdbas? That may tell you why this would happen.
– Botje
Mar 22 at 8:28
@Botje I can confirm that by using break points, the IDE is showing it's on the correct callback, however it is always doing the exit event. If I remove my exit button, my restart button works just fine.
– iProgram
Mar 22 at 8:36
@Botje What is interesting is when I puts a message, that message shows correctly and if I remove the this->_window->close(); line, it works.
– iProgram
Mar 22 at 8:37
1
Could it be that all callbacks are executing? Show the code that should trigger the callbacks.
– Botje
Mar 22 at 8:40
1
It is not MCVE. How are you storing buttons? How are you iterating over them and call callbacks? Where is update definition? Where are buttons generated, you are capturing
this
so it happens from some member functions.. and so on.– rafix07
Mar 22 at 8:40