Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

libGDX make touchDown loop until touchUp

Tags:

libgdx

I'm making game in libGDX.

I want to implement game controls, but I have problem with touchDown method. touchDown method runs only once. I want to loop in touchDown until touchUp is called. Can someone help me ?

class  onPlayerGoLeftListener extends InputListener {

    public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
        //start runnable   (move player)     

        return true;
    }

    public void touchUp (InputEvent event, float x, float y, int pointer, int button) {
        //stop runnable
    }

}

Thanks

p.s. I don't like to use main update method to do this

like image 812
Jovan Avatar asked Feb 18 '26 10:02

Jovan


2 Answers

You can't "loop" inside an event callback or render callback, as the whole system will stop. One way to get what you want is to set a flag in the touchDown method, clear the flag in the touchUp method, and then put the "body" of your loop in render, guarded by your flag.

boolean touchActive = false;

public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
   //start runnable   (move player)     
   touchActive = true;
   return true;
}

public void touchUp (InputEvent event, float x, float y, int pointer, int button) {
   //stop runnable
   touchActive = false;
}


public void render(float delta) {
   ...
   if (touchActive) {
      // Do one iteration of your "while" loop
   }
   ...
}
like image 103
P.T. Avatar answered Feb 21 '26 12:02

P.T.


In render() or in your draw thread you can just use

 if (Gdx.input.isTouched()){
  //some thing move()
}
like image 21
jpm Avatar answered Feb 21 '26 12:02

jpm



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!