如何获得参考点击sf::CircleShape
How to get reference to clicked sf::CircleShape?
在我的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;
}
相关文章:
- 使用 sf::Text 数组时崩溃
- 尝试将 sf::Texture* 推送到 std::vector 时出错
- 为什么 sf::Text 显示点而不是数字?
- sf::文本不显示?
- sf::Windows上的音乐:api-ms-win-crt-locale-l1-1-0.dll:无法打开共享对象文件
- 如何将sf::RenderWindow传递给另一个函数
- 当我尝试绘制 sf::Text 对象时,SFML 中的分段错误
- 重载运算符时出错<在 sf::Vector 中
- 声明 sf::SoundBuffer 会导致程序在终止时崩溃
- 正在加载 sf::图片来自 QDialog
- SFML 中的分段错误 调用 sf::窗口::关闭后
- 未解决的外部符号sf ::带有SFML的时间
- crtdbg 在使用 sf::Text::setOutlineThickness 时转储内存泄漏
- sf::声音函数会导致链接器错误
- SFML sf::Mouse::getPosition 方法无法编写正确的参数
- SF :: Renderwindow的destrutor被检测到的堆叠粉碎
- 为什么我的SFML将SF ::纹理作为参数传递到函数时会不断崩溃
- SFML/C++ 闪烁命中效果 sf::精灵
- SFML 2.4.2 在绘制 sf::Text 对象之前获取本地边界
- 如何获得参考点击sf::CircleShape