2016-04-20 27 views
-2

當我按下跳躍按鈕時,如何阻止我的精靈突然在空中晃動?如何阻止我的精靈在SFML中空中顛簸?

我的代碼:

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; 
} 

我希望我的精靈跳起來下來順利當我按下跳躍現在突然顛簸起來,但回來下來順利。

+0

請提供[mcve]。否則幾乎不可能分辨出什麼是錯的。 – nvoigt

回答

0

在這部分,你不應該使用重力而不是跳躍速度?

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(); 
} 
+0

不,jumpSpeed用於跳躍,如果精靈不再與地面碰撞,則會應用重力。 –