SFML community forums
Help => General => Topic started by: ingwik on January 29, 2012, 12:20:33 pm
-
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?
-
If I make a Windows program, I can use key down and key up commands, but SFML seems to only have key down.
When IsKeyDown returnes false then your key is up. You don't need a IsKeyUp function.
For your problem, just add something like this:
- Check if both left and right key are up (check if IsKeyDown returns false)
- If speed is negative then add a value to the speed until it is 0
- If speed is positive then reduce the speed until it is 0.
-
Thanks, I didn't know it was a bool value. Using:
if (!App.GetInput().IsKeyDown(sf::Key::Down))
{std::cout << "Down is not pressed " << std::endl;}
works as a charm. Topic solved.