我如何才能阻止我的精灵在SFML中在空中颠簸

How can I stop my sprite from jolting up in air in SFML?

本文关键字:精灵 SFML 颠簸 在空中 我的      更新时间:2023-10-16

当我按下jump时,我如何才能阻止我的精灵突然在空中颠簸?

我的代码:

sf::Vector2f velocity, position;
const float speed,gravity,jumpSpeed;
bool groundCollision,jumping;
if (!groundCollision) {
    isJumping = true;
    velocity.y += gravity;
} else {
    isJumping = false;
}
if (!isJumping && sf::Keyboard::isKeyPressed(sf::Keyboard::Space)) {
    velocity.y -= jumpSpeed;
}

我希望我的精灵在按下jump时能平稳地上下跳跃,现在它突然向上摇晃,但又平稳地回来了。

在这部分中,你不应该使用重力而不是跳跃速度吗?

if (!isJumping && sf::Keyboard::isKeyPressed(sf::Keyboard::Space))
{
    //velocity.y -= jumpSpeed * deltaTime.asSeconds();
    //              ^ should this jumpSpeed be gravity?
    velocity.y = jumpSpeed;
    velocity.y -=  gravity * deltaTime.asSeconds();
}