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

Author Topic: How do I create speed=0 when no key is pressed? (SOLVED)  (Read 2876 times)

0 Members and 1 Guest are viewing this topic.

ingwik

  • Jr. Member
  • **
  • Posts: 65
    • View Profile
How do I create speed=0 when no key is pressed? (SOLVED)
« 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:

Code: [Select]
               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).

Code: [Select]
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?

texus

  • Hero Member
  • *****
  • Posts: 505
    • View Profile
    • TGUI
    • Email
How do I create speed=0 when no key is pressed? (SOLVED)
« Reply #1 on: January 29, 2012, 12:52:30 pm »
Quote
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.
TGUI: C++ SFML GUI

ingwik

  • Jr. Member
  • **
  • Posts: 65
    • View Profile
How do I create speed=0 when no key is pressed? (SOLVED)
« Reply #2 on: January 30, 2012, 11:42:35 am »
Thanks, I didn't know it was a bool value. Using:

Code: [Select]
if (!App.GetInput().IsKeyDown(sf::Key::Down))
 {std::cout << "Down is not pressed " << std::endl;}


works as a charm. Topic solved.