尝试使用SFML for Comp Sci final制作按钮,并且真的不想在每个按钮上绘制不可见的精灵。
我找到了一些解决方案,但他们都使用旧版本的sfml,这些功能已被删除或更改,不知道它们已被更改为什么。
while(window.isOpen()){
Event event;
while(window.pollEvent(event)){
switch(event.type)
{
case Event::Closed:
window.close();
cout << "Window Closed!" << endl;
break;
case Event::MouseButtonPressed:
if(event.mouseButton.button == Mouse::Left){
cout << " if(event.mouseButton.button == Mouse::Left){" << endl;
if(equationsButtonText.getLocalBounds().contains(event.mouseButton.x, event.mouseButton.y)){
cout << "This works!" << endl;
}
}
default:
break;
}
}
}
cout <<“if(event.mouseButton.button == Mouse :: Left){”<< endl;只是为了测试它到达循环的距离。
getLocalBounds
返回文本本地坐标中的边界。您需要使用getGlobalBounds
以世界坐标获取它。
您还需要使用窗口的mapPixelToCoords
方法将鼠标的坐标也转换为世界坐标。
它会是这样的:
if(equationsButtonText.getGlobalBounds().contains(window.mapPixelToCoords({event.mouseButton.x, event.mouseButton.y}))){
cout << "This works!" << endl;
}