此功能似乎使应用程序崩溃。我将其注释掉,然后该应用程序运行良好。你们看到我在做什么错吗?buttons
是map
,其键的类型为string
,值的类型为sf::sprite
我让它在while
循环中运行,该循环被告知菜单已打开
std::string Menu::buttonPress(sf::RenderWindow* window, sf::Event* event)
{
while(window->pollEvent(*event))
{
if(event->type == sf::Event::EventType::MouseButtonPressed)
{
sf::Vector2i mousepos = sf::Mouse::getPosition(*window);
for(auto it: buttons)
{
sf::Rect<float> sprite;
sprite = it.second.getGlobalBounds();
if(sprite.contains(mousepos.x,mousepos.y))
{
return it.first;
}
}
}
}
}
编辑
此
function
在while循环内运行。window
是sf::RenderWindow
。event
是sf::Event
。if(openmenu)
{
options.showMenu(&window); //This draws to the renderwindow
if(options.buttonPress(&window, &event) == "Exit")
{
openmenu = false;
window.close();
}
}
最佳答案
如果事件队列中没有MouseButtonPressed
事件(我希望大多数迭代都是这种情况),或者如果存在,则鼠标位置不在您的按钮之一内,您的函数不返回值,导致未定义的行为。
关于c++ - 鼠标碰撞测试时SFML崩溃进程终止255,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25656528/