首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >鼠标定位并单击sfml中的检测

鼠标定位并单击sfml中的检测
EN

Stack Overflow用户
提问于 2012-06-09 23:34:24
回答 2查看 15.7K关注 0票数 0
代码语言:javascript
代码运行次数:0
运行
复制
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;
    }
}

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

代码语言:javascript
代码运行次数:0
运行
复制
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;

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

EN

回答 2

Stack Overflow用户

发布于 2013-04-02 02:58:37

使用sf::Mouse::getPosition().x返回相对于桌面的位置,如果需要相对于renderWindow,则需要执行以下操作: sf::Mouse::getPosition(renderWindow).x

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

票数 5
EN

Stack Overflow用户

发布于 2012-06-09 23:44:03

我认为你的问题是,你将位置与x坐标和高度进行比较。您需要比较x和x+height (与y坐标/宽度类似)

尝试:

代码语言:javascript
代码运行次数:0
运行
复制
if (event.type == Event::MouseButtonReleased && 
    Mx > x && Mx < x + Dot.height &&
    My > y && My < y + Dot.width) {
  //...
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10962270

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档