Сергей Asked:2020-04-13 05:42:02 +0000 UTC2020-04-13 05:42:02 +0000 UTC 2020-04-13 05:42:02 +0000 UTC SFML C ++单击对隐形精灵的反应[关闭] 772 在应用程序窗口的不同位置有几个精灵——它们都被隐藏了。当您单击这些精灵时,会与它们进行交互,尽管它们是隐藏的。也就是说,通过单击屏幕上的空白区域,它们处于活动状态。如何摆脱它? c++ 2 个回答 Voted user252359 2020-04-13T07:55:54Z2020-04-13T07:55:54Z 你可以这样做: int main() { sf::RenderWindow wind(sf::VideoMode(800, 600, 32), "Window", sf::Style::Default); bool draw = true; while (wind.isOpen()) { sf::Vector2f rectSize(100,50); sf::RectangleShape rect; rect.setSize(rectSize); rect.setOrigin(rectSize.x/ 2, rectSize.y/2); rect.setPosition(wind.getSize().x / 2, wind.getSize().y / 2); rect.setFillColor(sf::Color::Red); sf::Event sfEvent; while (wind.pollEvent(sfEvent)) { if (sfEvent.type == sf::Event::Closed) { wind.close(); } if (sfEvent.type == sf::Event::KeyPressed) { if (sfEvent.key.code == sf::Keyboard::Enter) { draw = !draw; } } } sf::Vector2i pos = sf::Mouse::getPosition(wind); float halfX = rectSize.x / 2; float halfY = rectSize.y / 2; if (draw) { if (pos.x >= rect.getPosition().x - halfX && pos.x <= rect.getPosition().x + halfX || pos.y >= rect.getPosition().y - halfY && pos.y <= rect.getPosition().y + halfY) { std::cout << "Inside" << std::endl; } } wind.clear(); if (draw) { wind.draw(rect); } wind.display(); } return 0; } Best Answer Сергей 2020-04-13T18:32:30Z2020-04-13T18:32:30Z 一种可能的方法是通过条件if(true){}和if(false) {}分别暂时阻止和取消阻止鼠标与不可见精灵的交互。
你可以这样做:
一种可能的方法是通过条件
if(true){}和if(false) {}分别暂时阻止和取消阻止鼠标与不可见精灵的交互。