SFML community forums
Help => General => Topic started by: DarknessxBuilder on June 03, 2017, 06:47:22 pm
-
hi all im new to sfml and have been trying to make a game and require a bit of help
when i want my chracater go left and right i use these 2 functions.
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
and now i want to animate my character standing still is their a function to detect if their isnt any keypresses
i tried using this function below but it didnt work
if (sf::Event::KeyReleased)
Any Ideas?
-
You may want to do some research into "state machines".
-
Animation also consists in update the frames...
so update the idle animation in game loop when are not updating the left or right animation...
-
You can do something like this to get you started.
But I recommend maybe you look into state machines if your game is going to get complex.
// Couple of ways to do it.
// #1 Player position is the same as his previous position
if(playerPos == prevPlayerPos){
// change animation to idle
}
// #2 Player velocity = 0, player is not moving
if(playerVel.x == 0 && playerVel.y == 0){
// change animation to idle
}
#3
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) {
// change animation to left
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) {
// change animation to right
}
else{
// change animation to idle
}
}
Hope this helps you get started.