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
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
}
...
}
In render() or in your draw thread you can just use
if (Gdx.input.isTouched()){
//some thing move()
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With