Search code examples
javaandroidlibgdxspriteontouch

Libgdx Sprite not moving smoothly


I am having a problem where when I go to move my sprite it stops and I have to press again for the sprite to move again.

What I want is when the user holds their input down the character keeps moving in the inputted direction, instead of what is happening at the moment where the input will make the character move a tiny bit and need the finger lifting and inputing again. I have attached the below to try and help explain.

What is currently happening:

enter image description here

And What I want:

enter image description here

My code is as below, please advise on why this is happening and if possible what I need to change

 @Override
    public boolean touchDown(int screenX, int screenY, int pointer, int button) {


        if (Gdx.input.isTouched()) {

          player.b2body.applyLinearImpulse(new Vector2(5.1f, 0), player.b2body.getWorldCenter(), true);

            }

        }
            return true;
          }

As an example also of simlar code that work is here: https://github.com/BrentAureli/SuperMario/blob/master/core/src/com/brentaureli/mariobros/Screens/PlayScreen.java


Solution

  • I am not absolutely sure, but I think your problem is that you are calling this code from the touchDown method. I think this method only gets called once when a touch/click occurs. For your desired outcome, move the code

    if (Gdx.input.isTouched()) {
    
              player.b2body.applyLinearImpulse(new Vector2(5.1f, 0), player.b2body.getWorldCenter(), true);
    
                }
    
            }
    

    into your game loop/ render method