Hassan Hassan - 1 month ago 6
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

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;