Hassan Hassan - 1 year ago 138
C++ Question

How to get reference to clicked sf::CircleShape?

In my SFML program I am storing drawn CircleShapes in a Vector. How to get reference to one on clicking mouse button?

Answer Source

There's no shape.makeClickable() function in SFML, all you have to do is:

sf::CircleShape* onClick(float mouseX, float mouseY) {//Called each time the players clicks
    for (sf::CircleShape circle : vec) {
        float distance = hypot((mouseX - circle.getPosition().x), (mouseY - circle.getPosition().y)); //Checks the distance between the mouse and each circle's center
        if (distance <= circle.getRadius)
            return &circle;
    return nullptr;

With this vector in your class:

std::vector<sf::CircleShape> vec;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download