当我移动我的精灵时,它一直从窗户掉下来
As I move my sprite around it keeps going off the window
我目前正在将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"。
相关文章:
- 即使我读取了所有内容,在FIFO上打开的QSocketNotifier也会一直启动
- Switch 语句(字符串)一直选择默认值,除非其为零
- 如何在窗户上使用和配置叮当声?
- 链接 c++ 动态库一直失败
- 我试图用这段代码找到二叉树的高度,但它一直返回 0,有人可以告诉我为什么吗?
- 我正在尝试创建一个菜单,但我的代码一直在循环
- 我想在 c++ 中对单向链表进行冒泡排序,但我一直面临左值错误,无法解决它
- 我一直试图弄清楚我在这个链表程序中做错了什么
- 我正在尝试在 C++ 中做一个方程,但它一直输出 1
- 工作线程一直在等待,condition_variable甚至调用了notify_all
- 从 c++ 运行 MsiExec.exe?窗户
- 我一直得到'std::invalid_argument'什么():似乎无缘无故地抛出 stoi 异常?
- 我无法让我的程序工作,我一直得到未定义的符号:C
- 我一直收到错误"cannot convert 'float*' to 'float' in return"
- 我需要帮助创建一个评分系统,但它一直给我一个错误,注释掉的整数是给我带来麻烦的部分
- 选择"Y"继续后,我的程序一直处于关闭状态
- 为什么这一直打印圆的宽度?
- 如何找出为什么我的程序一直有一个无限循环?
- Ofstream一直给我函数的最后一行,而不是显示所有内容
- 当我移动我的精灵时,它一直从窗户掉下来