c++ - 当我四处移动我的 Sprite 时,它一直在离开窗口

标签 c++ window game-engine sfml

我目前正在将 SFML 与 C++ 结合使用,但在将我的 sprite 保留在页面上时遇到了一个小问题。下面是代码,如果有人有任何想法可以帮助解决我的问题,那就太棒了!

     if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down) && shape1.getPosition().y < screenLength)
        {   
                if (shape1.getGlobalBounds().intersects( line1.getGlobalBounds()) || shape1.getGlobalBounds().intersects(line.getGlobalBounds()) || shape1.getGlobalBounds().intersects(line2.getGlobalBounds()))
                {
                    shape1.move(0.0f, -.2f);                    
                    std::cout << "Collision";
                }

当我不断按下向下按钮时,它让我离开生成的窗口底部,而不是像根据 if 语句应该跳过它。

最佳答案

我发现了问题并解决了...所以当我接受

 shape1.globalBounds().x 

它返回 Sprite 顶部的 X。所以我不得不把这一行放在它以使其正常工作:

float test = shape1.getGlobalBounds().top +  shape1.getGlobalBounds().height;

现在它停止在 screenLength 上,如果调整屏幕尺寸等,它将继续工作。

关于c++ - 当我四处移动我的 Sprite 时,它一直在离开窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26385521/

相关文章:

c++ - (C++) 得到错误:类::函数的无效使用,我一点也不知道为什么

c++ - 重定向子进程的输出

actionscript-3 - ActionScript 3、角色选择

c++ - Directx:Obj文件解析到Index Buffer

android - 使用 LibGDX 制作静态动画的有效方法?

c++ - g++ c++11 constexpr 评估性能

c++ - 泄漏是什么意思?

javascript - IE8 中关闭子弹出窗口失败

C++ Lua,获取全局字符串

cocoa - 如何创建模态窗口