sfml中的鼠标位置和点击检测

Bla*_*bbs 0 c++ sfml

int Game::MouseOnDot(float x, float y, RenderWindow &renderWindow) {
    Rect<int> Dot;
    Event event;
    Dot.left = x;
    Dot.top = y;
    Dot.width = 20;
    Dot.height = 20;
    while (renderWindow.pollEvent(event)) {
        float Mx = sf::Mouse::getPosition().x;
        float My = sf::Mouse::getPosition().y;
        if (event.type == Event::MouseButtonReleased&&Mx > x && Mx < Dot.height && My > y && My < Dot.width){
                return 1;
        }
            else
                return 0;
    }
}
Run Code Online (Sandbox Code Playgroud)

我不知道为什么如果在返回 1 的点上按下按钮,这将不起作用,它告诉其他函数关闭窗口。我在鼠标位置上做错了吗?

while (renderWindow.isOpen()) {
        processEvents(renderWindow);
        float Time = clock.getElapsedTime().asSeconds();
        float TimeDifference = Time - LastUpdateTime;
        if (TimeDifference >= UpdateTime) {
            processEvents(renderWindow);
            y += 3;
            if (y <= 770) {
                if(Game::MouseOnDot(x, y, renderWindow)==1)
                                    renderWindow.close();
                Game::Spawn(renderWindow, Green_Dots, x, y);
                LastUpdateTime = Time;
                return;
Run Code Online (Sandbox Code Playgroud)

当 MouseOnDot 返回 0 或 1 时,我仍然不工作,我将部分粘贴到这里。它不会关闭窗口,我不知道为什么?

小智 5

使用 sf::Mouse::getPosition().x 返回相对于桌面的位置,如果你想要它相对于你的 renderWindow 你需要做: sf::Mouse::getPosition(renderWindow).x

那么阿提拉关于鼠标/点比较是完全正确的:)