Hello. In my game I want the player's character to sprint if I double tap the movement keys. (WASD)
Here is my current movement logic...
void PlayerCharacter::handleInputCurrent(sf::Event& event) {
int speed = DefaultSpeed;
setVelocity(sf::Vector2f(0, 0));
if (event.type == sf::Event::KeyReleased) {
switch (event.key.code) {
case sf::Keyboard::W:
m_state = IdleUp;
break;
case sf::Keyboard::S:
m_state = IdleDown;
break;
case sf::Keyboard::A:
m_state = IdleLeft;
break;
case sf::Keyboard::D:
m_state = IdleRight;
break;
}
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::W)) {
setVelocity(sf::Vector2f(getVelocity().x, -speed));
m_state = WalkUp;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::S)) {
setVelocity(sf::Vector2f(getVelocity().x, speed));
m_state = WalkDown;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::A)) {
setVelocity(sf::Vector2f(-speed, getVelocity().y));
m_state = WalkLeft;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::D)) {
setVelocity(sf::Vector2f(speed, getVelocity().y));
m_state = WalkRight;
}
}
By the way,
m_state is used for animation purposes and
setVelocity(x, y) is used to set the velocity of an entity.
Thanks for reading!