如何获得对点击的sf :: CircleShape的引用?

时间:2016-10-26 09:03:56

标签: c++ sfml graphic

在我的SFML程序中,我将绘制的CircleShapes存储在Vector中。如何通过单击鼠标按钮来引用一个?

1 个答案:

答案 0 :(得分:0)

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

在班上使用此向量:

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

EDIT
获取您点击的所有圈子,而不仅仅是它找到的第一个圈子:

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