我目前正在将 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/