Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Need Help With IDLE ANIMATION (Character Standing Still)  (Read 1727 times)

0 Members and 1 Guest are viewing this topic.

DarknessxBuilder

  • Newbie
  • *
  • Posts: 1
    • View Profile
Need Help With IDLE ANIMATION (Character Standing Still)
« 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?

dabbertorres

  • Hero Member
  • *****
  • Posts: 506
    • View Profile
    • website/blog
Re: Need Help With IDLE ANIMATION (Character Standing Still)
« Reply #1 on: June 03, 2017, 08:00:17 pm »
You may want to do some research into "state machines".

Carlos Augusto Br Cpp

  • Newbie
  • *
  • Posts: 40
  • Programming is life
    • View Profile
    • Email
Re: Need Help With IDLE ANIMATION (Character Standing Still)
« Reply #2 on: June 04, 2017, 05:43:15 am »
Animation also consists in update the frames...

so update the idle animation in game loop when are not updating the left or right animation...

smilesprower

  • Newbie
  • *
  • Posts: 25
    • View Profile
    • Email
Re: Need Help With IDLE ANIMATION (Character Standing Still)
« Reply #3 on: June 04, 2017, 03:24:24 pm »
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.

Code: [Select]
                // 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.
« Last Edit: June 04, 2017, 03:30:47 pm by smilesprower »