如何获取对 clicked sf::CircleShape 的引用?

How to get reference to clicked sf::CircleShape?

在我的 SFML 程序中,我将绘制的 CircleShapes 存储在 Vector 中。如何在单击鼠标按钮时获取引用?

SFML中没有shape.makeClickable()功能,你只需要:

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;
}

在您的 class 中使用此矢量:

std::vector<sf::CircleShape> vec;

编辑
要获得您点击过的所有圈子,而不仅仅是它找到的第一个圈子:

std::vector<sf::CircleShape*> onClick(float mouseX, float mouseY) {//Called each time the players clicks
    std::vector<sf::CircleShape*> clicked;
    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())
            clicked.push_back(&circle);
    }
    return clicked;
}