I have a sprite called “player” and I tried to make it rotate using the arrow keys.
while (window.pollEvent(event)) {
if (Event::Closed) {
window.close();
}
if (Keyboard::isKeyPressed(Keyboard::Left)) {
player.rotate(3);
}
if (Keyboard::isKeyPressed(Keyboard::Right)) {
player.rotate(-3);
}
}
When I run the code, it results in the sprite rotating like I’m holding a key while typing, it moves once, pauses, then moves constantly.
Could anyone help me out with this problem?
Christian B. is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1
You should not mix events and real-time inputs, see also the official tutorial.
Keyboard events are triggered at a specific interval (usually configurable in your OS/peripheral settings), but unless you’re writing text, you don’t actually want to rely on that repetition for your game actions.
What you can do is track the key states in separate variables, i.e. when the KeyPressed event triggers, you set the state to “pressed” and when KeyReleased is triggered, you set it to “released”. Whether this is a simple bool
variable, something more generic std::unordered_map<sf::Keyboard::Scancode, bool>
, or in any other way is up to you.
You can then in you code, outside of the event loop, do something like this:
if (keys[sf::Keyboard::Scancode::Left])
{
player.rotate(3 * dt.asSeconds());
}
Note the use of dt
(i.e. delta time), this is to ensure that your movement is framerate independent, meaning that you won’t just rotate as fast as your framerate is currently at, but that it would rotate over time. The delta time represents the frame time, which means how long it took to render the previous frame.
auto clock = sf::Clock{};
while (window.isOpen())
{
auto dt = clock.restart();
// Handle events
if (keys[sf::Keyboard::Scancode::Left])
{
player.rotate(3 * dt.asSeconds());
}
window.clear();
window.draw(player);
window.display();
}
Note that this a very simple approach and for more “serious” games, you may consider using a fix timestep with interpolation, to be truly framerate independent.