I’m using SFML, and I want to make a sprite rotate when the left or right arrow keys are pressed. I don’t want to use while loops, and I had another version of this that worked, but it moved like you were holding a key in a google doc, it rotates once, pauses, then rotates normally.
if (Keyboard::isKeyPressed(Keyboard::Left)) {
rotateLeft = true;
}
if (Keyboard::isKeyPressed(Keyboard::Right)) {
rotateRight = true;
}
if (Event::KeyReleased && event.key.code == Keyboard::Left) {
rotateLeft = false;
}
if (Event::KeyReleased && event.key.code == Keyboard::Right) {
rotateRight = false;
}
if (rotateLeft) {
player.rotate(-playerSpeed);
}
if (rotateRight) {
player.rotate(playerSpeed);
}
I’ve tried nesting the loops to seperate the conditions, like this:
//Something like this i think
if (Keyboard::KeyPressed) {
if (event.key.code == Keyboard::Left) {
rotateLeft = true;
}
if (event.key.code == Keyboard::Right) {
rotateRight = true;
}
}
if (Keyboard::KeyReleased) {
if (event.key.code == Keyboard::Left) {
rotateLeft = false;
}
if (event.key.code == Keyboard::Right) {
rotateRight = false;
}
}
//Something like that i think
As stated earlier, I expect the sprite to rotate when the left or right arrow keys are pressed. But, it only happens if I press another key at the same time, it could even be another one of the arrow keys.
Christian Boe is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
4