【发布时间】:2016-10-26 09:03:55
【问题描述】:
在我的 SFML 程序中,我将绘制的 CircleShapes 存储在向量中。如何在单击鼠标按钮时获得引用?
【问题讨论】:
在我的 SFML 程序中,我将绘制的 CircleShapes 存储在向量中。如何在单击鼠标按钮时获得引用?
【问题讨论】:
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;
}
【讨论】: