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

Author Topic: Jump Code  (Read 1339 times)

0 Members and 1 Guest are viewing this topic.

alexb9054

  • Newbie
  • *
  • Posts: 4
    • View Profile
Jump Code
« on: October 08, 2017, 06:13:07 pm »
I've been a bit stuck for the past few days. Basically what I want to happen is when pImage(player Sprite) hits a certain height gravity activates and brings the player down. That way when you press the space key you don't continue going up and when released you go down. I Want it similar to that of a mario or traditional platformer. I currently have collision done, it's just this one part that has been driving me mad. I'm a bit new to this so I'm sure there is an easy solution to this problem.
 
                if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Space))
                {
                        source.y = Up;
                        pImage.move(0, -moveSpeed* clock.getElapsedTime().asSeconds());
                        //Debugging: Getting Player Position
                        std::cout << "Position X: " << pImage.getPosition().x
                                << "||Position Y: " << pImage.getPosition().y << std::endl;
                }
                else if (pImage.getPosition().y >= 341)
                {
                        pImage.move(0, moveSpeed* clock.getElapsedTime().asSeconds());
                }

fallahn

  • Hero Member
  • *****
  • Posts: 507
  • Buns.
    • View Profile
    • Trederia
Re: Jump Code
« Reply #1 on: October 08, 2017, 11:23:56 pm »
Check out the Sonic Physics guide
http://info.sonicretro.org/SPG:Jumping

It has loads of really useful info on platform physics :) It's worth noting that gravity isn't 'activated' at some point in the jump - it's applied all the time the character is in the air.

alexb9054

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Jump Code
« Reply #2 on: October 09, 2017, 07:46:54 pm »
Thanks. I'll give that a shot