制作平滑滑行动画 SFML
Make smooth gliding animation SFML
所以,我有一个不允许在屏幕外移动的精灵。我做了一个函数,将播放器放回屏幕,它应该在的位置(我将播放器的原点设置在中间,这就是 *0.5 代表的意思):
if (playerSprite.getPosition().y + playerSprite.getGlobalBounds().height*0.5 > VideoMode::getDesktopMode().height)
playerSprite.setPosition(Vector2f(playerSprite.getPosition().x, VideoMode::getDesktopMode().height - playerSprite.getGlobalBounds().height*0.5));
if (playerSprite.getPosition().x + playerSprite.getGlobalBounds().width*0.5 > VideoMode::getDesktopMode().width)
playerSprite.setPosition(VideoMode::getDesktopMode().width - playerSprite.getGlobalBounds().width*0.5, playerSprite.getPosition().y);
if (playerSprite.getPosition().y - playerSprite.getGlobalBounds().height*0.5 < 0)
playerSprite.setPosition(Vector2f(playerSprite.getPosition().x, playerSprite.getGlobalBounds().height*0.5));
if (playerSprite.getPosition().x - playerSprite.getGlobalBounds().width*0.5 < 0)
playerSprite.setPosition(Vector2f(playerSprite.getGlobalBounds().width*0.5, playerSprite.getPosition().y));
有了这几段代码,我的播放器不仅会在碰到屏幕边缘时停止(当然,当它不垂直时),而是在屏幕边缘平滑地滑动。但是当我试图为另一个精灵做同样的事情时,比如说一个矩形精灵,无论碰撞角度如何,玩家都会停下来。谁能向我解释这种行为,并建议我如何让它沿着边缘滑动?这将是我已经尝试过的(这里仅用于向上移动,它是左、右、向下的模拟):
if (Keyboard::isKeyPressed(Keyboard::W)
sprite.movePlayer('W', speed);
if(playerSprite.getGlobalBounds().intersects(entity.getGlobalBounds()))
sprite.movePlayer('S', speed);
问题可能是,对于每个更新帧,您都在对精灵在其所命中的边界方向上的速度执行硬否定,同时仍然在它仍在边界内的方向上应用速度。 当精灵到达边界时,冻结所有移动,而不仅仅是向边界移动。
相关文章:
- 在执行其他功能的同时播放动画(LED矩阵和Arduino/ESP8266)
- SFML纹理像播放器
- 如何使用AngelScript注册SFML Vector2运算符
- 如何修复sfml c++代码编译错误
- SFML library: http request
- 落砂模拟碰撞检测C++和SFML
- 使用 SFML 和 C++ 将 Pixel 打印到屏幕上
- SFML RenderWindow打开窗口需要很长时间
- 使用 GLUT 使用键停止动画?
- 矢量下标超出SFML游戏中的范围
- 获取 SFML 窗口的 HWND 和高可用性?
- 将 Vulkan 与 SFML 结合使用?
- SFML 文本未绘制在窗口上
- SFML 碰撞永远不会在我的系统中注册
- 使用对象数组对 SFML 进行动画处理
- (SFML)按下键时,播放器构造函数未使用正确的动画进行更新
- 从精灵表在 sfml 中为精灵制作动画
- 制作平滑滑行动画 SFML
- 鼠标点击后爆炸的sfml动画
- 无键盘输入的SFML动画