【问题标题】:How to get reference to clicked sf::CircleShape?如何获得对单击的 sf::CircleShape 的引用?
【发布时间】:2016-10-26 09:03:55
【问题描述】:

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

【问题讨论】:

    标签: c++ sfml graphic


    【解决方案1】:

    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;
    

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-10-03
      • 1970-01-01
      • 2013-05-06
      • 2019-09-10
      • 1970-01-01
      • 2018-11-14
      • 2012-04-22
      相关资源
      最近更新 更多