C++ 在矩形中查找鼠标单击';使用sfml的s向量
我们刚刚自学了SFML,我们正在尝试实现一个函数,即查找鼠标单击位置并在矩形向量中查找它。我们正在尝试为单击的特定矩形的轮廓上色。。。这就是我们试图写的:C++ 在矩形中查找鼠标单击';使用sfml的s向量,c++,sfml,C++,Sfml,我们刚刚自学了SFML,我们正在尝试实现一个函数,即查找鼠标单击位置并在矩形向量中查找它。我们正在尝试为单击的特定矩形的轮廓上色。。。这就是我们试图写的: void Controller::run_board(Board ourBoard) { while (m_window.isOpen()) { sf::Event e; while (m_window.waitEvent(e)) { m_window
void Controller::run_board(Board ourBoard)
{
while (m_window.isOpen())
{
sf::Event e;
while (m_window.waitEvent(e))
{
m_window.clear();
drawboard(ourBoard);
creatMenu(ourBoard);
m_window.display();
if (auto event = sf::Event{}; m_window.waitEvent(event))
{
switch (event.type)
{
case sf::Event::Closed:
m_window.close();
break;
}
if (event.type == sf::Event::MouseButtonPressed)
{
sf::RectangleShape rec;
if (event.mouseButton.button == sf::Mouse::Left) {
auto xCoord = event.mouseButton.x;
std::cout << xCoord;
auto yCoord = event.mouseButton.y;
sf::Vector2f worldPos = m_window.mapPixelToCoords(yCoord);
rec.setPosition(xCoord, yCoord);
//m_window.clear();
for (int i = 0; i < m_length; i++)
for (int j; j < m_width; j++)
{
if (m_vecRec[i][j].getGlobalBounds(worldPos))
{
m_vecRec[i][j].setOutlineColor(sf::Color::Red);
m_window.draw(m_vecRec[i][j]);
}
}
//m_window.clear();
m_window.display();
// m_recMenu.setFillColor(sf::Color::Red);
//rec.setPosition(c.x, c.y);
}
}
}
}
}
}
void控制器::运行线路板(线路板)
{
while(m_window.isOpen())
{
sf::事件e;
while(m_window.waitEvent(e))
{
m_window.clear();
拖板;
创建菜单(我们的董事会);
m_window.display();
if(auto event=sf::event{};m_window.waitEvent(event))
{
开关(事件类型)
{
案例sf::事件::已结束:
m_window.close();
打破
}
if(event.type==sf::event::MouseButtonPressed)
{
sf::矩形形状rec;
if(event.mouseButton.button==sf::Mouse::Left){
auto xCoord=event.mouseButton.x;
std::cout如果m_vecRec[i][j]
是形状
或精灵
,getGlobalBounds
将返回一个边框。您应该能够调用。包含(worldPos)
,查看鼠标位置是否在形状的边框内:
if (m_vecRec[i][j].getGlobalBounds().contains(worldPos)) { ... }
我建议将这对I,j
存储在控制器中的某个位置,这样您就可以记住最后单击的矩形,并在必要时撤消着色