Physics body jumping and landing - Help with logic

I would like to jump up once and then land even when the jump button is pressed continuously.
Currently I am doing something like this.

When the jump button is pressed a flag is set indicating that the player needs to jump . The update loop then makes the character jump.When the jump button is released the character lands back down.Here is the code

void Player_Controls::onTouchesBegan(const std::vector<cocos2d::Touch*>& touch , cocos2d::Event* event)
{
     ....
        else if(JumpButton->boundingBox().containsPoint(pA))
        {
            CCLOG("JumpButton Pressed");
            //Tell the update loop to make this sprite jump
            this->sprite_motion.motionDirection=SpriteMotion::MotionDirection::EnumJump;
        }
}


   //Set a flag when player releases the button 
   void Player_Controls::onTouchesEnded(const std::vector<cocos2d::Touch*>& touch , cocos2d::Event* event)
    {
            ....
            ....
            if(JumpButton->boundingBox().containsPoint(pA))
            {
                CCLOG("FireButton Released");
                this->sprite_motion.motionDirection=SpriteMotion::MotionDirection::EnumFall; //Tell the loop to make character fall down
            }
    }

This is what goes in the update loop

 if( this->sprite_motion.motionDirection == SpriteMotion::MotionDirection::EnumJump)
    {

               eagle->getPhysicsBody()->setVelocity(Vec2(0,400));
               this->sprite_motion.motionDirection=SpriteMotion::MotionDirection::EnumNone;
    }
    if( this->sprite_motion.motionDirection == SpriteMotion::MotionDirection::EnumFall)
    {
        CCLOG("Falling Down");
        eagle->getPhysicsBody()->setVelocity(Vec2(0,-400));
        this->sprite_motion.motionDirection=SpriteMotion::MotionDirection::EnumNone;
    }

Now the problem with this approach occurs if the jump button is continuously pressed and not released. The character keeps on going higher and higher. I wanted to know how to emulate a single jump even if the jump button is pressed continuously.

implementing some kind of state machine early into your control logic will help in the long run as well, this is a good reference point to start http://gameprogrammingpatterns.com/state.html

I think you forgot to post the link to the url

lol, thanks for pointing that out it has been fixed.
here u go direct link seems like linker problem Arrrrgggghhhhh…

Not really :smile:

Instead of a boolean flag, set a float with the number of seconds you want the jump to take.
Then, in your update(float dt); method …

if (jumpTimer > 0)
{
  jumpTimer -= dt;
  if (jumpTimer <= 0)
  {
    stopJumping(); // implement this to start falling
  }
}
1 Like

You can also use function pointers/jump maps and get rid of those nasty branches.

I am not sure which branches are you talking about.

I’m talking about your if/else constructs.

It was just a suggestion, that there are possibilities to get rid of them. It’s just that I don’t like them :smile:

Performance depends on how often your branches are taken. If they are taken every frame, if could harm performance.

I totally agree. I will definitely be refactoring my code. I am currently in a prototyping phase and the code is all messy. Thanks for the suggestion though.