2
\$\begingroup\$
 public void update(float dt){
    float currentYVelocity = body.getLinearVelocity().y;

    switch(currentMoveState){
        case moveLEFT: body.setLinearVelocity(-walkSpeed * ppm, currentYVelocity);
            break;
        case moveRIGHT: body.setLinearVelocity(walkSpeed * ppm, currentYVelocity);
            break;
        case moveSTANDING: body.setLinearVelocity(0,currentYVelocity);
    }

    //handle input...
    if (Gdx.input.isKeyPressed(Input.Keys.A) && body.getLinearVelocity().x >= -2
            && body.getPosition().x >= 0.08f){
        currentMoveState = moveState.moveLEFT;
        //body.applyLinearImpulse(new Vector2(-walkSpeed * ppm,0), body.getWorldCenter(), true);
    }
    else if (Gdx.input.isKeyPressed(Input.Keys.D) && body.getLinearVelocity().x <= 2 ){
        currentMoveState = moveState.moveRIGHT;
        //body.applyLinearImpulse(new Vector2(walkSpeed * ppm, 0), body.getWorldCenter(), true);
    }
    else
        currentMoveState = moveState.moveSTANDING;
    if (Gdx.input.isKeyJustPressed(Input.Keys.SPACE)){
        body.applyLinearImpulse(new Vector2(0, jumpSpeed * ppm), body.getWorldCenter(), true);
    }

    // sets the players texture sprite to be same position as the box2d circle body...
    this.setBounds(body.getPosition().x - 8, body.getPosition().y - 8, 16, 16);
}

I have the above code in place. I'm using LibGDX and Box2d. Im trying to make a single screen platformer similar to old-style Donkey Kong.

My issue is that when I move and jump at the same time, the Y velocity is being heavily affected. When jumping without moving left/right it is fine, but if you hold left whilst jumping the character stays almost suspended in mid air and you can acheive all kinds of irregularities with trying to jump and move.

I tried adding the variable 'currentYvelocity' and passing that into the Y value each time i alter 'setLinearVelocity(x,y)' but I can see that this is the issue because somehow it must be removing the new calulation from Y velocity and replacing it with the previous frame perhaps..?!?

I've wracked my brains and now everything I try makes it worse.

Can anyone tell me of a proper solution to this please? (Or perhaps I'm almost right and just need a helping hint :) )

Anyways, thanks in advance for any help!

EDIT: By the way, I have now changed the currentYVelocity by removing that variable and just calling getLineaarVelocity.y in all the movement methods. But this had no effect unfortunately on my jumping whilst walking problem.

\$\endgroup\$
1
\$\begingroup\$
      //body.applyLinearImpulse(new Vector2(walkSpeed * ppm, 0), body.getWorldCenter(), true);
}
else if (!Gdx.input.isKeyPressed(Input.Keys.A) && !Gdx.input.isKeyPressed(Input.Keys.D))
    currentMoveState = moveState.moveSTANDING;

Maybe the problem was that if the player exceeds the speed limit currentMoveStates changes to moveSTANDING.

\$\endgroup\$
  • \$\begingroup\$ thank you for your input. Unfortunately I am not now workking on this exercise so I havent been able to test your solution. If I do manage to get back to it I'll be sure to report back. Thanks again for help. \$\endgroup\$ – Big T Larrity Nov 10 '16 at 3:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.