1
\$\begingroup\$

I know that you can just write a code like this in the render method:

 public void render(float delta) {
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    myStage.act(delta);
    myStage.draw();

    if(myTouchpad.isTouched()){
         //do stuff
     }else{
         //do different stuff
     }
}

But I don't think it's a good way to solve this, is there a way to handle this using listeners or something like that?

\$\endgroup\$
1
\$\begingroup\$

Why not just use it with a Stage and Table as intended?

Stage stage = new Stage();
Table table = new Table(skin); // You can skip table and add it directly to the stage as well.
Touchpad touchpad = new Touchpad(deadzoneRadius, skin);

stage.addActor(table);
table.add(touchpad);

Gdx.input.setInputProcessor(stage); // Stage will handle all input and updates for you.

Then you can just use the touchpad methods for whatever you need. If you need to be alerted when the touch pad is pressed you could use a listener.

touchpad.addListener(new ChangeListener() {
    @Override
    public void changed(ChangeEvent event, Actor actor) {
        // This is run when anything is changed on this actor.
        float deltaX = ((Touchpad) actor).getKnobPercentX();
        float deltaY = ((Touchpad) actor).getKnobPercentY();
        ...
    }
});

There are several listeners available (listener superclass javadoc, lists all listeners), but for a Touchpad a ChangeListener is probably best suited for the task.

\$\endgroup\$
0
\$\begingroup\$

You can use a ChangeListener:

touchpad.addListener(new ChangeListener() {
     @Override
     public void changed(ChangeEvent event, Actor actor) {
        //do your things
     }
  });

Add the listener in your show() or create() method or in the screen's constructor and you're done!

\$\endgroup\$

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.