The gravity is smooth going down however, when I press space it kind of jolts up. GIF: http://i.gyazo.com/a876e13925dde99c3bbbe8b14a1fa164.gif
#include "Player.h"
Player::Player()
{
if (!playerTexture.loadFromFile("images/wizardT.png"))
{
std::cout << "Can't load the player image";
}
playerImage.setTexture(playerTexture);
playerImage.setPosition(200, 200);
playerImage.setTextureRect(sf::IntRect(48, 14, 73, 73));
m_playerSpeed = 5.0f;
gravity = 2.0;
groundHeight = 525;
flyTime = 0;
isGrounded = true;
keyPressed = false;
moveSpeed = 2.0f;
jumpSpeed = 150.0f;
}
Player::~Player()
{
}
void Player::OnKeyDown(){
sf::Vector2f velocity(sf::Vector2f(0, 0));
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Space) && keyPressed == false && playerImage.getPosition().y >= 0){
keyPressed = true;
velocity.y = -jumpSpeed;
std::cout << "jump";
}
else if (!sf::Keyboard::isKeyPressed(sf::Keyboard::Space))
{
keyPressed = false;
}
if (playerImage.getPosition().y + playerImage.getScale().y < groundHeight || velocity.y < 0)
{
velocity.y += gravity;
}
else{
playerImage.setPosition(playerImage.getPosition().x, groundHeight - playerImage.getScale().y);
velocity.y = 0;
}
playerImage.move(velocity.x, velocity.y);
}
void Player::Draw(sf::RenderWindow &window){
window.draw(playerImage);
}