Hello,
I have the following code that allows a sprite to move (in this case the main player's sprite) using the default arrow keys:
void NewPlayer::ProcessInputs(sf::RenderWindow &Target)
{
//The code follows:
//If an arrow key is pressed
//If the sprite if bellow or above the axis value required
//Move the sprite
//UP
if(Target.GetInput().IsKeyDown(sf::Key::Up))
if(splayer.GetPosition().y > 40.0)
splayer.Move(0,-m_movespeed);
//DOWN
if(Target.GetInput().IsKeyDown(sf::Key::Down))
if(splayer.GetPosition().y < 730.0)
splayer.Move(0,m_movespeed);
//LEFT
if(Target.GetInput().IsKeyDown(sf::Key::Left))
if(splayer.GetPosition().x > 240)
splayer.Move(-m_movespeed,0);
//RIGHT
if(Target.GetInput().IsKeyDown(sf::Key::Right))
if(splayer.GetPosition().x < 780.0)
splayer.Move(m_movespeed,0);
}
However it's glitching me a bit.
If the sprite reaches the top of the screen and if i keep pressing UP, the sprite doesn't move, good!
However if I keep pressing UP and then press DOWN
at the same time, the sprite nudges down a bit and doesn't move until I release UP.
The only problem here is that little nudge. Even though it's just a small movement, it feels really strange during gameplay.
EDIT: (The same thing happens with Left and Right Keys)
Here's a picture to understand better what happens:
Left: Just Pressing UP
Right: Pressing UP and DOWN
What could I change in my code to fix this?
Thanks in advance