1

I'm trying to make an android game with libgdx, and am having problems with detecting if a button is being held down. I want to be able to detect when a button is being held down because i want to move my character when the button is down and stop him when it is up.

I really just want the button to work like an arrow key on the keyboard.

This is what i'm currently using:

    buttonRight.addListener(new ClickListener() {
        @Override
        public void clicked(InputEvent event, float x, float y) {
            levels.get(index - 1).getBlackPoint().moveRight();
        }
    });

i've tried other ways like using a change listener, but couldn't get it to work.

I want to have four buttons on my gamescreen, left, right, up, down. Each button will move the character while the user is holding the button down. Once the button is released the character will stop moving until another one of the four buttons is held down.

  • I think the question needs more refinement to get some useful help. You may at least show a part of your program or pseudocode that you want to implement. – Keugyeol Apr 27 '14 at 22:37
  • i have updated the question, hopefully this all makes sense. I'm still learning the ropes. – TouchyGibbons Apr 27 '14 at 23:40
  • The problem seems you are using onClick instead of onTouch. I'm not into android but you may find the following Q&As useful: 1. http://stackoverflow.com/questions/2615616/triggering-event-when-button-is-pressed-down-in-android 2. http://stackoverflow.com/questions/13409271/android-how-to-know-if-button-is-held-down?rq=1 – Keugyeol Apr 28 '14 at 00:12
  • Those Q&As are awesome, but they still don't answer my question. The problem is that I'm using the libgdx buttons and can't set an onTouchListener to them. Is there away that i could do that? – TouchyGibbons Apr 28 '14 at 00:35
  • Have you tried in your listener, the `touchdown`, `touchup` and/or `touchDragged` event handlers? By the description [here](http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/scenes/scene2d/utils/ClickListener.html), they seem equivalent to `onTouch` event handler. You might also need to use `isPressed()` to continuously monitor the pressed state. – Keugyeol Apr 28 '14 at 01:16
  • Thank you! i've solved the problem! I used my original clickListener and just added the touchup and touchdown methods to it. Works great! Thanks for you help! – TouchyGibbons Apr 28 '14 at 02:21
  • 1
    @TouchyGibbons It would be helpful if you share your solution in detail in the answers section. – Sunil Kumar Sep 27 '15 at 09:07

0 Answers0