当我移动我的精灵时,它一直从窗户掉下来

As I move my sprite around it keeps going off the window

本文关键字:一直 窗户 移动 我的 精灵      更新时间:2023-10-16

我目前正在将SFML与c++一起使用,并且在保持精灵在页面上时遇到了一个小问题。下面是代码,如果有人有任何想法可以帮助解决我的问题,那将是惊人的!

     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 

它返回精灵顶部的X。所以我不得不把这句话放在它上面,以使它正确工作:

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

现在它停止在屏幕长度上,如果屏幕大小等被调整,它将继续工作。

您的测试是"y<screenLength",但在调用move时会降低值。记录你的y,也许你的解决方案类似于"如果y>=0"。