Manage libgdx issues for a single clicklistener… here is a solution to the problem.
Manage libgdx issues for a single clicklistener
In Libgdx, I’m trying to set Clicklistener to a specific case when a specific button is pressed.
public void setExamine(final ClickListener cl) {
examine.addListener(cl);
}
This is my code in one class that can be accessed from another class via :
table.setExamine(new ClickListener(){...});
However, doing so means adding a new ClickListener each time.
Is there a way to get it to manage a click listener instead of adding one instead of this happening? I tried using it
public void setExamine(final ClickListener cl) {
examine.clearListeners();
examine.addListener(cl);
}
But it seems to remove any functionality of button clicking altogether.
Solution
You can keep a reference to the “cl” to be passed to the setExamine method in the class. Then you can first remove this particular listener in your setExamine method. I assume that examine is a scene2d actor.
private ClickListener clickListener;
public void setExamine(final ClickListener cl) {
if(clickListener != null) {
examine.removeListener(clickListener);
}
examine.addListener(cl);
clickListener = cl;
}
If you want to apply a solution like clearListeners(), you should keep the button’s default listener.
while(examine.getListeners().size > 1) {
examine.removeListener(examine.getListeners()[1]);
}