Environment: Win 7 Enterprise 64, SFML 1.6, Visual C++ express 2010
I need some help to solve this problem, or to give me some ideas how to move on.
I have a platform game, just a test bed and without animations. In the code I have a small class for players, and in the class I store speed in X and Y as doubles: dspeed_X and dspeed_Y and a sprite. I create a player at program start named ball1.
When I hit the L/R arrow keys, I have this code to accelerate the ball:
if (App.GetInput().IsKeyDown(sf::Key::Left)) //Move left
{
if (ball1.dspeed_X > -0.1)
{ball1.dspeed_X = -0.1;}
else
(ball1.dspeed_X = (ball1.dspeed_X - (-ball1.dspeed_X * ElapsedTime))/2);
}
if (App.GetInput().IsKeyDown(sf::Key::Right)) //Move right
{
if (ball1.dspeed_X < 0.1)
{ball1.dspeed_X = 0.1;}
else
(ball1.dspeed_X = (boll1.dspeed_X + (boll1.dspeed_X * ElapsedTime))/2);
}
Further down in the code, I add the movement (dspeed_Y is calculated depending on if the sprite is standing on firm ground or not).
ball1.sprite.Move(ball1.dspeed_X, ball1.dspeed_Y); //Move the ball
The problem is that I want the ball to stand still, preferably decelerate/continually loose speed until it reach 0, when I don't press any key at all, and I just can't figure out how to do it. If I make a Windows program, I can use key down and key up commands, but SFML seems to only have key down.
So the question is:
How do I do to move a sprite with the arrow keys, that loose speed and finally get 0 speed when no key at all is pressed?